問題描述
我有一個通常使用標準圖形界面運行的應用程序.但是,對于某些長時間運行的任務,它會生成以腳本模式"運行的同一應用程序的其他進程,我從父進程控制它.一切都很好,除了對于每個子進程,我都會得到另一個停靠欄圖標,該圖標會彈出一兩秒鐘然后消失.
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.
有沒有辦法運行應用程序有時,而應用程序圖標不會顯示在 Dock 上?我無法編輯 info.plist 或任何東西,因為通常我想要停靠圖標.該選項必須能夠通過更改進程的屬性或通過命令行參數來設置.我可以完全控制應用程序的來源.它是用 C++ (Qt) 編寫的,但針對原生 Cocoa 庫的解決方案也不錯.
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.
如果我將此代碼放入單獨的應用程序中,則會導致大量重復,因此我寧愿保持原樣.我無法在后臺線程中運行長時間運行的任務,因為它們正在做必須在 GUI 線程中完成的事情.(在 Qt 中,您無法可靠地使用字體、像素圖或將 SVG 內容渲染到后臺線程的 QGraphicsScene 上.)
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.)
有什么解決辦法嗎?
推薦答案
從 這里 得到啟發,您可以:
Motivated from here, you can do:
[NSApp setActivationPolicy: NSApplicationActivationPolicyAccessory];
或
[NSApp setActivationPolicy: NSApplicationActivationPolicyProhibited];
這應該隱藏停靠欄圖標.有關一些文檔,請參閱此處關于NSApplicationActivationPolicy
.
This should hide the dock icon. See here for some documentation about NSApplicationActivationPolicy
.
在 Python 中,隱藏停靠欄圖標的代碼是:
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)
另見相關問題如何隱藏 Dock 圖標".
如果你想避免一開始就彈出dock圖標,你可以這樣做:
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"
這篇關于在沒有停靠欄圖標的 Mac OS X 中啟動 GUI 進程的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!