I am trying to get Safari to load a page. But every script I try usuall just opens an Untitled document and the Event Log of the script below returns the document as “about:blank”. Anybody know why this is happening?
set delaytime to 0
set theLocation to "http://www.location goes here"
tell application "Safari"
open location theLocation
repeat
if (do JavaScript "document.URL" in document 1) is theLocation then
exit repeat
else
set delaytime to delaytime + 1
delay delaytime
end if
end repeat
end tell
I just checked into it a little more, and I’m not sure why you aren’t able to tell Safari to “open location”. Could your URL be bad? Here’s a script that does what I think you want it to do (note: you were also setting yourself up for an infinite loop, this will protect against that):
set current_delay to 0
set max_delay to 5
set uri to "http://www.macscripter.net/"
tell application "Safari"
open location uri
set current_loc to ""
repeat while ((current_loc does not contain uri) and ¬
(current_delay is less than max_delay))
try
set current_loc to URL of document 1
current_loc -- Just checking if it's null
on error
set current_loc to ""
end try
delay current_delay
set current_delay to current_delay + 1
end repeat
activate me
display dialog "Done. Current Delay: " & current_delay
end tell
I used “does not contain” above because sometimes after you go to a web site, it automatically tags on a file name, so waiting for an ‘equal’ URL could leave you thinking it didn’t work, when in fact it did.
It could be an error in your url, it could be that you’re not activating safari before you send it a request, or it could be that you’re making things more complicated than they need be by using javascript. There’s no reason to use javascript at all for what you’re trying to accomplish. In fact, adding javascript probably just adds more potential for errors and conflicting results.
The following code tries to open the url in the frontmost safari window, and opens a new one if there is no window. It then repeats “tmpSeconds” seconds to check whether the page has been loaded or not, displaying a dialog after the page is loaded or the loop times out.
set theUrl to "http://bbs.applescript.net/"
set tmpSeconds to 5
set tmpMessage to ("Could not load after " & tmpSeconds & " seconds.") as string
set tmpURL to ""
tell application "Safari"
activate
if (count windows) = 0 then
open location theUrl
else
set URL of document 1 of window 1 to theUrl
end if
repeat with tmpCount from 1 to tmpSeconds
try
set tmpURL to (URL of document 1 of window 1) as string
end try
if tmpURL is theUrl then
set my tmpMessage to "The page is loaded"
exit repeat
else
delay 1
end if
end repeat
end tell
activate
display dialog tmpMessage
I wouldn’t use an endless repeat loop as you’ve done, especially when using the internet or other unpredictable environment. If your connection went down or you entered an erroneous URL, you’d be stuck looping forever, or until you killed the process manually. Always use some sort of time or other criteria to make sure your process ends…whether success or failure. Also, the code you provided increments the time that each repeat delays by one second. Every time you loop, it adds a second to how long it waits before it loops again. After 10 loops, it would be waiting 55 seconds before it fired your code again. I think you probably wanted to just return 1 every time in the loop so it waits another second before evaluating the url.