Skip to content

Instantly share code, notes, and snippets.

@henckellbach
Last active January 18, 2021 11:47
Show Gist options
  • Select an option

  • Save henckellbach/a95153da6390b7f4e285958a743df9ad to your computer and use it in GitHub Desktop.

Select an option

Save henckellbach/a95153da6390b7f4e285958a743df9ad to your computer and use it in GitHub Desktop.
AppleScript application for organizing your open windows
-- You need to allow assistive access to this application
-- System Preferences > Security > Privacy > Privacy > Accessibility
-- AppleScript does not allow optional parameters, therefore we are using a list of parameters
-- @param {string} processName - Name of the application process
-- @param {int} x - Window X position
-- @param {int} y - Window Y position
-- @param {int} w - Window width
-- @param {int} h - Window height
-- @param {int} xOffsetDirection - Additional windows offset direction (-1 for left, 1 for right)
on resizeApp(params)
set processName to item 1 of params
set x to item 2 of params
set y to item 3 of params
set w to item 4 of params
set h to item 5 of params
-- Effectively make the last item optional
if (count params) = 6 then
set xOffsetDirection to item 6 of params
else
set xOffsetDirection to 1
end if
tell application "System Events"
tell process processName
set windowList to every window
set i to 0
repeat with currentWindow in windowList
try
set xOffset to (i * 40 * xOffsetDirection)
set position of currentWindow to {x + xOffset, y}
set size of currentWindow to {w, h}
set i to (i + 1)
on error errormsg
display dialog "An error has occured.
" & errormsg with title "Error" buttons {"OK"}
end try
end repeat
end tell
end tell
end resizeApp
-- If you rock a multi-monitor setup, you might need to use negative x and y positions to move windows to secondary screens
on run {input, parameters}
resizeApp({"Terminal", -1920, 660, 620, 380})
resizeApp({"Fork", -1920, 0, 1056, 659})
resizeApp({"Code", -1520, 0, 1520, 1000, -1})
end run
@henckellbach
Copy link
Author

henckellbach commented Jan 18, 2021

How to Use

  1. Open Automator.
  2. Create a new Application or Quick Action.
    An application will be just a regular application which you can launch from Finder, your desktop or the dock.
    A Quick Action can be launched from the menu bar (under Services) or even from the Touch Bar.
  3. Find the action Run AppleScript and drag it to the empty space on the right.
  4. Copy and paste the contents of this gist.
  5. Update the values inside the run method and use the ▶️ button for testing.
    You might need to play around with the values a little bit, based on your system. Multi-monitor setups might even need negative x values. I recommend updating the arguments one by one going from the left until you are satisfied with the results.
  6. ????
  7. PROFIT!!!

Adding the Quick Action to your Touch Bar

  1. Before saving, select the icon (Image) you would like to use for the Quick Action.
  2. Save the Quick Action.
  3. Open your System Preferences and select Extensions and Touch Bar.
  4. Click Customize Control Strip….
  5. Drag the Quick Action item to the Touch Bar.
  6. You can now find your Quick Action under the Quick Actions icon on your Touch Bar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment