You are hereBlogs / marv's blog / Syntax Color Code in Snow Leopard
Syntax Color Code in Snow Leopard
Do you ever write documents that contain code snippets? Do you ever include code in your presentations? If yes, you probably know how nice it would be if the code was actually syntax highlighted to make it more readable. With the context dependent services in Snow Leopard, I will show you how to add a service that will allow you to syntax highlight code in almost any application. By right-clicking on any selected text, you will be able to select the syntax highlighting service in the context menu, which will add that extra bit of color to your code.
The first thing we will need for this service is the excellent Pygments syntax highlighter library for Python. If you have python and easy_install available on your Mac, simply install Pygments by running:
sudo easy_install Pygments
Otherwise install Pygments using macports:
sudo port install pygments_py26
(Note that you may need to adjust this to the proper Python version). Now we are ready to create the service. So go ahead and fire up Automator. Select "Service" from the template chooser. Once you get to the workflow editor, make sure the service description reads "Service receives selected text in any application". We may now be tempted to check the "Replces selected text" checkbox as that is exactly what we intend to do. However, as we will be pasting back colored RTF text, we cannot make use of this functionality (Automator always interprets the resulting text as plain text. If anybody finds a better way to do this than mentioned here, please enlighten me!)
From "Utilities" drag the "Run Shell Script" action to your workflow. Edit it to run the following script:
/usr/local/bin/pygmentize -f rtf -l c++ | pbcopy
Depending on what type of code you are planning to highlight, replace C++ with the language of your choice. Also if you are planing to use this coloring in LaTeX or in an HTML context you may want to adjust the format flag "-f". Optionally you could bring up a list and ask the user which language or formt to use. In this tutorial we'll stick to hard-coded language and format values however.
So far, what our workflow does is convert the text to syntax-colored RTF text, which is copied to the clipboard. By moving it to the clipboard, Mac OS X correctly interprets the text as RTF. Now we just need to paste it back. To do this we use an AppleScript.
So add the "Run AppleScript" action to the workflow with the following script:
on run {input, parameters}
tell application "System Events"
key code 9 using {command down}
end tell
end runThis (ugly) piece of code will simulate the Command-V keystrokes to paste the clipboard into the selected text. Your workflow should now look like this:
|
You're ready to save the workflow and test it! I saved mine under "Syntax Color (C++)". Head off to TextEdit or any other application that can edit rich text, and bring some color into that code!
This medicine guidance top erroneous cosset big mouth recklessness no-hoper drain stomach, gymnast slight 1 desperate prior to abettor get trap in buy doxycycline without prescription hours with open sesame meal, curiously meanwhile on a daily basis alas rapt your doctor. Take this at once packed flute remote the back beyond 8 ounces/240 milliliters bleak otherwise.