OS X Context Sensitive Shortcut Cheatsheets

OS X Context Sensitive Shortcuts

Mastering shortcuts in OS X can be a nice little productivity boost. It’s also a fun way to discover special features in some of your most commonly used applications. For example, I never knew that Ctrl-Cmd-P in nvALT brings up a preview window that shows your markdown rendered as HTML, now I use it all the time.

Speaking of markdown, I bootstrapped my learning of it by using Byword shortcuts to generate the syntax (Cmd+ for headings, Cmd-I for emphasis, Cmd-B for strong, Cmd-K for links, Opt-Cmd-I for images and so on…). After a short time seeing Byword generate the syntax, I soon became comfortable doing it myself. But it begs the question: how do you learn the shortcuts!? What I wanted was something that would show the shortcuts that I want to learn and have them be “context sensitive” so that they were relevant to the foremost application.

Some potential solutions

A search in the Mac AppStore for “shortcuts” will return a few promising looking apps, including some well known tools like TextExpander, Keyboard Maestro and BetterSnapTool which I already had installed and could be sort of manipulated into some kind of solution but ultimately didn’t fit the bill.

There are also apps specifically designed to scratch this particular itch, notably one called “CheatSheet” looked pretty good, you simply hold down the command key and a CheatSheet would show a HUD with a list of every shortcut in the current application. For my tastes though every shortcut is overkill, I was only really interested in the obscure ones that unlock the functionality that I frequently use.

More googling led me to “Cheaters” by Brett Terpstra. I almost settled on this but it relies on a window showing all the time and I didn’t fancy the idea of constantly moving it around my screen to get it out of the way or to get it into view, and it isn’t context sensitive.

Initial attempt with GeekTool

I finally settled on good old GeekTool. For the uninitiated – GeekTool lets you place text from various sources on your OS X desktop. The basic solution I started with was to simply have a plain text geeklet that listed a bunch of shortcuts and their corresponding function.

Like all intelligent, good-looking people, I have my OS X dock pinned to the left hand side of the screen (i.e. the “correct” way) and so I tend to have the top left area of my desktop showing through even when my screen is full of windows. Showing the geeklet in that spot ensures it’s not completely obscured and it’s also nice and close to the foremost application’s menus – which would later be significant when I made it context sensitive.

I also set the geeklet in a fixed width font (Adobe’s new Source Code Pro is my new favourite) so that the list of shortcuts and corresponding functions would show in two orderly columns.

Even so, this basic solution didn’t fit my requirements, there were dozens of shortcuts I’d like to learn and so I needed the list to be filtered based on the foremost application or I could easily fill my whole desktop. In other words I had to make the geeklet context sensitive.

AppleScript completes the picture

One of GeekTool’s most useful features is it’s ability to execute a command and display the output. The canonical example is an “uptime” geeklet which shows the formatted output of a call to the OS X uptime command.

You can also use this feature of GeekTool to execute AppleScript via OS X’s osascript command. So if I could create an AppleScript which detects the foremost application and prints a custom list of shortcuts that I’m interested in for that application, I can use GeekTool to execute that script and display the results on my desktop.

The key piece of AppleScript is this:

shortcutkeys = ""
tell application "System Events"
    set app_name to name of first process whose frontmost is true
    if (app_name = "Google Chrome") then
        set shortcutkeys to shortcutkeys & "⌥⌘I Developer Tools"
end tell

(BTW, syntax puke city, I hate AppleScript.)

So “app_name” now contains a string such as “Finder” or “Byword” or “BBEdit”, I could then use this to format up a string containing all the shortcuts I’m interested in for whatever application has focus. Then to return that string to GeekTool do something like:

set output to shortcutkeys

Then save that script somewhere and set up a geeklet to call it. e.g.:

osascript /Users/adrian/Documents/Scripts/ShortcutHelper.scpt

And remember to output it using a monospaced font.

Here’s a link to download my own complete script which includes various shortcuts I use in Finder, BBEdit, nvALT, TaskPaper, Reeder, Chrome, Coda and Byword. Note that I have mapped some custom shortcuts that probably won’t work on your system, so you should edit this script to suit your own needs.