Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,000 hints and counting!

An AppleScript to check complete page loading in Safari Web Browsers
Here is an AppleScript to check if the foremost window in Safari has finished loading the requested page. Some people have suggested using a do javascript solution way, and I know there are hints like this out there, but this seems to be the only bulletproof way to do things.

How does it work? Well, when you click on a link or request a page in Safari, the Safari status bar will always display the text Contacting... and subsequently Loading.... However, it seems not everyone knows how to access that text within AppleScript. So, here's how I did it:
--check if page has loaded
  repeat
    delay 0.5
    tell application "System Events" to ¬
    tell application process "Safari"
      if (name of static text 1 of group 1 of window 1 as text) ¬
      begins with "Contacting" or (name of static text 1 of group 1 ¬
      of window 1 as text) begins with "Loading" then
      else
        exit repeat
      end if
    end tell
  end repeat
That code will basically run until the page is done loading in Safari -- hooray for infinite loops! You can make it into a subroutine, if you'd like. I'm sure there are other ways to use the status bar information, too.

[robg adds: This worked as described for me...]
    •    
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)
 
[7,193 views]  

An AppleScript to check complete page loading in Safari | 11 comments | Create New Account
Click here to return to the 'An AppleScript to check complete page loading in Safari' hint
The following comments are owned by whomever posted them. This site is not responsible for what they say.
An AppleScript to check complete page loading in Safari
Authored by: benleivian on Wed, Oct 17 2007 at 10:08AM PDT
Nice. I was using the javascript solution before, but I like this one better.

I also added the following in the conditional:
or (name of static text 1 of group 1 of window 1 as text) begins with "Waiting"

[ Reply to This | # ]
An AppleScript to check complete page loading in Safari
Authored by: halfapie on Thu, Oct 18 2007 at 12:37PM PDT
Thanks! I didn't realize that sometimes the page says "waiting"

Also, I should have mentioned that the "repeat" loop can be nested within the "tell safari" statement. It would probably use less resources to have it that way, although it probably doesn't matter that much.

[ Reply to This | # ]
Will this always work?
Authored by: Krioni on Thu, Oct 18 2007 at 9:14PM PDT
I believe I've noticed that, on some pages that have dynamic content, Safari and other browsers never really "finish" loading the page. Those might cause a problem for this type of script.

I've done a lot of work scripting web browsers since 2000 (including scripting IE back in OS 9 when IE was the only browser that you could use AppleScript to send Do Javascript commands . . . blech!). Later on, I graduated to URL Access Scripting Addition. In Mac OS X, URL Access Scripting had features that were completely broken (could not POST forms, for example, even though the commands were there). Switched to cURL, which was so much better.

All that said, if you're trying to script something where you want the user to see what you're doing and perhaps also interact with, scripting a web browser is all you can do. I suppose you could add something that, if it is still checking the status message after a cut-off time, checks the source for "</HTML>" or "</html>" to see if the page's content is all there (in theory). Perhaps even check for that first. It seems to get an error if you ask for source of a not-yet-loaded page - maybe that's something to try.

---
http://www.danshockley.com

[ Reply to This | # ]
An AppleScript to check complete page loading in Safari
Authored by: madom on Thu, Nov 8 2007 at 1:16AM PST
I need this same script for Firefox. Can anybody help?

[ Reply to This | # ]
An AppleScript to check complete page loading in Safari
Authored by: iGods on Tue, Jun 9 2009 at 10:37AM PDT
curses! Safari 4 breaks this technique; it no longer sets the title differently as it loads.

I've scanned the dictionary from here to Sunday, and I can't find a reliable completion parameter... anyone? Bueller? Bueller?

[ Reply to This | # ]
An AppleScript to check complete page loading in Safari
Authored by: iGods on Tue, Jun 9 2009 at 11:30AM PDT
I adjusted the original poster's technique by simply checking for a closing html tag... not exactly bullet proof, but for now this will keep my various browser-based scripts working.

I encourage others out there to come up with a more reliable method than my little hack here.

thanks folks.

-----------

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 ≠ "" 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


[ Reply to This | # ]
An AppleScript to check complete page loading in Safari
Authored by: iGods on Tue, Jun 9 2009 at 11:31AM PDT
I adjusted the original poster's technique by simply checking for a closing html tag... not exactly bullet proof, but for now this will keep my various browser-based scripts working.

I encourage others out there to come up with a more reliable method than my little hack here.

thanks folks.

-----------

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 ≠ "" 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


[ Reply to This | # ]
An AppleScript to check complete page loading in Safari
Authored by: iGods on Tue, Jun 9 2009 at 11:33AM PDT
I adjusted the original poster's technique by simply checking for a closing html tag... not exactly bullet proof, but for now this will keep my various browser-based scripts working.

I encourage others out there to come up with a more reliable method than my little hack here.

thanks folks.

-----------

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 ≠ "" 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


[ Reply to This | # ]
An AppleScript to check complete page loading in Safari
Authored by: benbowler on Wed, Jan 27 2010 at 4:08PM PST
Hey this looks exactly what I need but in trying to implement it I get an error because of these weird characters:

if theURL ≠"" then

What in the world should they be?

[ Reply to This | # ]
An AppleScript to check complete page loading in Safari
Authored by: benbowler on Wed, Jan 27 2010 at 4:12PM PST
Oh apparently those characters should be 'is' and there's also a ; which shouldn't be there.

[ Reply to This | # ]
An AppleScript to check complete page loading in Safari
Authored by: benbowler on Wed, Jan 27 2010 at 4:15PM PST
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 | # ]