Restore Previous Display Window Positions AppleScript

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

‘batch’ broken in Mac OS 10.5 (Leopard) – SOLVED

Give it a try and let me know if you agree… Quick test:
$ batch now
> ls
[control-d]
job 1 at Wed May 14 14:02:27 2008

$ atq
778240 Fri Jan 18 09:03:00 2002

———-

$ batch 17:00
> ls
[control-d]
job 5 at Wed May 14 17:00:00 2008
$ atq
778240 Sat Jul 17 12:39:44 1993

Update: 10.5.3
This doesn’t seem to happen anymore in 10.5.3, but it doesn’t quite seem to work either:

$ at 3:00
> touch /TEST.TEST
job 10 at Sun Jun 1 03:00:00 2008

This was ran at 2:59:30(or so) and as of 3:45:
$ atq
10 Sun Jun 1 03:00:00 2008

Yet, the command did not execute.

Update 9/21/09:

I don’t know how I missed this before, but in the man page for at on OS X, there is the following comment:

Note that at is implemented through the launchd(8) daemon periodically
invoking atrun(8), which is disabled by default.  See atrun(8)
for information about enabling atrun.

Going over to the atrun page shows that you can enable a launch daemon to periodically run queued at and batch jobs. The executiable that does this should be run with root permissions and is located at /usr/libexec/atrun