Oliver Nassar

OSX Bash Script Execution through Keyboard Shortcut

June 30, 2012

I got an email the other day updating me on the Chocolat text editor, and how it's officially at version 1.0.

I gave it a shot, and it's a really solid editor. I suppose I like it so much due to it's simplicity. Not many options, or configurations, just the editor.

Anyway, as a a faithful Coda user for years, I got accustomed to the shortcut keys, but more than anything, the plugins architecture. I've created two plugins. One simply truncates bodies of text to 80 characters, while the other runs an rsync script that pushes the contents of a directory to a Parallels VM I have running.

While Chocolat doens't (yet) have a plugin system, I was able to find a workaround using a technique I saw on on Super User.

It addresses the question: osx snow leopard - Starting application with custom keyboard shortcut

While the full example is pretty thorough, I wanted to walk through the process I undertook to have some bash code/script run through a shortcut key. I'm going to split this into two pieces.

Creating a Service

From any application, you can go to the Application Menu, down to Services and choose one. These services can do a variety of things, such as copy/paste the open file's text, upload the file through an FTP application, etc.

We want to create a service that runs a bash command/script.

  1. Prepare the bash command and test it in Terminal.app
  2. Open up Automator.app
  3. Create a new Service
  4. In the left column, under the Library header, choose the Utilities option
  5. In the column to the right, double-click the Run Shell Script option
  6. Paste your command into the textarea
  7. Above, under the Service received selected drop-down list, choose no input
  8. Hit Command-S to save it, and give it a name (in my case "rsync")

Creating the shortcut key

Now that the service has been created, we want to make sure it is both accessible through the the Services submenu, as well as having a shortcut key bound to it.

Here are the steps to do both:

  1. Open up System Preferences.app
  2. Go to the Keyboard settings
  3. On the left, choose the Services option
  4. Scroll to the bottom, and ensure the rsync (or whatever name you gave it) option is checked
  5. When selecting the service, a button titled add shortcut should become visible. Choose this, and add your shortcut

As it often the case, I hope this post reminds me of the process later on, say after a reformat, and that it helps others trying to do the same :)