I'm trying to determine if a tab is playing audio via javascript, which is being called by the following applescript:
tell application "Safari"
set theScript to "!!Array.prototype.find.call(document.querySelectorAll('audio,video'),function(elem){return elem.duration > 0 && !elem.paused});"
set ret to do JavaScript theScript in current tab of first window
return ret
end tell
However, I get the following error:
error "The variable ret is not defined." number -2753 from "ret"
This javascript works fine when called through the Safari Console. I have tried investigating missing imports that are loaded in Console but not via the call from Applescript, but can't seem to solve this. Please could someone help me?
Edit:
Running the following works, so the problem lies with the specific javascript I am trying to run
tell application "Safari"
set theScript to "numbervalue=3;fred=numbervalue+4"
set ret to do JavaScript theScript in current tab of first window
return ret
end tell
returns 7
as expected
For some strange reason do JavaScript
has trouble returning boolean
s, so after hours of trying things this ended up working:
tell application "Safari"
set theScript to "a = !!Array.prototype.find.call(document.querySelectorAll('audio,video'),function(elem){return elem.duration > 0 && !elem.paused}); a.toString();"
set ret to do JavaScript theScript in current tab of first window
return ret
end tell
BUT this is only a problem when calling in Safari! Hope this helps a poor soul out