I’m having trouble with AppleScript and Safari on my new M1 MacBook Pro running macOS 12.0.1. It seems like JavaScript boolean values aren’t being returned properly anymore.
Here’s what used to work:
tell application "Safari" to do JavaScript "document.querySelector('video') == null" in document 1
This would give me true or false. But now it’s not returning anything (just a missing value).
I found a workaround, but it feels clunky:
tell application "Safari"
set result to do JavaScript "document.querySelector('video') ? 0 : 1" in document 1
return result is 1
end tell
Oddly, other types still work fine. For example, this returns 3.0:
tell application "Safari" to do JavaScript "3" in document 1
What’s going on here? Is there a better way to get boolean results? Has anyone else run into this issue?
I’ve encountered this issue as well on my M1 Mac mini. It seems to be a quirk with how AppleScript and Safari are interacting on Apple Silicon. One approach that’s worked reliably for me is to wrap the JavaScript in a function that explicitly returns the boolean value. Something like this:
tell application "Safari" to do JavaScript "(function() { return document.querySelector('video') === null; })()" in document 1
This method forces JavaScript to evaluate and return the boolean directly, which AppleScript seems to handle better. It’s a bit more verbose, but it’s been consistent in my testing across various scenarios. Hope this helps solve your problem!
hey there! i ran into a similar issue on my m1 air. frustrating, right? try using toString() to convert the boolean to a string, then parse it in applescript. like:
do JavaScript “(document.querySelector(‘video’) == null).toString()” in document 1
then use “true” = result to check. not ideal, but it worked for me!
I’ve been dealing with this issue too on my M1 Mac. It’s definitely a pain. Have you tried using the Number() function to convert the boolean to a numeric value? Something like this might work:
tell application “Safari” to do JavaScript “Number(document.querySelector(‘video’) == null)” in document 1
This should return 1 for true and 0 for false, which AppleScript seems to handle better. It’s not perfect, but it’s been pretty reliable for me without adding too much complexity. Let me know if that helps or if you need any clarification!