我想隐藏Dock图标并显示一个NSStatusItem
.我可以创建StatusItem但我不知道如何从Dock中删除图标.: - /
有任何想法吗?
我想你正在寻找LSUIElement
Info.plist中的内容
LSUIElement(String).如果此键设置为"1",则启动服务将应用程序作为代理应用程序运行.代理应用程序不会出现在Dock或强制退出窗口中.虽然它们通常作为后台应用程序运行,但如果需要,它们可以到前台呈现用户界面.
请参阅此处有关打开/关闭它的简短讨论
您可以使用所谓的激活策略:
// The application is an ordinary app that appears in the Dock and may
// have a user interface.
[NSApp setActivationPolicy: NSApplicationActivationPolicyRegular];
// The application does not appear in the Dock and does not have a menu
// bar, but it may be activated programmatically or by clicking on one
// of its windows.
[NSApp setActivationPolicy: NSApplicationActivationPolicyAccessory];
// The application does not appear in the Dock and may not create
// windows or be activated.
[NSApp setActivationPolicy: NSApplicationActivationPolicyProhibited];
// The application is an ordinary app that appears in the Dock and may
// have a user interface.
NSApp.setActivationPolicy(.regular)
// The application does not appear in the Dock and does not have a menu
// bar, but it may be activated programmatically or by clicking on one
// of its windows.
NSApp.setActivationPolicy(.accessory)
// The application does not appear in the Dock and may not create
// windows or be activated.
NSApp.setActivationPolicy(.prohibited)
这应该隐藏停靠图标.
启发这一点的答案:如何创建一个也有(可移动)停靠图标的帮助应用程序(LSUIElement)
文档NSRunningApplicationActivationPolicy
.
一个相关问题:"在没有停靠栏图标的Mac OS X中启动GUI过程".
要遵守不修改应用程序包的Apple准则,并保证Mac App Store应用程序/(Lion应用程序?)不会通过info.plist修改打破其签名,您可以默认将LSUIElement设置为1,然后应用程序启动:
ProcessSerialNumber psn = { 0, kCurrentProcess }; TransformProcessType(&psn, kProcessTransformToForegroundApplication);
显示它的停靠图标,或者如果用户选择不想要图标则绕过它.
只有一个副作用,应用程序的菜单在丢失并重新获得焦点之前不会显示.
来源:制作复选框打开和关闭Dock图标
我个人不喜欢不设置任何Info.plist选项并使用TransformProcessType(&psn, kProcessTransformToForegroundApplication)
或TransformProcessType(&psn, kProcessTransformToUIElementApplication)
基于用户设置.
在Xcode 4中,它显示为"Application is agent(UIElement)",它是布尔值.
在Info.plist控件中 - 单击一个空白区域并从菜单中选择"添加行"类型"应用程序是代理(UIElement)"将其设置为YES.
为了使它可选,我在代码中添加了以下行(感谢Valexa!)
// hide/display dock icon if (![[NSUserDefaults standardUserDefaults] boolForKey:@"hideDockIcon"]) { //hide icon on Dock ProcessSerialNumber psn = { 0, kCurrentProcess }; TransformProcessType(&psn, kProcessTransformToForegroundApplication); }
更新Swift :(上面已经介绍了两种方式,它们具有相同的结果)
public class func toggleDockIcon_Way1(showIcon state: Bool) -> Bool { // Get transform state. var transformState: ProcessApplicationTransformState if state { transformState = ProcessApplicationTransformState(kProcessTransformToForegroundApplication) } else { transformState = ProcessApplicationTransformState(kProcessTransformToUIElementApplication) } // Show / hide dock icon. var psn = ProcessSerialNumber(highLongOfPSN: 0, lowLongOfPSN: UInt32(kCurrentProcess)) let transformStatus: OSStatus = TransformProcessType(&psn, transformState) return transformStatus == 0 } public class func toggleDockIcon_Way2(showIcon state: Bool) -> Bool { var result: Bool if state { result = NSApp.setActivationPolicy(NSApplicationActivationPolicy.Regular) } else { result = NSApp.setActivationPolicy(NSApplicationActivationPolicy.Accessory) } return result }