Monthly Archives: June 2014

Syntax highlighting for Mac OS X Mail and applications

Code is a lot harder to read without syntax highlighting, and I needed highlighting for emails and presentations. So, I cobbled this solution from what I could find on the net. This OS X service allows me to select and highlight text in many OS X applications. It works for Pages, Keynote, and Apple’s Mail. It does not work, as written, in Microsoft Office, although you can still copy highlighted text from Pages to Word or PowerPoint, or perhaps modify this script to make it work in Office.

You will need two things to get this service working on your computer. First, download and install a command-line based syntax highlighter. Second, you need to set up an Automator workflow, to enable the service itself.

Install a command-line highlighting utility.

I use highlight. A strength of Pygments is that it can auto-detect languages, while highlight does not. But, Pygments often doesn’t guess the correct language if the highlighted code is short, because today’s popular languages use highly similar syntax.

I installed highlight using Homebrew:

$ brew install highlight

To install Pygments:

$ pip install pygments

Create your Automator workflow.

Open Applications > Automator. Choose “Service” as your new workflow type. Choose “Run Applescript” as your one and only action. Above this action, you will see some options. Set this workflow to receive text in any application. Do not check the box that says ‘Output replaces selected text’ because then this script won’t work.

Copy and paste this code.

In your run Applescript box, copy and paste:

-- inspired from http://apple.stackexchange.com/questions/94222/how-to-get-automator-to-treat-text-as-rtf/94246#94246

-- limitation: does not auto-detect syntax (which is iffy with short sections of code, anyway); therefore this action is limited to Python syntax

on run {input, parameters}
    try
        set old to the clipboard as record
    end try

-- the tr function is necessary because some applications copy rtf as OS 9 CRs instead of Unix LFs.

    do shell script "echo '" & input & "' | tr '\\r' '\\n' | /usr/local/bin/highlight --syntax=Python --out-format=rtf  --style=edit-matlab --font=courier | pbcopy"

--do shell script "echo '" & input & "' | tr '\\r' '\\n' | /usr/local/bin/pygmentize -l python -f rtf | pbcopy"

-- copying and pasting via the clipboard necessary because text replacement bypasses the word processor's automatic conversion of RTF

    tell application "System Events" to keystroke "v" using command down
    delay 0.5

    try
        set the clipboard to old
    end try
end run

Save this service.

I use the name “Python Syntax Highlighting” but you can call your service whatever you want. Whenever you select text in any application, your new Service will appear on the menu bar as [Application] > Services > Python Syntax Highlighting [or whatever name you saved it as]

This service works well for programs that support RTF. I use it all the time in Keynote, Pages, and Apple’s Mail. It does not work in Microsoft Office, though, although it may be possible to modify the script above to make it work in Office. Good luck!

Advertisements