0

Similar to Return Javascript variable to AppleScript , I want to run JavaScript in Safari and return a value to AppleScript. This works:

tell application "Safari"
    tell front document
        set myVar to do JavaScript "function foo() {return 5;};result=foo()"
        return myVar
    end
end tell

The problem is that the function returning the variable is asynchronous, waiting for the server to respond a few times. This fails:

tell application "Safari"
    tell front document
        set myVar to do JavaScript "async function foo() {return 5;};result=foo()"
        return myVar
    end tell
end tell

with error Script Error: The variable myVar is not defined.

With javascript async function foo() {return 5;};result=await foo(), I get the same error.

The reason I want a value from the async function is that I'm automating a reply to messages on a website. One JavaScript function closes existing messages pop-ups, and waits until they all disappear (with const async_delay = ms => new Promise(res => setTimeout(res, ms)); and an await). Another JavaScript function opens the message pop-up I want and waits until it appears. For now, I am using a delay in AppleScript that is long enough. But the time required for JavaScript varies with the initial number of messages: if AppleScript waits too long, another pop-up message may appear in the meantime and the code sends a reply to the wrong person. Hence, it would be cleaner and more robust if I can fetch the result from asynchronous JavaScript.

Can AppleScript fetch variables from async JavaScript?

9
  • 1
    Try internet searching your question "Can AppleScript fetch variables from async JavaScript". When I did a link came up where an OP (different site) brings up inserting a delay (say 2 or more seconds, but depends, just needs to be long enough) to give time for the async to return a value. Commented Jan 24, 2024 at 13:34
  • 1
    "async function foo() {return 5;};result= await foo()" ? Commented Jan 24, 2024 at 13:37
  • @BurakAyyildiz With your code, I still get the same error. I edited the question. Commented Jan 24, 2024 at 14:30
  • 1
    "but if AppleScript waits too long, other unwanted elements may appear in the page" ... What other elements? And is the AppleScript blocking or non-blocking? ... In addition, from the SO post you linked refer to @Jerry Stratton's answer. That may be another issue with having a return. He says not to use one. stackoverflow.com/questions/23224563/… Commented Jan 24, 2024 at 23:17
  • 1
    Would it be possible to invoke the function and set the result to a hidden HTML field? Then in your AppleScript, do a loop to read the value of this hidden form, if missing, wait for 1 second, repeat a couple of times until you get the value you were looking for in that hidden field or if the max number of retries is reached. Commented Feb 2, 2024 at 10:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.