Monday, March 1, 2021

#1 2021-02-18 10:16:59 am

rpaulpen
Member
Registered: 2021-02-09
Posts: 9

Catching a Safari Time Out

Hi all,

My script runs through websites and when it loads one with a pop up, I want it to time out and return a message I can pass back to the calling routine.  I can't seem to get it to work.  Here is a sample script where it should display a message instead of returning to calling routine.

tell application "Safari"
    activate
    --make new document
   
    set oldURL to "https://www.homedepot.com"
    try
       
        with timeout of 20 seconds
            tell window 1 to set current tab to (make new tab at end of tabs with properties {URL:oldURL})
            set newURL to URL of document 1
        end timeout
       
    on error errMsg
        set newURL to "Timed Out, Don't Use"
       
        display dialog "ERROR: " & errMsg & newURL
    end try
   
end tell



If you load the homedepot.com site it will pop up a location window.  If this sits long enough it times out and crashes the script.  I just want to catch the timeout and basically skip over it by returning something else. 

The above script returns the error "missing value" since it can't find URL and never gets a chance to fully load due to the popup.  Shouldn't the error trap catch that or am I missing something? In reality, I check the URL in a loop for a few seconds for slow loads, but once that's done, I need to catch the pop up before it crashes the script.

This is the result in the console:

tell application "Safari"
    activate
    make new tab at end of every tab of window 1 with properties {URL:"https://www.homedepot.com"}
        --> tab 13 of window id 1225
    set current tab of window 1 to tab 13 of window id 1225
    get URL of document 1
        --> missing value
end tell
Result:
missing value



Any advice would be appreciated.

RP

Offline

 

#2 2021-02-18 11:26:09 am

rpaulpen
Member
Registered: 2021-02-09
Posts: 9

Re: Catching a Safari Time Out

Now that I've done some further testing, the URL is returned even though the pop up message box is shown.  It's one of those boxes requesting an answer like USA or Canada site? 
The script seems to hang there and not move on to the next one.  Maybe the answer is to test if it's hanging and then move on. 

Any ideas?

RP

Offline

 

#3 2021-02-18 01:42:56 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1645

Re: Catching a Safari Time Out

Takes some time creating new tab in the Safari.app. Adding little delay should help:

Applescript:


set oldURL to "https://www.homedepot.com"

tell application "Safari"
   activate
   if not (exists window 1) then make new document
   try
       with timeout of 20 seconds
           tell window 1 to set current tab to (make new tab at end of tabs with properties {URL:oldURL})
           delay 0.5 -- THIS
           set newURL to URL of (get window 1's document)
       end timeout
   on error errMsg
       set newURL to "Timed Out, Don't Use"
       display dialog "ERROR: " & errMsg & newURL
   end try
end tell

Last edited by KniazidisR (2021-02-18 01:43:21 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#4 2021-02-18 06:39:06 pm

rpaulpen
Member
Registered: 2021-02-09
Posts: 9

Re: Catching a Safari Time Out

Thank you for your reply.  Yes, it needs some time.  In the actual code I send it in a loop with a delay checking for URL is not "". 

It seems to hang though when one of these popup's come up requiring a response.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)