Created
November 2, 2016 19:01
-
-
Save ibuys/04ac3495b82814c80da9910fa1e0d532 to your computer and use it in GitHub Desktop.
Applescript to Center a Window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tell application "Finder" | |
set screenSize to bounds of window of desktop | |
set screenWidth to item 3 of screenSize | |
set screenHeight to item 4 of screenSize | |
end tell | |
property WP : 0.1 -- prop of w free screen we want | |
property HP : 0.02 -- prop of w free screen we want | |
tell application "System Events" | |
-- set screenHeight to (screenHeight - dockHeight) | |
set myFrontMost to name of first item of ¬ | |
(processes whose frontmost is true) | |
end tell | |
if myFrontMost is "Sublime Text" then | |
tell application "System Events" to tell application process myFrontMost | |
set stprops to get the properties of window 1 | |
set windowXl to item 1 of (position of stprops) | |
set windowYt to item 2 of (position of stprops) | |
set windowWidth to item 1 of (size of stprops) | |
set windowHeight to item 2 of (size of stprops) | |
end tell | |
else | |
tell application myFrontMost | |
set windowSize to bounds of window 1 | |
end tell | |
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 windowHeight to windowYb - windowYt | |
end if | |
if myFrontMost is "Sublime Text" then | |
tell application "System Events" to tell application process myFrontMost | |
set position of window 1 to {¬ | |
round (screenWidth * WP), ¬ | |
round (screenHeight * HP) ¬ | |
} | |
set size of window 1 to {¬ | |
round (screenWidth * (1 - 2 * WP)), ¬ | |
round (screenHeight * (1 - 2 * HP)) ¬ | |
} | |
end tell | |
else | |
tell application myFrontMost | |
set bounds of window 1 to {¬ | |
round ((screenWidth - windowWidth) / 2) rounding as taught in school, ¬ | |
round ((screenHeight - windowHeight) / 2) rounding as taught in school, ¬ | |
round ((screenWidth + windowWidth) / 2) rounding as taught in school, ¬ | |
round ((screenHeight + windowHeight) / 2) rounding as taught in school ¬ | |
} | |
end tell | |
end if |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found this script somewhere, modified it to get the screen size without calling out to a command line tool.