LasseAarhus
LasseAarhus

Reputation: 79

document.readyState "Access not allowed" with Applescript +Javascript that waits for page to load

I'm trying to write an applescript that opens a html page in my browser, waits for it to load, then runs window.find to check for a specific keyword. It is very important that the .find can run as soon as the page is loaded but doesn't try before. If the keyword is present, the script clicks the "next" button. This is a loop i tried to make, to check whether the html is loaded

to checkInteractive()       

    repeat 60 times

        try     
            tell application "Safari"
                set a to do JavaScript 
                "document.readyState" in document 1
            end tell
        end try

        if a = "interactive" then
            exit repeat
        end if

        delay 0.01

    end repeat

end checkInteractive

but i get this error:

SYNTAX ERROR Can’t get "document.readyState" in document 1. Access not allowed.

Does anyone know what i should do different? Or whether there is another way to check if the html is loaded?

Upvotes: 0

Views: 485

Answers (1)

vadian
vadian

Reputation: 285180

Either put the statement in one line

tell application "Safari"
     set a to do JavaScript "document.readyState" in document 1
end tell

or use the AppleScript line separator

tell application "Safari"
    set a to do JavaScript ¬
        "document.readyState" in document 1
end tell

Upvotes: 1

Related Questions