Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac - How to programmatically hide NSApplicationActivationPolicyAccessory or LSUIElement application?

Tags:

macos

cocoa

I have a small Swift-based Cocoa app that I'm writing. It's a single window app, something like Spotlight/QuickSilver/Alfred. It's set as NSApplicationActivationPolicyAccessory (docs) (though I've tried the same thing using LSUIElement, which is equivalent). It's activated via a global hot key.

Everything works well, except that when it's active I can't hide the application using NSRunningApplication.currentApplication().hide().

The docs for the hide method say "The property of this value will be NO if the application has already quit, or if of a type that is unable to be hidden." (emphasis mine), and I'm getting a NO back (though I'm actually using Swift, so I'm getting false).

I can understand why a NSApplicationActivationPolicyProhibited app wouldn't be able to be hidden, since it's never active, but it's confusing to me that this would be the case with NSApplicationActivationPolicyAccessory too.

I tried myWindow.orderOut(self);, but that just hides the window without hiding my application and returning focus to the previous app.

I do store a reference to the previously active application, so if need be I can manually activate that app again, but I'm hoping there is a cleaner method of doing this.

like image 987
Matt Avatar asked Oct 22 '25 16:10

Matt


1 Answers

Use NSApplication.sharedApplication().hide(nil). One would normally address the application object (instance of NSApplication) rather than an instance of NSRunningApplication to operate on the current app.

like image 181
Ken Thomases Avatar answered Oct 25 '25 09:10

Ken Thomases