How to workaround KM detecting Hand off apps from iPhone as running on Mac?

I have a given macro which essentially needs to focus the app currently playing media. It checks if Podcasts is running, and focuses that. Otherwise it checks Books, then finally it will always open/focus Music if nothing else is running.

Apple devices have the ability to "hand off" apps between devices, so when Podcasts is opened on my iPhone I see it pop up on my Mac's dock. The problem is that KM will actually detect that Podcasts is running in this case which messes up the script if I happened to have Podcasts opened on my iPhone too recently.

I don't think this is specifically an issue with KM since running pgrep also shows Podcasts as running, but I'm wondering if there would be a way to work around this issue without just disabling the hand off feature?

Without seeing your macros, it's hard to say specifically, but you should be able to use an if/then action to see if the app is frontmost before you do anything.

image

-rob.

I don't use Apple Podcasts app, but some quick testing suggests that hand-off does not begin running a given app's process. I'm not sure how KM detects that a hand-off app is 'running', but I would probably solve this issue by adding either a try/catch or if statement that tests whether the app's process is actually running and if not to cancel the macro. (Note: process names are case senstive.)

I'm not trying to check if the app is frontmost, I'm trying to check if it's running. I want to focus it if it's running.

I called that out in my initial post. I originally did this through a script with pgrep but that still triggers from Hand off.

Whoops, my eyes skipped right over that. Strange, though, pgrep fails to find hand-off apps on my system. What version of macOS are you running?

15.3.1

I will say that in trying to replicate it for that screenshot above it took a few tries before it did, so it seems to be a little inconsistent whether pgrep/KM will detect it.

Sorry, I misunderstood. If Keyboard Maestro thinks it's running, it must be finding a match somewhere. Is there anything in ps -ax | grep Podcasts output that's different when a handoff Podcasts is "running" versus when it's running (really running) locally?

If there's no difference, then I suspect that's why Keyboard Maestro sees it as active, and it's going to be tricky to avoid the issue.

-rob.

Just to confirm, when you used pgrep did you capitalise the first letter in Podcasts? Because there are sometimes helper processes that pgrep will find if it's all lowercase, but the app's actual process will always be capitalised.

Have you tried changing the option at the very bottom of the application chooser popup to "Match by Path"?

image

I checked with ps and they both seem to use the same process (/Podcasts.app/Contents/MacOS/Podcasts), however it seems that the issue isn't actually hand off, but related to when you play audio?

So just opening Podcasts doesn't seem to run the process but if I play some of a track on my iPhone long enough the Mac process will start (to sync the timestamp of the current podcast I guess?)

Regardless I think for now I'll simply work around it by adding an additional check that the main music app is running, so it at least avoids the issue of Podcasts randomly opening when I want to focus Music.

1 Like

That seems like a reasonable guess as to what's going on.

-rob.