I have a program that downloads the PDFs provided from a webpage from my university (for copying automatically to my Kindle). I've been using the delay command to make it wait for the download and has worked fine, but I've always wanted a script function that would loop until Safari's front page stopped loading. This is what I have been using:
on waitforload()
tell application "Safari"
delay 1
repeat
set thisDoc to front document
if (do _javascript_ "document.readyState" in thisDoc) is "complete" then exit repeat
delay 2 -- wait a second before checking again
end repeat
delay 1
end tell
end waitforload
This only works for the loading of the HTML/CSS/DOM side, but doesn't reflect the fact that, in some cases (including mine) the document.readyState shows "complete" even though the document itself actually downloading a PDF.
I was wondering if anyone has found a surefire way to write this function that works in every case conceivable? (For Safari 4 at least) Maybe file a bug with Apple, saying scripters ought to be able to detect this with a call to, say, ready state of document?