How can my app bring another app to the foreground when it's not currently the frontmost app?

I'm developing software that implements functionality for third party touch screens. It provides features such as having touch gestures trigger mouse clicks and drags, among other possible actions.

One of its features allows keyboard focus to return to whichever app originally had it prior to performing a touch gesture. Previously it worked by recording the current frontmost app when a touch action starts, and then when all touch actions have concluded, calling NSRunningApplication.activateWithOptions and passing NSApplicationActivateIgnoringOtherApps.

However, in macOS 14 and later, this no longer works, as NSRunningApplication.activateWithOptions is deprecated. The new API provides a way for the current frontmost app to yield frontmost to whichever app is being activated with the new functions NSRunningApplication.yieldActivationToApplication and NSRunningApplication.activate, but in my case it just silently fails, presumably because my app in that moment is not frontmost. (However there is no error code provided for me to be able to find out what exactly the issue is.)

Is there a supported way I can fix this feature in macOS 14 and later?

Some corrections (that I unfortunately cannot edit into my original post!)

  • NSRunningApplication.activateWithOptions isn't deprecated, but the option NSApplicationActivateIgnoringOtherApps is.
  • Instead of NSRunningApplication.yieldActivationToApplication I meant NSApplication.yieldActivationToApplication.
  • Instead of NSRunningApplication.activate I meant NSRunningApplication.activateFromApplication.

These corrections aside, I'm still looking for an answer to my original main question.

How can my app bring another app to the foreground when it's not currently the frontmost app?
 
 
Q