My Alfred workflow

You can download the workflow from osxnotes.alfredworkflow.

u searches for Unicode characters by character name and inserts the selected character, like C-x 8 <RET> in Emacs. It uses a script filter like this:

echo '<?xml version="1.0"?><items>'
x="{query}" awk -F\; 'tolower($3)~tolower(ENVIRON["x"]){gsub("&","\\\&amp;",$1);gsub("\"","\\\&quot;",$1);gsub("<","\\\&lt;",$1);gsub(">","\\\&gt;",$1);print"<item><arg>"$1"</arg><title>"$1" "$2" "$3"</title><icon></icon></item>"}' unicode
echo '</items></xml>'

The unicode file contains the output of this command:

curl|while read l;do [[ ${l#*;} != @(<|SEMICOLON;)* ]]&&printf \\U$(printf %08d $((0x${l%%;*})));printf ';%s\n' "$l";done

s searches for files including files treated as system files in the search results.

A hotkey opens the file browser in the home directory, so that it is equivalent to invoking Alfred and typing a tilde.

Using a text file to configure custom web search URLs

#!/usr/bin/env bash

amp() { local x=${1//&/&amp;};x=${x//</&lt;};printf %s "${x//>/&gt;}"; }

out='<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">

while read l;do
  keyword=$(amp "${l% *}")
  url=$(amp "${l#* }")
  uuid=$(printf %26s $(printf %s "$keyword"|xxd -p))
  uuid=${uuid// /0}
  uuid=4c3ab872-${uuid:0:4}-${uuid:4:4}-${uuid:8:4}-${uuid:12:14} # the first part is just a random hex string
<string>$keyword {query}</string>

printf %s\\n "$out</dict>
</plist>">~/Library/Application\ Support/Alfred\ 2/Alfred.alfredpreferences/preferences/features/websearch/prefs.plist

Alfred registers changes to websearch/prefs.plist automatically within a few seconds. The script generates UUID-like strings that depend on the keyword instead of actual UUIDs, because the UUIDs are used to identify items in order to rank frequently used items higher in the search results.

~/n/urls contains lines like this:


~/Library/LaunchAgents/alfredwebsearch.plist runs the script when ~/n/urls is modified:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "">
<plist version="1.0">

I use this function to open URLs in ~/n/urls from a shell:

url(){ local x y;while read x y;do if [[ $x = "$1" ]];then open "${y//'{query}'/$(jq -Rr @uri "${*:2}")}";break;fi;done<~/n/urls; }

For example url ru query opens

Workflow notes

Hotkeys have a short delay by default, but you can reduce it by secondary-clicking a shortcut recorder and changing the "Trigger behaviour" option from "Simulate modifier keys released" to "Pass through modifier keys (fastest)".

You can get the text selected in the frontmost application as input by adding a hotkey input element and setting the argument option to "Selection in OS X". You can insert text in the frontmost application by adding a "Copy to Clipboard" output element and checking the "Automatically paste text to frontmost app" option.

Displaying or searching the clipboard history from a shell

clip(){ sqlite3 ~/Library/Application\ Support/Alfred\ 2/Databases/clipboard.alfdb 'select replace(item,"\\","\\\\")||"\\0"from clipboard'|perl -pe's/\\0/\0/g;s/\\\\/\\/g'|([[ $1 ]]&&ggrep -zZF "$@")|awk 1; }

-z uses null as the input delimiter and -Z uses null as the output delimiter in GNU grep but both grep -z and grep -Z are equivalent to zgrep in OS X's grep.

awk 1 adds a linefeed to the end of the input if the input does not end with a linefeed and is not empty.