"code to check to see if webpage is loaded" isn't working

I’m trying to get this to work and it’s not happy with me. This is what I’m trying to adapt it to. And just to make sure, though it is in the script, the page in question is www.allmusic.com. It’s a great resource, but the site can take awhile to load yet other times its really quick.

Anyhoo, mucho thanks!


set timeout_value to 20
set page_loaded to open location "http://www.allmusic.com"

tell application "Safari"
	activate
	tell application "System Events"
		tell process "Safari"
			run page_loaded
		end tell
	end tell
end tell

on page_loaded(timeout_value) -- in seconds
	delay 2
	repeat with i from 1 to timeout_value
		tell application "Safari"
			
			if (do JavaScript "document.readyState" in document 1) is "complete" then
				return true
			else if i is timeout_value then
				return false
			else
				delay 1
			end if
		end tell
	end repeat
	return false
end page_loaded

it originally came from here:
http://bbs.applescript.net/viewtopic.php?id=19262

this is the original code:

on page_loaded(timeout_value) -- in seconds
    delay 2
    repeat with i from 1 to timeout_value
        tell application "Safari"
            if (do JavaScript "document.readyState" in document 1) is "complete" then
                return true
            else if i is timeout_value then
                return false
            else
                delay 1
            end if
        end tell
    end repeat
    return false
end page_loaded

there’s also this one but it didn’t seem to work either since the page didn’t load and it was done…

this one came from here:
http://bbs.applescript.net/viewtopic.php?id=17098


tell application "Safari"
   activate
   open location "http://macscripter.net/"
   delay 2
   set the_state to missing value
   repeat until the_state is "complete"
       set the_state to (do JavaScript "document.readyState" in document 1)
   end repeat
end tell
beep 3

Hi,

if you’re using Safari 3, try this:


tell application "Safari" to open location "http://www.allmusic.com"
if page_loaded(20) then
	say "done"
else
	say "failed"
end if

on page_loaded(timeout_value) -- in seconds
	delay 2
	repeat with i from 1 to timeout_value
		tell application "Safari"
			if name of current tab of window 1 is not "Loading" then exit repeat
		end tell
		delay 1
	end repeat
	if i is timeout_value then return false
	tell application "Safari"
		repeat until (do JavaScript "document.readyState" in document 1) is "complete"
			delay 0.5
		end repeat
	end tell
	return true
end page_loaded