Mac - How to programmatically hide NSApplicationActivationPolicyAccessory or LSUIElement?
I have a small Swift based Cocoa application that I am writing. It's a single window application, something like Spotlight / QuickSilver / Alfred. It's installed as NSApplicationActivationPolicyAccessory
( docs ) (although I've tried the same using LSUIElement
, which is equivalent). It is activated with a global hotkey.
Everything works well, except that when it is active, I cannot 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 is already exited, or if the type cannot be hidden ." (highlight is mine) and I get NO
back (although I am actually using Swift, so I get false
).
I can understand why the app NSApplicationActivationPolicyProhibited
won't be able to be hidden since it never works, but it confuses me that this also applies to NSApplicationActivationPolicyAccessory
.
I tried it myWindow.orderOut(self);
, but this just hides the window without hiding my application and returning focus to the previous application.
I keep a link to a previously active app, so I can manually reactivate that app if needed, but I hope there is a cleaner way to do this.
source to share
The manual solution is to keep a link to the previously active instance NSRunningApplication
(named previouslyActiveApplication
below) and then activate it if you want to deactivate the application NSApplicationActivationPolicyAccessory
, for example:
previouslyActiveApplication!.activateWithOptions(NSApplicationActivationOptions.ActivateIgnoringOtherApps);
source to share