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.
NSApplication.sharedApplication().hide(nil)
? One would normally address the application object (instance ofNSApplication
) rather than an instance ofNSRunningApplication
to operate on the current app. – Ken Thomases