macos 如何隐藏 Dock 图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/620841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:03:59  来源:igfitidea点击:

How to hide the Dock icon

macoscocoaiconsdocklsuielement

提问by papr

I want to make a preference for hiding the Dock icon and showing an NSStatusItem. I can create the StatusItem but I don't know how to remove the icon from Dock. :-/

我想优先隐藏 Dock 图标并显示NSStatusItem. 我可以创建 StatusItem,但我不知道如何从 Dock 中删除该图标。:-/

Any ideas?

有任何想法吗?

回答by epatel

I think you are looking for the LSUIElementin the Info.plist

我认为您正在LSUIElementInfo.plist中寻找

LSUIElement (String). If this key is set to “1”, Launch Services runs the application as an agent application. Agent applications do not appear in the Dock or in the Force Quit window. Although they typically run as background applications, they can come to the foreground to present a user interface if desired.

LSUIElement(字符串)。如果此键设置为“1”,则启动服务将应用程序作为代理应用程序运行。代理应用程序不会出现在 Dock 或强制退出窗口中。尽管它们通常作为后台应用程序运行,但如果需要,它们也可以进入前台以呈现用户界面。

See a short discussion hereabout turning it on/off

在此处查看有关打开/关闭它的简短讨论

回答by Albert

You can use what is called Activation Policy:

您可以使用所谓的激活策略:

Objective-C

目标-C

// 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];

Swift 4

斯威夫特 4

// 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)

This should hide the dock icon.

这应该隐藏停靠栏图标。

See also

也可以看看

回答by valexa

To do it while abiding to the Apple guidelines of not modifying application bundles and to guarantee that Mac App Store apps/(Lion apps ?) will not have their signature broken by info.plist modification you can set LSUIElement to 1 by default then when the application launches do :

要做到这一点,同时遵守不修改应用程序包的 Apple 指南,并保证 Mac App Store 应用程序/(Lion 应用程序?)不会因为 info.plist 修改而破坏其签名,您可以将 LSUIElement 默认设置为 1,然后当应用程序启动做:

ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType(&psn, kProcessTransformToForegroundApplication);

to show it's dock icon, or bypass this if the user chose not to want the icon.

显示它的停靠栏图标,或者如果用户选择不想要该图标,则绕过它。

There is but one side effect, the application's menu is not shown until it losses and regains focus.

只有一个副作用,应用程序的菜单在它失去焦点并重新获得焦点之前不会显示。

Source: Making a Checkbox Toggle The Dock Icon On and Off

来源:制作复选框打开和关闭 Dock 图标

Personally i prefer not setting any Info.plist option and use TransformProcessType(&psn, kProcessTransformToForegroundApplication)or TransformProcessType(&psn, kProcessTransformToUIElementApplication)based on what is the user setting.

就个人而言,我不喜欢设置任何 Info.plist 选项并使用TransformProcessType(&psn, kProcessTransformToForegroundApplication)TransformProcessType(&psn, kProcessTransformToUIElementApplication)基于用户设置。

回答by Tibidabo

In Xcode 4 it is shown as "Application is agent (UIElement)" and it is Boolean.

在 Xcode 4 中,它显示为“Application is agent (UIElement)”并且它是布尔值。

In your Info.plist control-click to an empty space and select "Add Row" from the menu Type "Application is agent (UIElement)" Set it YES.

在您的 Info.plist 控件中单击一个空白区域,然后从菜单中选择“添加行” 类型“应用程序是代理(UIElement)”将其设置为 YES。

TO make it optional I added the following line to my code (thanks Valexa!)

为了使它成为可选,我在我的代码中添加了以下行(感谢 Valexa!)

 // hide/display dock icon
if (![[NSUserDefaults  standardUserDefaults] boolForKey:@"hideDockIcon"]) {
    //hide icon on Dock
    ProcessSerialNumber psn = { 0, kCurrentProcess };
    TransformProcessType(&psn, kProcessTransformToForegroundApplication);
} 

回答by Huy Nguyen

Update for Swift: (Use both ways has been presented above, they have the same result)

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
}

回答by Benedict Cohen

If you want to make it a user preference then you can't use UIElement. UIElement resides in the application bundle you shouldn't edit any of the files in the app bundle as this will invalidate the bundles signature.

如果您想使其成为用户首选项,则不能使用 UIElement。UIElement 驻留在应用程序包中,您不应编辑应用程序包中的任何文件,因为这会使包签名无效。

The best solution I've found is based on this excellent article. My solution is based on the comment by Dan. In short, There's no way to do this with Cocoa, but it is possible with a tiny bit of Carbon code.

我找到的最好的解决方案是基于这篇优秀的文章。我的解决方案基于 Dan 的评论。简而言之,使用 Cocoa 无法做到这一点,但是使用一点点 Carbon 代码就可以做到。

The article also suggests making a helper app that handles the dock icon exclusively. The main app then starts and kills this app depending on the users preferences. This approach strikes me as being more robust than using the Carbon code, but I haven't tried it yet.

这篇文章还建议制作一个专门处理停靠栏图标的助手应用程序。然后主应用程序根据用户的喜好启动并终止该应用程序。这种方法让我觉得比使用 Carbon 代码更健壮,但我还没有尝试过。