Saturday, April 7, 2007

Vim and Applscripting fun

I love vi.

Sometimes I like my vi to be a bit more 'graphical'.

I'm not alone and so some nice people created vim for mac that actually uses Aqua (why the default vim installed with mac OSX doesn't include this is a mystery to me.)

I downloaded and installed and it basically just works except for one annoying little thing:

It always brings up the window in the upper left hand corner.

Not a big deal except I like having my dock on the left and unlike most mac apps mac-vim doesn't seem to obey some window placement rules and so shows up with its left-hand side under the dock (which is annoying).

So I decided a little applescripting was in order to fix this little annoyance.

First off I've got to say it is a bit of a pain in the butt to find what commands an app will respond to. This seems such a basic thing that I'm shocked that there isn't some great big site on the net which has this info. (there is a script-editor File->Open Dictionary menu item which is handy but only seems to work on those apps that are nice enough to include a dictionary (which doesn't seem to include mac-vim))

After a bit of searching I discovered this little gem : UI Element Inspector here

Then it became obvious to me that there is a catch-all application called "System Events", and hey what-do-you-know it is kind enough to have a dictionary in Script Editor.

Using a bit of googling of 'system events ui element inspector' turned up this page.

Which helped me craft this bit of magic:

tell application "System Events"
tell process "Vim"
set position of front window to {160, 150}
end tell
end tell

The important gotcha here is that one has to turn on the 'accessibility framework' under 'System Preferences' -> 'Universal Access' -> 'Enable access for assistive devices' (I have no clue as to why this isn't on by default).

Using the nifty osascript command which I discovered in this book I can then:

osascript -e 'Tell app "System Events" to tell process "Vim" to set position of front window to {160,150}'

Unfortuantely the window has to come up before I can tell 'System Events' to move it.

So the final solution is a bit ugly in that I have to run a bash script to fork off the above command which takes just enough time for mac-vim to come up so it can be repositioned. Maybe there is some sort of 'waitfor' command in applescipt but my solution works 'good enough' for the moment. :)


#!/bin/bash &

/Applications/ -g $@


osascript -e 'Tell app "System Events" to tell process "Vim" to set position of front window to {160,150}'

No comments: