Raise a Single Window by Title on Mac OS-X


Posted: 08 Jun 2013

Tagged: AppleScript Objective-C Snippet

##Update: Not three full days after posting this, come to find it does not work on OS-X Maverick beta. See the bottom for the updated solution.

Ever since switching off of a dedicated application for my Gmail access, no more Mailplane, the one thing I have missed most is the ability to use a keyboard shortcut to raise Gmail to the front. It’s an integral part of my workflow to pull my most used items up quickly, and banish them as quickly, especially in Mac’s limited spaces setup. The solution seemed simple, just write a script to find the window and raise it. How incredibly wrong I was on that point. This post will list out what I tried and what finally worked in hopes of saving others some pain in the future.

Being relatively new to AppleScript, my initial tries were relatively rudimentary. The script would run through the windows of the application hosting Gmail, in this case Google Chrome, looking for the title, and activating the window.

Of course, this works, but it pulls all of chrome to the front instead of just Gmail. Not going to work. Ok, I search high and low for anything that would let me raise a window by itself. Having found a StackOverflow article on iterating over windows, I once again felt pretty confident something would work. Add on an application name check and some assistive devices glue to click the window up, should be good to go.

Better, it only manages one window, but now even though the window moves to the front of other chrome windows, it doesn’t get focus! Now seriously starting to doubt my own sanity, and that of the Apple developers responsible for this, I add in some activates to bring the application to the front.

And now I’m back to the same place as the much simpler first script. Crap.

Digging for far longer than I would like to admit, I find this forum post on cocoabuilder in which someone is complaining that -[NSWorkspace launchApplication:] only raises the frontmost window, interesting… So, how does one access Cocoa from AppleScript? Surely that must be possible? Enter rabbit hole number two.

First I find call methods through this post, then find out that they don’t exist anymore, great. Finally end up here, where I find out about AppleScriptObjC! Perhaps this will finally work the way I want? With a few more modifications to the last script, adding access to the NSWorkspace class and using its launchApplication: method to raise the window, we get this.

Finally something works. Of course, as with every other step of this process, it’s not quite perfect. The reason? This type of script cannot be run as an applescript. It must be compiled to an application and run that way. Fine, it’s good enough for now. If you want to test it, in AppleScript editor use the “New from Template -> Cocoa AppleScript Applet” menu item to create the proper kind of file and paste the code in. It should work by running an application created from the script. This has been bugging me for a long time, so I’m glad it’s finally working, but if anyone from Apple reads this, please make it more reasonable to do things on a per window basis!

##Update: Now with OS X Mavericks, something seems to have changed. The same technique continues to work, but the “click” tends to raise an error, preventing the launch from getting run. Additionally the original would continue and raise any other windows that matched the pattern because the “tell me to quit” action is not immediate. Further, adding “return” immediately after it not only doesn’t fix the problem, it cancels the quit command. The final version that I’m using to raise my GMail window in Alfred right now follows.

blog comments powered by Disqus