Can I run "do javascript" when Safari is not visible?

I have written this simple script:

tell application "Safari" to do JavaScript "audioplayer.paused ? audioplayer.play() : audioplayer.pause()" in tab "Overcast" of window 1

It simply plays and pauses the web player for the Overcast podcast website. The script works when Safari is active or even when is not frontmost but is visible on a second monitor.

However, the script does not work if Safari is hidden or not visible. How can I make the script work when Safari is not visible? Is it even possible?

Thanks.

Hey There,

No, you’re out of luck unless/until Apple starts fixing long-standing bugs in its applications.

Safari does not properly recognize the concept of front window or window 1 when it is hidden.

Run this script with Safari visible and hidden, and scope-out the difference in the results:


tell application "Safari"
	{index, name} of (windows where its document is not missing value)
end tell

The best you can do is put in code to make certain Safari is not hidden before running the main part of your script.


tell application "System Events"
	set quit delay to 0
	tell application process "Safari"
		if its visible is false then set its visible to true
	end tell
end tell

I’ve been fussing about this and to Apple about this for many years… :frowning: