Start a GUI process in Mac OS X without dock icon

匿名 (未验证) 提交于 2019-12-03 01:33:01

问题:

I have an application that normally runs with a standard graphical interface. However, for certain long-running tasks, it spawns additional processes of the same application that run in a "script mode," where I am controlling it from the parent process. Everything works great, except that for each child process I get another dock icon that pops in for a second or two and then disappears.

Is there a way to run an application sometimes without the application icon showing up on the dock? I can't edit the info.plist or anything because normally I want the dock icon. The option must be able to be set by changing a property on the process or via a command line parameter. I have full control over the source to the application. It is written in C++ (Qt), but solutions that target the native Cocoa library are fine.

If I put this code into a separate application it would cause major duplication, so I'd rather keep it the way it is. I cannot run the long-running tasks in background threads because they are doing things that must be done in a GUI thread. (In Qt, you cannot reliably use fonts, pixmaps, or render SVG content onto a QGraphicsScene on background threads.)

Any solutions?

回答1:

Motivated from here, you can do:

[NSApp setActivationPolicy: NSApplicationActivationPolicyAccessory]; 

or

[NSApp setActivationPolicy: NSApplicationActivationPolicyProhibited]; 

This should hide the dock icon. See here for some documentation about NSApplicationActivationPolicy.

In Python, the code to hide the dock icon is:

# https://stackoverflow.com/a/9220857/133374 import AppKit # https://developer.apple.com/library/mac/#documentation/AppKit/Reference/NSRunningApplication_Class/Reference/Reference.html NSApplicationActivationPolicyRegular = 0 NSApplicationActivationPolicyAccessory = 1 NSApplicationActivationPolicyProhibited = 2 AppKit.NSApp.setActivationPolicy_(NSApplicationActivationPolicyProhibited) 

See also the related question "How to hide the Dock icon".


If you want to avoid that the dock icon pops up at all right at the beginning, you can do that:

import AppKit info = AppKit.NSBundle.mainBundle().infoDictionary() info["LSBackgroundOnly"] = "1" 


回答2:

import AppKit info = AppKit.NSBundle.mainBundle().infoDictionary() info["LSBackgroundOnly"] = "1" 

This code works for my Non-GUI background python script.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!