Skip to content

Instantly share code, notes, and snippets.

@andrewle
Created April 19, 2010 06:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewle/370799 to your computer and use it in GitHub Desktop.
Save andrewle/370799 to your computer and use it in GitHub Desktop.
Center the top most window using AppleScript
#!/usr/bin/env bash
# Center the top most window using AppleScript
# I call this up quickly using Quicksilver and run it as a terminal
# script because (surprise!) it's faster than running as native AppleScript
osascript -e "
tell application \"Finder\"
set screenSize to bounds of window of desktop
set screenWidth to item 3 of screenSize
end tell
tell application \"System Events\"
set myFrontMost to name of first item of (processes whose frontmost is true)
end tell
try
tell application myFrontMost
set windowSize to bounds of window 1
set windowXl to item 1 of windowSize
set windowYt to item 2 of windowSize
set windowXr to item 3 of windowSize
set windowYb to item 4 of windowSize
set windowWidth to windowXr - windowXl
set bounds of window 1 to {(screenWidth - windowWidth) / 2.0, 0, (screenWidth + windowWidth) / 2.0, (windowYb - windowYt)}
end tell
end try
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment