Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,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 2.38 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (8 votes cast)
 
[17,584 views]  

An AppleScript to check complete page loading in Safari | 14 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 whoever posted them. This site is not responsible for what they say.
An AppleScript to check complete page loading in Safari
Authored by: benleivian on Oct 17, '07 10:08:12AM

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 Oct 18, '07 12:37:08PM

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 Oct 18, '07 09:14:52PM

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 Nov 08, '07 01:16:38AM

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 Jun 09, '09 10:37:27AM

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 Jun 09, '09 11:30:13AM
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 Jun 09, '09 11:31:35AM
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 Jun 09, '09 11:33:20AM
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 Jan 27, '10 04:08:44PM

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 Jan 27, '10 04:12:37PM

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 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 | # ]
An AppleScript to check complete page loading in Safari
Authored by: cerniuk on Feb 13, '10 02:13:40PM

Working in Safari 4.0.4 nicely. A slight reorg:

on SafariWindowIsLoading(inWindowIndex)

tell application "System Events" to ¬
tell application process "Safari"

set theStatusText to name of static text 1 of group 1 of window inWindowIndex as text

if theStatusText begins with "Contacting" or ¬
theStatusText begins with "Loading" or ¬
theStatusText begins with "Waiting" then
set theReturnValue to true
else
set theReturnValue to false
end if

end tell

return theReturnValue
end SafariWindowIsLoading


but for some reason I cannot pass the Window in by ID and then get the property of Window id inWindowID



[ Reply to This | # ]
An AppleScript to check complete page loading in Safari
Authored by: cerniuk on Feb 13, '10 02:37:55PM

using the index of the window (aka 1, 2, 3) works but simply getting the name of the window and using the name form:

set theWindowName to the name of window 1
set theStatusText to name of static text 1 of group 1 of window theWindowName as text



[ Reply to This | # ]
An AppleScript to check complete page loading in Safari
Authored by: SantaCruzJimbo on Jul 18, '11 02:09:47AM
In Safari 5.1 (as shipped with OS X Lion) the status item moved groups... So references to
static text 1 of group 1
should be replaced with
static text 1 of group 2
And your script will start working again.

[ Reply to This | # ]