Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start a GUI process in Mac OS X without dock icon

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?

like image 458
Dave Mateer Avatar asked Dec 27 '22 16:12

Dave Mateer


1 Answers

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"
like image 168
Albert Avatar answered Dec 30 '22 06:12

Albert