Skip to content

Instantly share code, notes, and snippets.

@etiennejcharles
Last active January 30, 2025 21:39
Show Gist options
  • Select an option

  • Save etiennejcharles/97a0e20149b577cd4fb87fef69fb6f80 to your computer and use it in GitHub Desktop.

Select an option

Save etiennejcharles/97a0e20149b577cd4fb87fef69fb6f80 to your computer and use it in GitHub Desktop.
Convert Alfred 5 Snippets to Raycast
#!/bin/bash
# ----------------------------------------------------------------------------
# Script: convert_alfred_5_snippets.sh
# ----------------------------------------------------------------------------
# Purpose:
# This script automates the process of exporting snippets from Alfred,
# converting them into the Raycast snippet format, and ensuring they are
# correctly formatted for manual import into Raycast.
#
# Functionality:
# - Extracts Alfred snippets from the correct path.
# - Removes duplicate snippets based on name, text, and keyword.
# - Deletes the "keyword" property since Raycast does not use it.
# - Saves the output as `converted_alfred_5_snippets.json` on the Desktop.
# - Provides instructions to manually import the snippets into Raycast,
# since Raycast does not support direct snippet imports via script.
#
# Notes:
# - This script does not automatically import snippets into Raycast due to
# the app's import restrictions.
# - It ensures a clean conversion from Alfred snippets to Raycast format.
#
# Usage:
# 1. Run this script:
# chmod +x convert_alfred_5_snippets.sh && ./convert_alfred_5_snippets.sh
# 2. Follow the instructions in the output to manually import into Raycast.
# ----------------------------------------------------------------------------
# Dynamically find the Alfred preferences path
PREFS_FILE="$HOME/Library/Application Support/Alfred/prefs.json"
if [ -f "$PREFS_FILE" ]; then
# Extract the current preferences path from prefs.json
ALFRED_PREFS_PATH=$(jq -r '.current' "$PREFS_FILE")
if [ -n "$ALFRED_PREFS_PATH" ] && [ "$ALFRED_PREFS_PATH" != "null" ]; then
ALFRED_SNIPPETS_PATH="$ALFRED_PREFS_PATH/snippets"
echo "✅ Found Alfred snippets path from prefs.json"
else
echo "⚠️ Could not find current path in prefs.json, using default path..."
ALFRED_SNIPPETS_PATH="$HOME/Library/CloudStorage/Dropbox/Etienne Inc/Web/Alfred Sync/Alfred.alfredpreferences/snippets"
fi
else
echo "⚠️ Could not find Alfred prefs.json, using default path..."
ALFRED_SNIPPETS_PATH="$HOME/Library/CloudStorage/Dropbox/Etienne Inc/Web/Alfred Sync/Alfred.alfredpreferences/snippets"
fi
EXPORT_PATH="$HOME/Desktop/converted_snippets.json"
# Validate Alfred snippets path
if [ ! -d "$ALFRED_SNIPPETS_PATH" ]; then
echo "❌ Error: Alfred snippets folder not found at: $ALFRED_SNIPPETS_PATH"
exit 1
fi
# Create an empty JSON array to store formatted snippets
echo "[]" > "$EXPORT_PATH"
# First, list all snippet categories and their contents
echo "🔍 Found the following Alfred snippet categories:"
echo "----------------------------------------"
folder_count=0
total_snippets=0
for category in "$ALFRED_SNIPPETS_PATH"/*; do
if [ ! -d "$category" ]; then
continue
fi
category_name=$(basename "$category")
if [ "$category_name" = "info.plist" ]; then
continue
fi
((folder_count++))
snippet_count=$(find "$category" -name "*.json" -not -name "info.plist" | wc -l | tr -d ' ')
((total_snippets+=snippet_count))
echo "📁 $category_name ($snippet_count snippets)"
done
echo "----------------------------------------"
echo "Total categories: $folder_count"
echo "Total snippets found: $total_snippets"
# Prompt for confirmation
echo -e "\nDo you want to proceed with converting these snippets to Raycast format? (y/n)"
read -r response
if [[ ! $response =~ ^[Yy]$ ]]; then
echo "❌ Operation cancelled by user"
exit 0
fi
# Extract and process each snippet file
echo -e "\n🔄 Processing Alfred snippets..."
declare -a transferred_snippets=()
# Process snippets by category
for category in "$ALFRED_SNIPPETS_PATH"/*; do
if [ ! -d "$category" ] || [ "$(basename "$category")" = "info.plist" ]; then
continue
fi
category_name=$(basename "$category")
echo -e "\n📁 Processing category: $category_name"
# Extract and process each snippet file in the category
for snippet_file in "$category"/*.json; do
if [ ! -f "$snippet_file" ] || [[ "$(basename "$snippet_file")" == "info.plist" ]]; then
continue
fi
# Extract relevant fields from the nested alfredsnippet structure
name=$(jq -r '.alfredsnippet.name' "$snippet_file")
text=$(jq -r '.alfredsnippet.snippet' "$snippet_file")
keyword=$(jq -r '.alfredsnippet.keyword' "$snippet_file")
# Skip if we couldn't extract the name or text
if [ "$name" = "null" ] || [ "$text" = "null" ]; then
echo "⚠️ Skipping invalid snippet file: $(basename "$snippet_file")"
continue
fi
# Check for duplicates (same name and text)
if jq -e --arg name "$name" --arg text "$text" \
'.[] | select(.name == $name and .text == $text)' \
"$EXPORT_PATH" > /dev/null; then
echo "⚠️ Skipping duplicate snippet: $name"
continue
fi
# Format snippet for Raycast (include keyword only if it's not empty)
if [ -z "$keyword" ] || [ "$keyword" = "null" ]; then
snippet_entry="{\"name\": \"$name\", \"text\": \"$text\"}"
else
snippet_entry="{\"name\": \"$name\", \"text\": \"$text\", \"keyword\": \"$keyword\"}"
fi
# Append snippet to the JSON file
jq --argjson snippet "$snippet_entry" '. += [$snippet]' "$EXPORT_PATH" > "${EXPORT_PATH}.tmp"
mv "${EXPORT_PATH}.tmp" "$EXPORT_PATH"
transferred_snippets+=("$name")
echo "✅ Added: $name"
done
done
echo "🎉 All Alfred snippets have been processed and saved in: $EXPORT_PATH"
# Display summary of transferred snippets only if there are any
if [ ${#transferred_snippets[@]} -eq 0 ]; then
echo -e "\n⚠️ No snippets were transferred. Please check that:"
echo "1. The Alfred snippets directory is not empty"
echo "2. The snippets are in the correct JSON format"
echo "3. You have the necessary permissions to read the files"
exit 1
fi
# Only show summary and instructions if snippets were transferred
echo -e "\n📋 Summary of Transferred Snippets:"
echo "----------------------------------------"
for ((i=0; i<${#transferred_snippets[@]}; i++)); do
echo "$(($i+1)). ${transferred_snippets[$i]}"
done
echo "----------------------------------------"
echo "Total snippets transferred: ${#transferred_snippets[@]}"
echo -e "\n"
# Instructions for manual import to Raycast
echo "📌 Next Steps:"
echo "1️⃣ Open Raycast."
echo "2️⃣ Navigate to Extensions > Snippets."
echo "3️⃣ Click 'Import Snippets' and select '$EXPORT_PATH'."
echo "4️⃣ Review and confirm import."
echo "✅ Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment