Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'An AppleScript to check complete page loading in Safari' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to check complete page loading in Safari
Authored by: benbowler on Jan 27, '10 04:15:57PM

Ah but this still doesn't work;

SafariLoad("http://www.macintouch.com/")

on SafariLoad(theURL)

-- this routine attempts to load a Safari page to completion, and returns the final title
local theVersion, thedocument, thewindow, theSource, thetab, theTitle

-- check to see if we should open a new URL or just wait for the existing window to load
tell application "Safari"
set theVersion to version
set theVersion to (character 1 of theVersion) as number

if theURL is "" then
set thedocument to (make new document) as reference
set URL of thedocument to theURL
end if

--check to see if page has finished loading
repeat
-- let the script breathe for a second
delay 1.0

-- Safari version 3 and version 4 have different techniques for determining load completion
if theVersion = 3 then
if (name of thedocument as text) ¬
begins with "Contacting" or (name of thedocument as text) begins with "Loading" or (name of thedocument as text) begins with "Waiting" then
else
exit repeat
end if
end if

-- Safari version 4 no longer updates the page title based on progress
-- this cheap and dirty technique below *requires* that the target URL contains a closing HTML tag
-- this is highly unreliable for accessing pages that you do not control
-- however, this completion check is technically accurate from an HTML standpoint, even if not from that of HTTP
if theVersion = 4 then
set thewindow to front window
set thetab to current tab of thewindow
set theSource to source of thetab
-- if the page has not started loading, safari will error out when accessing the page source
-- allow for that error and simply retry
try
if (theSource contains "</html>") then
exit repeat
end if
end try
end if

end repeat

end tell

copy (name of document 1) to theTitle
return theTitle

end SafariLoad

(Sorry about comment overload today!)



[ Reply to This | # ]