This AppleScript is sort of a hack to restore window positions after removing/adding an external display, similar my other script to move all windows to the main screen. You should run it before you disconnect the display, then again when you reconnect it. It’s customized for my environment right now, so you’ll need to look through the code a bit to customize it. I have this saved as an application in my Applications folder for quick launching with Spotlight.
property numFFWindows : 0 property FFPos : {} property FFSize : {} property iTunesPos : {} property iTunesSize : {} property iCalPos : 0 property iCalSize : 0 property AdiumContactsPos : 0 property AdiumContactsSize : 0 property AdiumIMSize : 0 property AdiumIMPos : 0 property OFPos : 0 property OFSize : 0 display dialog "Set Window Position or Save Window Position?" buttons {"Restore", "Save"} default button "Restore" set theResult to result tell application "System Events" if (button returned of theResult is "Restore") then -- Restore Settings if (numFFWindows > 0) then tell process "Firefox" repeat with i from 1 to numFFWindows set position of window i to (item i of FFPos) set size of window i to (item i of FFSize) end repeat end tell end if if (iTunesPos is not {0, 0}) then tell process "iTunes" set position of window 1 to iTunesPos set size of window 1 to iTunesSize end tell end if if (iCalPos is not {0, 0}) then tell process "iCal" set position of window 1 to iCalPos set size of window 1 to iCalSize end tell end if if (OFPos is not {0, 0}) then tell process "OmniFocus" set position of window 1 to OFPos set size of window 1 to OFSize end tell end if if (AdiumContactsPos is not {0, 0}) then tell process "Adium" set position of window "Contacts" to AdiumContactsPos set size of window "Contacts" to AdiumContactsSize repeat with i from 1 to (count windows) if ((window i) is not (window "Contacts")) then set position of window i to AdiumIMPos set size of window i to AdiumIMSize end if end repeat end tell end if else -- Save Settings tell process "Firefox" set numFFWindows to count windows set FFPos to {} set FFSize to {} repeat with i from 1 to numFFWindows set end of FFPos to (position of window i) set end of FFSize to (size of window i) end repeat end tell tell process "iTunes" set iTunesPos to position of window 1 set iTunesSize to size of window 1 end tell tell process "iCal" set iCalPos to position of window 1 set iCalSize to size of window 1 end tell tell process "OmniFocus" set OFPos to position of window 1 set OFSize to size of window 1 end tell tell process "Adium" set AdiumContactsPos to position of window "Contacts" set AdiumContactsSize to size of window "Contacts" set AdiumIMPos to {} set AdiumIMSize to {} repeat with i from 1 to (count windows) if ((window i) is not (window "Contacts")) then set AdiumIMPos to (position of window i) set AdiumIMSize to (size of window i) end if end repeat end tell end if end tell
I couldn’t get this to work man. Any tricks you had to do?
Thanks.
@Justin
No tricks, but it will almost certainly require some modification depending on the applications you use. It would be possible to actually make a list of all applications and windows, and restore them – but it could be ugly, and error prone. I use this as there are a few specific apps and windows I need moved every time.
Works great thanks!
Thanks you so much for this! As a web developer who needs to switch his second monitor back and forth from Mac to PC multiple times a day, this script has cured my biggest frustration. Killer.
Thanks so much, this is a huge help! Mostly b/c I am so anal about my windows all being in a perfect position… I just wish Apple would add a listener into the finder that remembered the position with multiple displays!
This seems like exactly what I need, and it seems to be working for me — mostly. It seems like even if an application is running, it throws an error unless the application is in the current Space. Is there a way to make this Spaces-aware?
I don’t doubt that there is a way to make it spaces-aware, unfortunately I do not use spaces and really lack the time to look in to this. Google around a bit, and maybe check out http://macscripter.net
Thank you so much for this script! It is precisely what I was looking for. I’m even wondering if it could be expanded to loop through all the open windows and persist their position and size.
Thanks!
It may be possible, but I believe it would be quite the task. The current script solves most of the problem, but you do have to manually configure it.
This looks like the treasure I’ve been Googling for for the past hour. Just a couple of questions:
1) Is it Snow Leopard safe?
2) Any idea if it’s possible to save 2 different settings: one for having an external monitor hooked up (with windows positioned on it) and one for just the laptop itself?
Thanks!
1) Yes
2) Yes. What I would recommend doing (although I have not tested it) is to save two copies of the application (copy/paste script in to Applescript Editor, file->save as and selection application). Name them accordingly (Main Display Only, Two Displays). Then run Main Display Only with your external display disconnected, and have it save the positions. Connect the external display, adjust the windows, and run the Two Displays application and have it save. Now when you want to switch just open the appropriate application and have it restore.
Again, because this was custom made for my environment, you will almost certainly need to make some other tweaks. The code is fairly simple though, so adding/removing/adjusting things shouldn’t be too bad. Good luck!
@Jonathan Laliberte
Excellent, thank you much good sir!
I wrote a more generalized version of this script, if it’s useful to anyone:
http://snipt.net/Fotinakis/applescript-to-save-and-restore-window-positions
If you use Quicksilver, then just add a global hotkey that runs the script with either –save or –restore so you can save and restore window positions quickly. Enjoy!
Beautiful! Thank you very much. I easily modified it for the Apps I’m interested in. Even though I’ve never done any AppleScripting. Saved it as an App and it works perfectly. My situation was switching between a 24″ monitor and my laptop display.
Thanks again.
Bob
@Fotinakis How in the world to you make a QuickSilver trigger run a script with the command-line argument flag? I’ve been googling and trying everything I can think of, but I can’t figure out how to make QS run your script with a –save or –restore flag.
@Fotinakis Ugh, I just ended up creating a new bash script for each option flag, and setting my QS hotkeys to each bashscript:
#!/bin/sh
osascript /path/to/windowPositions.compiled.scpt –restore
Hello! how can i save the positions to a txt files, to load and restore them after a reboot?
Thanks
Hi Michele. I don’t actually use this script anymore, but there are some great applications that can handle this even better. Check out Moom or Stay