Syntax Highlighting

Whenever I need to insert snippets of code into a Keynote presentation, I use a service built from these two shell script snippets. Just paste them in to two Run Shell Script actions set to handle input from stdin, make sure the second is set to run Python, and save the result as a service:

/usr/local/bin/pygmentize -l python -f rtf -O "style=pastie,fontface=Menlo" | sed "s/\\\\f0/\\\\f0\\\\fs72/g"| pbcopy

The above just takes the text input to the script, runs it through Pygments (which, obviously, you need to have installed) and sets the font size to something big enough to be visible in the slides (adjust the sed invocation to taste).

The following Python snippet then issues a system notification popup to let the user know the service worked:

import Foundation, objc
import AppKit
import sys

NSUserNotification = objc.lookUpClass('NSUserNotification')
NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')

def notify(title, subtitle, info_text, delay=0, sound=False, userInfo={}):
    notification = NSUserNotification.alloc().init()
    if sound:

notify("Clipboard", "Syntax Highlighter", "The text has been placed in your clipboard")

I’d love to have Automator display system notifications on its own (it would be terrific1), but this is a useful enough way to do it without having to install any plugins.

  1. Sadly, at the time of this writing, it seems that Mavericks is still missing that feature. ↩︎