Skip to content

Instantly share code, notes, and snippets.

@ericrasch
Created April 22, 2025 12:07
Show Gist options
  • Save ericrasch/eee1f678ac7755832a1543d618adb7c6 to your computer and use it in GitHub Desktop.
Save ericrasch/eee1f678ac7755832a1543d618adb7c6 to your computer and use it in GitHub Desktop.
Converts exported Alfred snippets into Raycast import format.
################################################################################
# Script Name: convert_alfred_snippets.py
#
# Converts exported Alfred snippets into Raycast import format.
#
# Usage:
# python3 convert_alfred_snippets.py
#
# Requirements:
# - Python 3.x
# - All exported Alfred snippet .json files should be located in SOURCE_DIR
#
# Behavior:
# - Scans each JSON file in SOURCE_DIR for valid snippet content.
# - Converts "name", "keyword", and "snippet" fields to Raycast format.
# - Prefixes each keyword with "1" to avoid accidental triggers.
# - Outputs a combined Raycast-formatted JSON file suitable for import.
#
# Author: Eric Rasch
# GitHub: https://github.com/ericrasch/
# Created: 2025-04-17
# Updated: 2025-04-21
# Version: 1.1
################################################################################
import json
import uuid
from pathlib import Path
# Set the input folder containing Alfred snippet .json files
SOURCE_DIR = Path("~/Documents/AlfredSnippets").expanduser()
# Set the output Raycast-formatted JSON file
OUTPUT_FILE = Path("./snippets.json")
# Function to extract relevant snippet data from an Alfred JSON file
def extract_from_alfred(file_path):
try:
with open(file_path, "r") as f:
raw = json.load(f)
data = raw.get("alfredsnippet", {}) # Extract core snippet fields
return {
"name": data.get("name", "Untitled"), # Snippet name
"text": data.get("snippet", ""), # Snippet content
"keyword": "1" + data.get("keyword", "") # Prefixed keyword
}
except Exception as e:
print(f"⚠️ Error processing {file_path.name}: {e}")
return None
# Main execution function
def main():
print(f"📁 Reading Alfred snippets from: {SOURCE_DIR}")
snippets = []
# Process each .json file in the directory
for file in SOURCE_DIR.glob("*.json"):
if file.name == "snippets.json": # Avoid re-processing output file
continue
result = extract_from_alfred(file)
if result and result["text"]:
snippets.append(result)
else:
print(f"⛔ Skipped empty or malformed: {file.name}")
# Write all valid converted snippets to Raycast import format
if snippets:
with open(OUTPUT_FILE, "w") as out:
json.dump(snippets, out, indent=2)
print(f"✅ Exported {len(snippets)} Raycast-ready snippets to: {OUTPUT_FILE}")
else:
print("🚫 No valid snippets found.")
# Entry point
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment