David Findlay

a man, a plan, a cake: nirvana

Accessing the OS X Services Menu programmatically

Work has been exceedingly busy lately, but I have found some time to get most of the way through Aaron Hillegass’s excellent 3rd edition of Cocoa Programming for Mac OS X.

The book is well written and well organized. I’m an experienced non-Apple programmer, but this was my first foray into Objective C and Cocoa, and my first time using Xcode. As a C++ guy, I can’t always bring myself to like the way Objective C goes about things, but there is a certain elegance in its simplicity. I’m also impressed with Xcode and with Cocoa. I’ve never done much in the way of visual programming, but I am obliged to use Visual Studio for work, and on occasion (I don’t know MFC, really, I don’t) I’ve done some minor GUI stuff with it and always found it most distasteful. Cocoa and Xcode just blow it away: proper separation of model, view and controller!

Falling back to my non-visual roots, I thought it would be interesting to see what it would take to make a command-line utility that would open up a list of files in Aquamacs Emacs (like mate does for TextMate). (Aquamacs Emacs will accept multiple files via open -a Aquamacs\ Emacs, and I have an alias set up to do that, so I don’t actually need this utility, but it fit the bill for a good, small, learning task.)

I struggled a bit with path manipulation - there has to be a better way to convert from relative to absolute than I came up with - and I only made it open one file, although it would be trivial to add a loop (multiple service calls - it won’t take multiple files in one) to cope with that. I also had to do a bit of googling to figure out the incantantion to get my file onto the pasteboard properly.

So, it’s certainly sub-optimal, but it does at least work. Here’s the code. It just takes the (first, absent the loop) file name from the command line arguments, places it on the appropriate pasteboard, then invokes Services/Aquamacs Emacs/Open Selected File.