在没有停靠栏图标的 Mac OS X 中启动 GUI 进程

2021-12-09 00:00:00 multithreading macos qt c++

我有一个通常使用标准图形界面运行的应用程序.但是,对于某些长时间运行的任务,它会生成以脚本模式"运行的同一应用程序的其他进程,我从父进程控制它.一切都很好,除了对于每个子进程,我都会得到另一个停靠栏图标,该图标会弹出一两秒钟然后消失.

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"

相关文章