MacOS – Assign monospaced font family to selected text with keyboard shortcut in mac os x

fontkeyboardmacos

I write a lot of technical emails, and like to use a monospaced font for the code bits. Highlight text, hit apple-T, choose my font from the list of favorites, then close the font window. Is there a way to use a keyboard shortcut to apply a given font to the currently selected text? Ideally this would be OS-wide, but if that's not possible I can use it for just a single app (Mail.app)

Best Answer

I've been looking for doing this for a very long time.

I finally managed to end up with something that really works great for me, with the help of Alfred and Highlight CLI tool.

Here is the Alfred script portion :

query="{query}"
if echo $query|grep -e "^__SHORTCUT__"
then
    echo "$query"|sed -E "s/^__SHORTCUT__//"|pbcopy
    syntax=txt
else
    osascript -e 'tell application "System Events" to keystroke "c" using {command down}'
    syntax=$query
fi

pbpaste|/usr/local/bin/highlight --syntax $syntax --font Menlo --font-size 12 --out-format=rtf|pbcopy
osascript -e 'tell application "System Events" to keystroke "v" using {command down}'

Make sure the shortcut trigger adds __SHORTCUT__ to be recognized and argument is "Selection in OSX" :

enter image description here

Short demo https://youtu.be/y8NJT52APRQ