SafariReload

July 30, 2007

Every now and again you run into silly (but useful) sites with silly (and useless) session timeouts with no means of changing them. So you do something else while working with the sites only to come back and find out you’ve been logged out.

Well, here’s a little script to help with this situation:

property reloadInterval : 120
property reloadUrls : {"gsx", "mac.com"}

on idle
    tell application "System Events"
        if (application "Safari" is not frontmost) then
            tell application "Safari"
                repeat with anUrl in reloadUrls
                    repeat with theDoc in (every document whose URL contains anUrl)
                        do JavaScript "window.location = window.location" in theDoc
                    end repeat
                end repeat
            end tell
        end if
        return reloadInterval
    end tell
end idle

Just set your preferred interval in seconds. This hack is particularly nice IMHO in that it even lets you set a list of partial URLs the pages to be reloaded should contain. Also, it checks System Events to make sure we’re not actually in Safari. Just don’t forget to switch to another app for this to work. ;)

Oh, and the on idle stuff only works if you save it as an application and check “Stay Open”.