5 Replies Latest reply: Aug 9, 2012 2:27 AM by andrv1126
andrv1126 Level 1 (0 points)

Hi guys, I'm trying to crate an automated process that will refresh a page that I've left open in a Safari window, search for a piece of text and, if said piece is not found, send me an e-mail (or better yet - iMessage). Basically, I want it to keep refreshing a page until it's contents change and then notify me.


To get to the page I want to monitor, however, you need to go through a login (including captcha), so making Safari go to a certain URL won't work - I need it to refresh a page I've left open for it. Oh, and the page triggers an "Are you sure you want to send a form again?" dialogue if refreshed - the script should be able to confirm that (so it can keep refrshing).


Also, it would be great if it's possible to make it run in the background, so I can use the computer while it's running.


Any help will be very appreciated

MacBook Pro, OS X Mountain Lion
  • Camelot Level 8 (46,665 points)

    Do you want/need the script to reload the current page? or load it from scratch?


    Your post states 'refresh a page that I've left open', which implies you've already passed the login/captcha process. Does reloading the page require another login/captcha or will the existing login credentials still work?


    That's important because it changes the script from trivial (already logged in) to almost impossible (process the login/captcha form).


    So if it's just a matter of reloading the current page, that's easy:


    tell application "Safari"

              do JavaScript "window.location.reload()" in tab 1 of window 1

    end tell


    You might need to better reference which tab/window you want to reload because 'tab 1 of window 1' will always point to the front document, even if that's not the one you want to reload (e.g. you've opened another browser window). It's possible to get around that, but before going down that path it would be useful to know if that's going to meet your needs, or whether you do need to re-login or pass the captcha.

  • andrv1126 Level 1 (0 points)

    No, no need to re-enter login and captcha, they still work on every next refresh

  • Camelot Level 8 (46,665 points)

    Then this should get you started. It grabs the URL of the current window and then repeatedly reloads it, looking for the specified keyword. You'll need to determine the actions to take when/if the keyword exists or doesn't.


    set keyword to "blah"

    tell application "Safari"

      -- get the current window

              set myWindow to (get current tab of window 1)


                        do JavaScript "window.location.reload()" in myWindow


      -- wait for the page to load

                        repeat until ((do JavaScript "document.readyState;" in myWindow) is equal to "complete")

                                  delay 0.5

                        end repeat


                        set pageContent to text of myWindow

                        if pageContent contains keyword then

      -- code here for 'situation normal'


      -- code here for when text is missing

                        end if

      delay 2 -- wait a bit before running again

              end repeat

    end tell

    Since it stores a reference to the window you're free to do other things (open new windows, switch apps, etc.) as long as you don't close this window.

  • andrv1126 Level 1 (0 points)

    Thanks for your help - I think I managed to get things going, however, this doesn't circumvent the "Are you sure you want to send a form again?" dialogue problem (I guess it just kinda skips it), so the web page doesn't refresh successfully.


    Is it possible to somehow get around this?

  • andrv1126 Level 1 (0 points)

    Okay, I just discovered that Opera doesn't have the resend form dialogue that pops up on Safari. However, the script for refreshing the page doesn't work on Opera... Anyone know how to autorefresh Opera via AppleScript?