Hi there,
I’m pretty new to the whole scripting business and I’ve never learned any programming before so I’m having some problems writing a script I really need. This script needs to open up a Safari web page and look for a specific word in it. If it’s not there, it reloads the page. If it’s there, the script says “word found” and stops. Here’s what I have so far:
tell application "System Events"
tell application "Safari" to activate
open location "URL"
delay 5
tell process "Safari" to keystroke "f" using command down
keystroke "word to be found"
(*try
tell process "Safari" to keystroke return
if not found
tell process "Safari" to keystroke "r" using command down
delay 5
end if*)
say "word found" using "Fred"
end tell
Obviously the “if not found” isn’t right but I have no clue how to work from there, and I’m not even sure it’s the right way to look at my problem. Can you guys please help me? It would be very appreciated!
AppleScript: 2.2
Browser: Safari 525.13
Operating System: Mac OS X (10.5)
Hi,
two suggestions:
Both of the following examples use the home page of macscripter and search for the word “Freelancer”.
The scripts search every 10 seconds for the word and exit the repeat loop if the word has been found.
If the site contains no php or other script contents and is not password protected, you don’t need any browser.
curl can do the job
property theURL : "http://macscripter.net"
property searchTerm : "MacFreelancer"
property delayValue : 10
repeat
try
do shell script "/usr/bin/curl -sL --connect-timeout 25 -m 120 " & theURL & " | /usr/bin/grep " & searchTerm
exit repeat
end try
delay delayValue
end repeat
if the curl version doesn’t work, try
property theURL : "http://macscripter.net"
property searchTerm : "MacFreelancer"
property delayValue : 10
tell application "Safari"
repeat
open location theURL
delay 2
set flag to false
repeat 20 times
if (do JavaScript "document.readyState" in document 1) is "complete" then
set flag to true
exit repeat
end if
delay 1
end repeat
if flag and text of document 1 contains searchTerm then exit repeat
delay delayValue
set URL of document 1 to theURL
end repeat
end tell
Note: The scripts have only a minor error handling, so under certain circumstances you run into a infinite loop
Thank you very much. The first one didn’t work but the second one is doing exactly what I want!