lauantai 6. joulukuuta 2014

Create Windows 7 snapping feature with AppleScript

One very handy feature in Windows 7 is the ability to snap application windows left and right side. This is not possible in OSX, so I wrote a script for it. But unfortunately it's only for keyboard, so no mouse dragging.

I've put a step by step tutorial below but here's the essential script:

set screenWidth to word 12 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width") as number
set screenHeight to word 12 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Height") as number
set positionLeft to 0
set positionRight to (screenWidth / 2)
tell application "System Events"
set frontAppName to name of first application process whose frontmost is true
tell process frontAppName to tell (1st window whose value of attribute "AXMain" is true) to set windowTitle to value of attribute "AXTitle"
end tell
tell application "System Events" to tell application process frontAppName to tell window windowTitle
set position to {positionLeft, 20}
set size to {screenWidth / 2, screenHeight}
end tell

Step by step

Start Automator and open a new Service.


Find Run AppleScript action from Library and drag it to your Service.


Set "no input" to what the Service receives. Copy the script from above into the Run AppleScript action. This script is moving window to left, but you can make another Service to move it to right. Just change the lower {positionLeft, 20} to {positionRight, 20}.


Save the Service as "set window to left"


From System Preferences -> Keyboard, set a keyboard shortcut to your new Service.


Now launch for example Safari and try your script with either your shortcut or choosing your Service with mouse.


You'll be prompted a security note. Choose "Open System Preferences".


Give Accessibility access to Safari.


Now try again the Service. It should work.

Ei kommentteja:

Lähetä kommentti