.@ Tony Finch – blog


It took me a few false starts to work out how to create arbitrary keyboard shortcuts in Mac OS X 10.6 "Snow Leopard", so with any luck this article will make it easier for other people who want to do something similar...

The way to attach an arbitrary script to a keyboard shortcut is as follows:

I tried doing something similar using the AppleScript Editor and the script menu, but for some reason the script menu is not eligible for keyboard shortcuts.

I wanted shortcuts to move a window into the left or right halves of the screen or maximize it to full screen. This is slightly less straightforward than it should be. See the comments in the following for details:

tell application "Finder"
	set screenBounds to bounds of window of desktop
	set Xmin to item 1 of screenBounds
	set Ymin to item 2 of screenBounds
	set Xmax to item 3 of screenBounds
	set Ymax to item 4 of screenBounds
	-- use the following line to move windows to the right half of the screen
	set Xmin to (Xmin + Xmax) / 2
	-- or this line to move them to the left half
	set Xmin to (Xmin + Xmax) / 2
	-- or leave them out to maximize
	set Xmid to (Xmin + Xmax) / 2
	set Ymid to (Ymin + Ymax) / 2
end tell

-- normally you should be able to just use "name of current application"
-- but when testing this using osascript in Terminal.app you end up
-- trying to move osascript's windows not Terminal.app's, and that fails.
tell application "System Events"
	set myFrontMost to name of first item of (processes whose frontmost is true)
end tell

try
	tell application myFrontMost
		if resizable of front window then
			set bounds of front window to {Xmin, Ymin, Xmax, Ymax}
		else
			-- just move non-resizable windows
			-- weirdly shaped preferences windows are amusing, though :-)
			set winBounds to bounds of front window
			set wXmin to item 1 of winBounds
			set wYmin to item 2 of winBounds
			set wXmax to item 3 of winBounds
			set wYmax to item 4 of winBounds
			set w to wXmax - wXmin
			set h to wYmax - wYmin
			set bounds of front window to {Xmid - w / 2, Ymid - h / 2, Xmid + w / 2, Ymid + h / 2}
		end if
	end tell
on error
	-- the above can fail if the target application is not scriptable enough
	-- one prominent example is Preview.app
	-- to solve that problem we use the GUI scripting functionality
	-- you need to go to System Preferences -> Universal Access
	-- and "enable access for assistive devices"
	tell application "System Events"
		tell process myFrontMost
			set position of front window to {Xmin, Ymin}
			set size of front window to {Xmax - Xmin, Ymax - Ymin}
		end tell
	end tell
end try