Skip to main content

create_alfred_snippet.sh

1#!/usr/bin/env bash
2# This script creates an Alfred snippet bundle that expands my HTML template
3# when I type the shortcut `!html`.
5set -o errexit
6set -o nounset
8KEYWORD="!html"
10ROOT=$(git rev-parse --show-toplevel)
12pushd $(mktemp -d)
14 # 1. Create the `info.plist` files with the settings for this collection
15 # of snippets: no keyword prefix/suffix.
16 cat > info.plist <<EOF
17<?xml version="1.0" encoding="UTF-8"?>
18<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
19<plist version="1.0">
20<dict>
21 <key>snippetkeywordprefix</key>
22 <string></string>
23 <key>snippetkeywordsuffix</key>
24 <string></string>
25</dict>
26</plist>
27EOF
29 # Get the HTML template as a JSON-escaped string
30 snippet=$(jq -R -s '.' < "$ROOT/TEMPLATE.html")
32 # This is an arbitrary choice of ID; it just has to be used consistently
33 snippet_id="$(uuidgen)"
35 # 2. Create the snippet JSON file, which expands the HTML when I type the
36 # given keyword.
37 cat > "$snippet_id.json" <<EOF
39 "alfredsnippet": {
40 "snippet": $snippet,
41 "uid": "$snippet_id",
42 "name": "HTML template",
43 "keyword": "$KEYWORD"
44 }
46EOF
48 # 3. Bundle the info.plist and snippet JSON into an `alfredsnippets`
49 # package, and open it in Alfred.
50 zip "HTML template.alfredsnippets" "info.plist" "$snippet_id.json"
51 open "HTML template.alfredsnippets"
52popd