Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Need some help with Automator/AppleScript

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

Posted on Aug 8, 2012 9:55 AM

Reply
5 replies

Aug 8, 2012 4:03 PM in response to andrv1126

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.

Aug 8, 2012 4:23 PM in response to andrv1126

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)

repeat

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'

else


-- 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.

Need some help with Automator/AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.