For my conference talks last year, I used Showoff which I really like. I can edit my slides in Markdown and it is really easy to include source code that is automatically syntax-highlighted.

I’m putting the finishing touches on my talk for Mountain West Ruby Conference next week. This talk is much more visual and contains less code than my other talks. A designer at my company is doing a bunch of really cool animations for me and we decided that Keynote would be a better option for what we needed. However, I needed to find a good solution for including syntax-highlighted code.

Another co-worker has used a workflow that involves generating the syntax-highlighted code as HTML and then copying and pasting from Safari. I believe he picked this up from this Apple StackExchange post. I wanted something much less tedious, so I did some digging.

I first ran across this gist, which talks about using highlight and piping its output to the very handy pbcopy command, which places the output in the clipboard for pasting into Keynote.

One of the comments on the gist pointed to another gist that shows how to use OS/X’s Automator to make the process simpler. I followed the instructions with a few tweaks to the script, and it works like a charm. This gist also includes instructions for using pygments instead of highlight if you prefer.

With this setup in place, I copy the code I want to include and paste it onto a slide in Keynote. I then select it, hit the keyboard shortcut I set up, and I end up with nicely syntax-highlighted code on my slide. I can make manual tweaks to the code in place, and I can also use Cmd+ and Cmd- to bump the font size up and down as needed. If I make more significant changes, I can repeat the process to re-highlight it.

Thanks to Josh Butts and Lukas Ritz for the very handy gists.

Here’s the Automator script I’m using:

Automator Script
set command to "/usr/local/bin/highlight --syntax ruby --out-format rtf --style edit-emacs --font Inconsolata --font-size 40"
do shell script "/bin/bash -c 'pbpaste | " & command & " | pbcopy'"
tell application "System Events" to tell (process 1 where frontmost is true)
click menu item "Paste" of menu 1 of menu bar item "Edit" of menu bar 1
end tell