Over the last month I’ve been teaching myself applescript and I found this website very helpful so I wanted to give something back.
This script is a subroutine you can call to pause your script until a web page in Safari has fully loaded. The script will watch the page for a predetermined amount of time. Once the web page fully loads the script will exit. If the web page hasn’t fully loaded in the predetermined time then the script will stop the page from loading and try to reload the page. It will go through this watch/wait/stop/reload process 3 times until the script will tell you that the web page couldn’t load.
As such, there’s 2 variables at the top of the script which you can customize. 1) “theDelay” which is the amount of time in seconds the script will watch for the web page to load until the stop/reload cycle starts. 2) “numTries” which is the number of times the stop/reload cycles before the script gives up.
This script works because I noticed that when a web page is loading Safari names the window with the word “Loading” while the web page is loading. Once a web page has loaded the word “Loading” goes away. So the script looks for this word and waits until it goes away.
on web_page_loading()
set theDelay to 10 -- the time in seconds the script will wait to let a web page load
set numTries to 3 -- the number of stop/reload cycles before giving up
set myCounter to 0
set finished to false
repeat until finished is true
set myCounter to myCounter + 1
set my_delay to 0.25
set startTime to current date
set endTime to startTime + theDelay
set web_page_is_loaded to false
delay my_delay
tell application "Safari"
activate
repeat until web_page_is_loaded is true
if endTime is greater than (current date) then -- only wait theDelay seconds for a web page to load otherwise stop the page loading and try to reload again
if name of window 1 contains "Loading" or name of window 1 contains "Untitled" or name of window 1 contains "Failed to open page" then
delay my_delay
else
set web_page_is_loaded to true
set finished to true
delay my_delay
end if
else
tell application "System Events"
tell process "Safari"
if myCounter is numTries then -- give up if the page doesn't load after numTries stop/reload cycles
keystroke "." using command down -- stop the page
delay my_delay
tell application "Finder"
activate
display dialog "The web page will not load!"
end tell
set connected to true -- a global variable which will stop this script if there's a problem
set web_page_is_loaded to true
set finished to true
tell application "Safari" to activate
else
keystroke "." using command down -- stop the page
delay my_delay
keystroke "r" using command down -- reload the page
delay my_delay
set web_page_is_loaded to true
end if
end tell
end tell
end if
end repeat
end tell
end repeat
end web_page_loading
Model: PM dual 2.0 GHz G5
AppleScript: AppleScript 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)