Alfred

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 www.unicode.org/Public/UNIDATA/UnicodeData.txt|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" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>customSites</key>
<dict>'

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
  out+="<key>$uuid</key>
<dict>
<key>enabled</key>
<true/>
<key>keyword</key>
<string>$keyword</string>
<key>plusSpaces</key>
<false/>
<key>text</key>
<string>$keyword {query}</string>
<key>url</key>
<string>$url</string>
<key>utf8</key>
<true/>
</dict>"
done<~/n/urls

printf %s\\n "$out</dict>
</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:

a https://www.amazon.com/s/?field-keywords={query}
bt https://btdb.in/q/{query}/
e https://www.ebay.com/sch/i.html?_nkw={query}
enja https://translate.google.com/#en/ja/{query}
i http://www.google.com/search?q={query}&tbm=isch
jpop http://www.jpopsuki.eu/torrents.php?searchstr={query}
maps http://maps.google.com/maps?q={query}
music http://www.last.fm/music/{query}
nyaa http://nyaa.eu/?page=search&term={query}&sort=2
ru http://rutracker.org/forum/tracker.php?nm={query}
scho https://scholar.google.com/scholar?as_vis=1&q={query}&hl=en&as_sdt=1,5
trau https://translate.google.com/#auto/en/{query}
tru http://translate.google.com/translate?sl=auto&tl=en&u={query}
w http://en.wikipedia.org/w/index.php?title=Special%3ASearch&search={query}
wiki http://en.wikipedia.org/wiki/{query}
y http://youtube.com/results?search_query={query}

~/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" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>alfredwebsearch</string>
  <key>Program</key>
  <string>alfredwebsearch</string>
  <key>WatchPaths</key>
  <array>
    <string>~/n/urls</string>
  </array>
</dict>
</plist>

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 http://rutracker.org/forum/tracker.php?nm=query.

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.