main
1#!/usr/bin/env bash
2# List org-capture templates for raffi script filter.
3# Outputs Alfred Script Filter JSON format.
4# Usage: org-capture-raffi [query]
5set -euo pipefail
6
7query="${1:-}"
8
9# Get capture templates from running Emacs
10templates=$(emacsclient --eval '
11(mapconcat
12 (lambda (tpl)
13 (let ((key (car tpl))
14 (desc (cadr tpl))
15 (selectable (> (length tpl) 2)))
16 (when selectable
17 (format "%s\t%s" key desc))))
18 org-capture-templates "\n")
19' 2>/dev/null | sed 's/^"//;s/"$//' | sed 's/\\n/\n/g')
20
21items="[]"
22while IFS=$'\t' read -r key desc; do
23 [[ -z "$key" ]] && continue
24 [[ -z "$desc" ]] && continue
25
26 if [[ -n "$query" ]] && ! echo "$key $desc" | grep -qi "$query"; then
27 continue
28 fi
29
30 items=$(jq --arg key "$key" --arg desc "$desc" \
31 '. + [{"title": $desc, "subtitle": "Capture template: \($key)", "arg": $key}]' <<< "$items")
32done <<< "$templates"
33
34jq -n --argjson items "$items" '{"items": $items}'