[英]Mac - How to programmatically hide NSApplicationActivationPolicyAccessory or LSUIElement application?
I have a small Swift-based Cocoa app that I'm writing. 我正在编写一个基于Swift的小型Cocoa应用程序。 It's a single window app, something like Spotlight/QuickSilver/Alfred.
它是一个单一的窗口应用程序,类似Spotlight / QuickSilver / Alfred。 It's set as
NSApplicationActivationPolicyAccessory
( docs ) (though I've tried the same thing using LSUIElement
, which is equivalent). 它被设置为
NSApplicationActivationPolicyAccessory
( docs )(虽然我尝试使用LSUIElement
做同样的事情,这是等效的)。 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()
. 一切都运行良好,除了当它处于活动状态时我无法使用
NSRunningApplication.currentApplication().hide()
应用程序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 ." hide方法的文档说:“如果应用程序已经退出,或者某个类型无法隐藏,则该值的属性为
NO
。” (emphasis mine), and I'm getting a NO
back (though I'm actually using Swift, so I'm getting false
). (强调我的),我得到了一个
NO
(虽然我实际上使用的是Swift,所以我变得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. 我可以理解为什么
NSApplicationActivationPolicyProhibited
应用程序无法隐藏,因为它永远不会被激活,但令我感到困惑的是NSApplicationActivationPolicyAccessory
也是如此。
I tried myWindow.orderOut(self);
我试过
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. 我存储了对以前活动的应用程序的引用,所以如果需要,我可以再次手动激活该应用程序,但我希望有一个更简洁的方法来执行此操作。
Use NSApplication.sharedApplication().hide(nil)
. 使用
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. 通常会解决应用程序对象(
NSApplication
的实例)而不是NSRunningApplication
的实例来操作当前的应用程序。
手动解决方案是存储对先前活动的NSRunningApplication
实例的引用(下面称为previouslyActiveApplication
),然后在您要停用NSApplicationActivationPolicyAccessory
应用程序时激活它,如下所示:
previouslyActiveApplication!.activateWithOptions(NSApplicationActivationOptions.ActivateIgnoringOtherApps);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.