Syntax Highlighting

Whenever I need to insert snippets of code into a 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 , 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 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()
    notification.setTitle_(title)
    notification.setSubtitle_(subtitle)
    notification.setInformativeText_(info_text)
    notification.setUserInfo_(userInfo)
    if sound:
        notification.setSoundName_("NSUserNotificationDefaultSoundName")
    notification.setDeliveryDate_(Foundation.NSDate.dateWithTimeInterval_sinceDate_(delay, Foundation.NSDate.date()))
    NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)

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

I’d love to have 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. ↩︎