Skip to content

Instantly share code, notes, and snippets.

@aaronzs
Created February 25, 2023 03:15
Show Gist options
  • Save aaronzs/68d4b03b71165b87d996579e7404a197 to your computer and use it in GitHub Desktop.
Save aaronzs/68d4b03b71165b87d996579e7404a197 to your computer and use it in GitHub Desktop.
Convert Alfred snippets to Raycast snippets
"""
Preparation:
Export Alfred snippets collection by right-clicking on the collection
and select "Export...".
Usage:
python3 alfred_raycast_snippets_cvt.py <alfred_snippets> [--output <output>]
"""
import argparse
import json
import plistlib
import zipfile
from functools import partial
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("alfred_snippets", type=str)
parser.add_argument("--output", type=str, default="raycast_snippets.json")
return parser.parse_args()
def read_alfred_snippets(alfred_snippets_path: str):
alfred_snippets_zip = zipfile.Path(alfred_snippets_path)
alfred_snippets = []
info = {"snippetkeywordprefix": "", "snippetkeywordsuffix": ""}
for p in alfred_snippets_zip.iterdir():
if p.name.endswith(".json"):
alfred_snippets.append(json.loads(p.read_text()))
elif p.name.endswith("info.plist"):
info = plistlib.loads(p.read_bytes(), fmt=plistlib.FMT_XML)
return alfred_snippets, info
def _convert(alfred: dict, kwprefix: str = "", kwsuffix: str = "") -> dict:
return {
"name": alfred["alfredsnippet"]["name"],
"text": alfred["alfredsnippet"]["snippet"],
"keyword": f"{kwprefix}{alfred['alfredsnippet']['keyword']}{kwsuffix}",
}
def main():
args = parse_args()
alfred_snippets, info = read_alfred_snippets(args.alfred_snippets)
convert = partial(
_convert,
kwprefix=info["snippetkeywordprefix"],
kwsuffix=info["snippetkeywordsuffix"],
)
json.dump(
[convert(snippet) for snippet in alfred_snippets],
open(args.output, "w", encoding="utf-8"),
indent=2,
ensure_ascii=False,
)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment