Managing window positions with apple script and alfred workflows
Table of Contents
Introduction⌗
Dear reader, I’m about to inform you a simple neat way on managing window positions and size within macOS without “additional” window manager tools. The only prerequisit is that you’ve bought the Alfred.app PowerPack.
Window Positions⌗
Within macOS there are 2 ways a window location is stored. bounds
and pos
+ size
. Not all apps support the pos + size, but for the scripts below it does not really matter.
The position is stored in (X,Y)
coordinates relative to your main screen.
Getting the coordinates⌗
To get either the bounds or pos+size I use the following snippet within Script Editor.app
(ships with macOS).
set theQuery to "Zed"
try
tell application theQuery to get the bounds of the front window
on error errorMessage number errorNumber
tell application "System Events" to tell application process theQuery
set appPos to get position of window 1
set appSize to get size of window 1
end tell
end try
When you run this snippet, you want to have the tab “replies” at the bottom open.
Output pos+size⌗
The size and position will be told by the replies
section as show below. In this case you can ignore the result
end tell
tell application "System Events"
get position of window 1 of application process "Zed"
--> {302, 203}
get size of window 1 of application process "Zed"
--> {2445, 1055}
end tell
Result:
{2445, 1055}
Output bounds⌗
And for bounds I changed theQuery
to Safari
. And the result will shouw the bounds (X,Y and size).
tell application "Safari"
get bounds of window 1
--> {520, 25, 2680, 1800}
end tell
Result:
{520, 25, 2680, 1800}
The script⌗
To tie it all together, and even with Safari Profile support:
on alfred_script(q)
set boundsChrome to {3200, -974, 5360, 457}
set boundsIterm to {0, 25, 3200, 1800}
set boundsMail to {1280, 25, 3200, 1093}
set boundsSafariFoo to {3200, -974, 5360, 457}
set boundsSafariBar to {3200, -974, 5360, 457}
tell application "System Events"
try
set bounds of the front window of application "Google Chrome" to boundsChrome
end try
try
set bounds of the front window of application "iTerm" to boundsIterm
end try
try
set bounds of the front window of application "Mail" to boundsMail
end try
try
tell process "WhatsApp"
set position of window 1 to {-2160, -959}
set size of window 1 to {2160, 963}
end tell
end try
try
tell process "Slack"
set position of window 1 to {-2160, 5}
set size of window 1 to {2160, 1430}
end tell
end try
end tell
tell application "Safari"
try
set bounds of every window whose name begins with "Foo" to boundsSafariFoo
end try
try
set bounds of every window whose name begins with "Bar" to boundsSafariBar
end try
end tell
end alfred_script
As you can see all applications are wrapped around try
’s. This is to keep it fast in case one of the defined apps is not open.
Alfred Workflow⌗
Now you just need to bind this into a Alfred workflow and voila, you can reposition your windows to the exact same place all the time.
Caveats⌗
Not all apps support this. As you can run multiple windows of a app without clear distinction. Orion
or Zed
is a good example in this. So you might need to run the workflow again per “window open” of that app.
But most apps I use are only open with 1 window.