Print AppleScript files with color-coding in the Terminal

Filed under Mac, Programming, Scripts

asprint image Even though I curse and hate its syntax, I have to admit that AppleScript certainly provides one of the nicest things OS X has to offer in comparison to other operating systems: almost-ubiquitous scripting of GUI applications (one could argue that this is not due to the AppleScript language itself, but the Open Scripting Architecture which AppleScript is simply a language for, but that's just semantics). Recently I've had to work with AppleScript files a bit more than usual while implementing Tagger's front app scripts feature, and I noticed that I often wanted to print out the contents of (compiled) AppleScript files in the Terminal. The built-in command osadecompile does just that: it reads in the AppleScript file, decompiles it, formats the code and nicely prints it out. I'm used to seeing my code with syntax highlighting, though, so I decided to write a small program that works similarly to osadecompile but uses ANSI escape sequences to format the output.

The Application Kit additions to the NSAppleScript class provide an implementation of the syntax highlighting (which would be the hardest part in implementing something like this) and I already have the ANSIEscapeHelper class that provides the translation of an NSAttributedString to an ANSI-escaped NSString (which would be the second-hardest part in implementing something like this) so writing a command-line program around these two parts was quite simple in the end. The program is called asprint and it has its own page on this site where you can download it.

This is what using asprint looks like:

asprint screenshot


Seth Milliken March 4, 2010 at 1:03 AM

FWIW, AutoHotKey is arguably the Windows equivalent of AppleScript, complete with comparably egregious syntax but even greater ubiquity. The accessibility frameworks have given AppleScript more range, but they’re still quite awkward to use. With AutoHotKey, you can script pretty much any UI element you can see; but then you’d be using Windows.

Ali Rantakari March 4, 2010 at 1:18 AM

Hi Seth, and thanks for letting me know about that. If I ever need a GUI macro program for Windows I’ll be sure to check it out. I should mention that AppleScript is not just a language for implementing GUI macros, though; its mainly useful because of the AppleScript dictionaries most applications provide to expose some of their internal functionality. For example, take look at the script in the screenshot above and think about how difficult that would be to implement if the only thing you had access to was GUI elements (and how susceptible it would be to breaking whenever the GUI elements would move or otherwise change).

Seth Milliken March 4, 2010 at 2:34 AM

Oh, yeah, AppleScript can definitely offer a great deal more scripting depth. I was just addressing the GUI-scripting aspect. It would be great if Apple did more to leverage the OSA by providing additional fully supported and documented languages. Third-party bridges like py-appscript and rb-appscript are pretty cool and do give you access to greater functionality, but they don’t really make the lines of AppleScript they replace look significantly cleaner.

You might enjoy this paper on AppleScript, written by one of its original developers:

Seth Milliken March 4, 2010 at 2:38 AM

Looks like I didn’t get that link right: http://www.cs.utexas.edu/~wcook/Drafts/2006/ashopl.pdf