Condition Related to macOS Playing Any Sound

Is there a way (maybe through a variable) to check if any sound (music, video playing, notification, etc) is currently playing at the moment a macro is triggered?

My goal is to trigger a macro with a shortcut but cancel it (or do something else) if a sound is playing.

Thanks!

Hey Ken,

This might work.

I'm fairly certain you won't be able to detect notifications.

-Chris

1 Like

Hi, ccstone! Thank you for your response. It works but it delays. Do we have a better way to solve the problem? PS: I've tried to replay the post above, but I'm not qualified enough to replay it.

Are you trying to do it via Terminal? The following takes only a couple of 10ths of a second, max, on my clunky old iMac:

Audio Test.kmmacros (3.4 KB)

Image

Yes. I've run it via Terminal. It responded promptly when a video played but delayed for several seconds when I paused the video.

And the macro above had the same problem.

I take it you actually mean it took a few seconds after pausing for the answer to be "No" rather than the script was delayed in its execution (which certainly shouldn't happen!). AFAIK there's no way round that, it's the way CoreAudio and pmset behave.

Perhaps you could explain what you want to achieve? The "better way to solve the problem" might be to attack it from a different angle.

1 Like

That's a good question. I want a video of a YouTube page on a Chrome browser auto full screen if it's playing when switching to the browser. But it didn't work well when the delay occurs ▼

The macro I created is as below:
Test Macro (v10.2)
Test.kmmacros (6.8 KB)

If you use a local variable you won't have to delete it at the end of the macro. You only care if a video is playing if the front window is Youtube, so test the front window first so you aren't running the shell script unnecessarily. The expression you are using to test the URL looks strange to me, but perhaps you are getting a different URL via a proxy service or similar -- I've used what works for me.

On my machine (M1 Air running Ventura) Core Audio seems to be running all the time (probably an issue with my setup). But when Chrome is playing a video it prevents sleep, so I've used that in the test instead -- that also seems to "release" quicker than Core Audio when I pause the video. I've added a dialog at the end to show how long the macro takes to run plus the result of the test, in case you need it for troubleshooting.

Chrome Test.kmmacros (8.8 KB)

Image

1 Like

I've tested it several times. The delay is still quite severe on my Mac (Intel Core i7 Ventura 13.2.1).

▲ Like I mentioned before, it will respond 'no audio playing' while some audio is playing.

You might ask on the Script Debugger Forum if there's a way to reach directly into coreaudio with AppleScriptObjC and test for any audio streams.

Did you try the "sleep-testing" macro above? Because that won't make any mention of audio in its responses. What times are being reported?