When I wake my laptop with external monitors plugged in, the previous window positions are not restored. This automation will save and restore them.
save_window_positions.js will save positions to a local file.
save_window_positions_and_sleep.js will also save the window positions but also sleep the computer.
set_window_positions.js will restore the saved positions from the same file.
To enable these as commands in MacOS:
- Open Automator app
- Select Quick Action document type
- Under
Workflow receives current, selectno inputat the bottom of the list - On the left where the search icon is with the text
Name, type inRun Javascript - Drag
Run Javascriptto the right and drop where it saysDrag actions or files here to build your workflow. - Select all existing boilerplate code and paste in the script you want to use
- File menu, Save
- Type in the name you want to be displayed under every Application Services menu
- Save
com.user.caffeinate.plistto~/Library/LaunchAgents/com.user.caffeinate.plist - Set a keyboard shortcut using Application Services menu item
Services Settings...
Credit to https://macmost.com/saving-and-recalling-window-positions-with-automator.html for the head start with this. This code differs in that it also matches the window name (for multiple chrome windows for example). It's also less user friendly in that it doesn't prompt for filename to save/read from, etc. Use at your own risk.