Find text in webpage and email notification using applescript/automator

I don't have much experience in automator/applescript.


I need a script that searches a web page for a string of text, then if that text is found, it sends me an email. It should reload the page and search again about every 10 minutes. Ideally, this would all occur in the background, but I can live without that feature.


I found this thread from way back in 2011 which is exactly what I'm after, but I'm thinking something has changed over the years, because attempting to run it results in a syntax error.


Can anyone help with an update or explanation? Here is the original script:

on idle
          set pagURL to "http://page.url.com?whatever"
          if application "Safari" is not running then
                    quit
                    return
          end if
          tell application "Safari"
                    set URL of document 1 of window 1 to "http://your.web.address/"
                    delay 5

-- delay to let page load.  

-- there are more elegant ways of doing this using do javascript, but this will suffice.
                    if source of document 1 of window 1 contains "search text" then
                              my sendAMail()
                    end if
                    return 900 --fifteen minutes
          end tell
end idle

on sendAMail()
          tell application "Mail"
                    set theMess to make new outgoing message at end with properties {sender:"your name", subject:"some subject line", content:"The message you want to send to yourself", visible:true}
                    tell theMess
                               makenewto recipientat end of to recipientswith properties {address:"email@adress.com"}
                    end tell

-- send theMess
          end tell
end sendAMail


When I try to run the script in Automator, I get the following error:


Posted on Jun 23, 2022 6:30 AM

Reply
3 replies

Jun 23, 2022 8:52 AM in response to Jamie Benaud

Some code posted ages ago has concatentated adjacent words due to the hosting software in use at that time. Here is your code again with proper command spacing:


use scripting additions

on idle
          set pagURL to "http://page.url.com?whatever"
          if application "Safari" is not running then
                    quit
                    return
          end if
          tell application "Safari"
                    set URL of document 1 of window 1 to "http://your.web.address/"
                    delay 5

-- delay to let page load.  

-- there are more elegant ways of doing this using do javascript, but this will suffice.
                    if source of document 1 of window 1 contains "search text" then
                              my sendAMail()
                    end if
                    return 900 --fifteen minutes
          end tell
end idle

on sendAMail()
          tell application "Mail"
                    set theMess to make new outgoing message at end with properties {sender:"your name", subject:"some subject line", content:"The message you want to send to yourself", visible:true}
                    tell theMess
                               make new to recipient at end of to recipients with properties {address:"email@adress.com"}
                    end tell

-- send theMess
          end tell
end sendAMail




Jun 27, 2022 6:16 AM in response to VikingOSX

Thanks mate, I'll have a look at it.


I've been dabbling with the original correction you posted, and it seems to be working with a few tweaks, other than it doesn't automatically repeat every 900 seconds for some reason. But I got around that by putting it into automator and adding a pause then loop. Once I'm happy with the testing I'll post back with my full solution so others can use it.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Find text in webpage and email notification using applescript/automator

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