Re: How Can One Tell When Safari Completes?
Re: How Can One Tell When Safari Completes?
- Subject: Re: How Can One Tell When Safari Completes?
- From: Gil Dawson via AppleScript-Users <email@hidden>
- Date: Tue, 11 Feb 2025 09:43:49 -0800
Thanks, Joel--
Thanks for the suggestion.
I tried your code suggestion in several tests. The "log" command was executed
only once each time, suggesting that the "delay" command was never executed.
Then I tried rearranging so that the delay came before the log: still only one
log command per test.
I tried Quitting Safari so as to take much longer. Still only one log command
appeared, but the page never appeared. Evidently "open location" creates an
invisible window, so I added an "activate".
This, plus the delay that you suggested to update the status, seems to have
done the trick.
Here's what worked:
set SecondsToWait to 30 -- Number of seconds to wait on Safari
tell application "Safari"
quit --set up for the test
delay 3 -- wait until almost certainly quiescent for the test
activate
open location "https://www.disney.com" --begin test
repeat SecondsToWait * 2 times
delay 0.5 -- Add a small delay for Safari to update its status
set ReadyState to do JavaScript "document.readyState" in
document 1
log "ReadyState=" & ReadyState
if ReadyState is "complete" then exit repeat
end repeat
end tell
(*ReadyState=loading*)
(*ReadyState=interactive*)
(*ReadyState=interactive*)
(*ReadyState=complete*)
Much better than adding ad hoc delays to my code.
Thanks for the suggestion, Joel!
--Gil
> On Feb 10, 2025, at 4:52 PM, Joel Esler <email@hidden> wrote:
>
> Hi Gil,
>
> The method you're using with document.readyState is correct, but it may not
> always be reliable due to timing issues.
>
> One alternative approach is to periodically check document.readyState with a
> slight delay between checks to give Safari enough time to update the page
> status. You can achieve this with the delay command in AppleScript.
>
> Try modifying your script with a small delay like this:
>
> tell application "Safari"
> open location "https://www.disney.com"
> set SecondsToWait to 30 -- Number of seconds to wait on Safari
>
> repeat SecondsToWait times
> set ReadyState to do JavaScript "document.readyState" in document 1
> log "ReadyState=" & ReadyState
> if ReadyState is "complete" then exit repeat
> delay 0.5 -- Add a small delay between each check
> end repeat
> end tell
>
> By adding a half-second delay (or any small value that works for you), you
> should be able to avoid exiting the loop prematurely, giving the page enough
> time to load completely.
>
> Hope this helps!
>
>
>> On Feb 10, 2025, at 19:26, Gil Dawson via AppleScript-Users
>> <email@hidden> wrote:
>>
>>
>> Hi--
>>
>> Is there a way to tell when Safari completes loading a webpage?
>>
>> I'm using do JavaScript ("document.readyState")...
>>
>> set TargetLoc to "disney.com"
>>
>> tell application "Safari" to ¬
>> tell its front document to ¬
>> open location TargetLoc
>>
>> set SecondsToWait to 30 --Number of seconds to wait on Safari
>>
>> repeat SecondsToWait times
>> tell application "Safari" to ¬
>> tell its front document to ¬
>> set ReadyState to ¬
>> do JavaScript ("document.readyState")
>> log "ReadyState=" & ReadyState
>> if ReadyState is "complete" then exit repeat ------------
>> -- delay 1 --no effect when I comment out the delay
>> end repeat
>>
>> --> (*ReadyState=complete*)
>>
>> ... but, even without the delay, only one message gets logged.
>> My repeat loop seems to exit before the page actually appears.
>>
>> My workaround is to insert plenty-long-enough delays in my code.
>>
>> MacOS Sequoia 15.3 running Safari 18.3 on a MacBook Air M1, 2020
>>
>> --Gil
>>
>>
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> AppleScript-Users mailing list (email@hidden)
>> Help/Unsubscribe/Update your Subscription:
>> Archives: http://lists.apple.com/archives/applescript-users
>>
>> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden