Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AppleScript: Center Windows on Mac Mojave
set theApp to "Google Chrome"
set windowWidth to 1300
set windowHeight to 950
tell application "Finder"
set screenResolution to bounds of window of desktop
end tell
set screenWidth to item 3 of screenResolution
set screenHeight to item 4 of screenResolution
tell application "System Events"
set myFrontMost to name of first item of (processes whose frontmost is true)
end tell
tell application "Finder"
set {desktopTop, desktopLeft, desktopRight, desktopBottom} to bounds of desktop
end tell
try
tell application "System Events"
tell process myFrontMost
set {{w, h}} to size of drawer of window 1
end tell
end tell
on error
set {w, h} to {0, 0}
end try
tell application theApp
set yAxis to (screenHeight - windowHeight) / 2 as integer
set xAxis to (screenWidth - windowWidth) / 2 as integer
set the bounds of the first window to {xAxis, yAxis, windowWidth + xAxis, windowHeight + yAxis}
end tell
tell application "System Events"
tell process myFrontMost
try
set {{w, h}} to size of drawer of window 1
on error
set {w, h} to {0, 0}
end try
set position of window 1 to {((screenWidth - windowWidth) / 2), ((screenHeight - windowHeight) / 2.0) - desktopTop}
set size of window 1 to {windowWidth - w, windowHeight}
end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment