3
votes

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.

2
Have you tried 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.Ken Thomases
@KenThomases That did it! Thanks! Can you write this as an answer so I can mark it as the right one?Matt

2 Answers

6
votes

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.

0
votes

The manual solution is to store a reference to the previously active NSRunningApplication instance (called previouslyActiveApplication below), and then activate it when you want to deactivate your NSApplicationActivationPolicyAccessory application, like so:

previouslyActiveApplication!.activateWithOptions(NSApplicationActivationOptions.ActivateIgnoringOtherApps);