Simple Applescript to Focus and Active MacVIM

Jun 9 2014

I use certain apps far more frequently than others, and I've been setting up specific keybindings for those apps. One app that I use all the time is MacVIM. But MacVIM doesn't quite behave like normal OSX apps. Specifically, since it doesn't show up as an "Application," I can't access it using the normal keybinding tools.

The keybinding I wanted to create raises and focuses the most recently used MacVIM window. So if I have several of them open, the last one I use should be the one that focuses.

I spent a few hours searching the web and trying various elaborate methods before distilling my meager Applescript knowledge down to this simple script:

tell application "MacVim"
    activate
    set visible of first window to true
end tell

This "activates" MacVIM, making it the currently running app, and then finds the first window in the MacVIM window list and makes it visible. This combination will take care of raising the window if it's minimized, and also focusing the window when it raises. No need for explicit raise requests.

Similar sequences will also work for iTerm2 and other hackery tools.

From there, it's easy to map the script to a keyboard shortcut using the System keyboard shortcuts tool or something else (like Better Touch Tool).



comments powered by Disqus