Calling Applescript from Emacs

In this thread concerning adding certain OS X features to Emacs, I learned something I didn’t know. Daniel Colascione points out that Emacs supports invokng Applescript directly. To invoke Applescript with the string SCRIPT you make the call

(do-applescript SCRIPT)

Strictly speaking, the do-applescript call isn’t necessary because you can always shell out to osascript as I did in my jcs-get-link function that gets a link to the current Web page, like so:

(shell-command-to-string
                  "osascript -e 'tell application \"Safari\" to return URL of document 1'")

With do-applescript you can simplify that to:

(do-applescript "tell application \"Safari\" to return URL of document 1")

As far as I can see looking at the code, this does avoid starting a new shell to execute the code. It’s not a huge win but it does tell us how capable Emacs is as well as being a bit faster.

Of course, if your platform isn’t OS X you don’t care but if you have similar needs you should investigate whether or not Emacs already has a way for you to accomplish it without invoking a shell.

This entry was posted in General and tagged . Bookmark the permalink.