How to hide the Dock icon
Solution 1
I think you are looking for the LSUIElement
in the Info.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.
See a short discussion here about turning it on/off
Solution 2
You can use what is called Activation Policy:
Objective-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
// 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
- Answer that inspired this one: How to create a helper application (LSUIElement) that also has a (removable) dock icon
-
Documentation for
NSRunningApplicationActivationPolicy
. - A related question: "Start a GUI process in Mac OS X without dock icon".
Solution 3
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 :
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
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.
Solution 4
In Xcode it is shown as "Application is agent (UIElement)" and it is Boolean.
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.
TO make it optional I added the following line to my code (thanks Valexa!)
// hide/display dock icon
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"hideDockIcon"]) {
//hide icon on Dock
ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
}
Solution 5
Update for Swift: (Use both ways has been presented above, they have the same result)
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
}
Related videos on Youtube
DimP
Developer of ShortIt, the native URL shortener for Mac OS X and reachable on Twitter under _papr
Updated on August 25, 2021Comments
-
DimP almost 3 years
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. :-/Any ideas?
-
Chris Dolan about 9 yearsif your app is based on Qt5, you also need to set the envvar
QT_MAC_DISABLE_FOREGROUND_APPLICATION_TRANSFORM
-
-
MrMage almost 13 yearsThanks! Just what I've been looking for!
-
Form over 12 yearsGreat tip! Thanks! You will always want hide the Dock icon this way in order to make sure your signed app works.
-
SG1 over 11 yearsSolutions derived from this codepath don't allow for an app that actually wants to be LSUIElement YES (as in, have no menubar, etc). Toggling the process in this fashion will cause a menu to be shown as stated in the answer. I certainly respect that this is the closest thing to an answer for this overlooked functionality, but it isn't a precise solution. I tell users to just manually add the app to the Dock if they want an icon there.
-
codingFriend1 about 11 yearsThis is definitely the most elegant solution. Works perfectly.
-
Mark Bao about 11 years+1.
NSApplicationActivationPolicyAccessory
actually allows the main menu to show up. -
Scott Allen almost 11 yearsFrom the Apple docs: Currently, NSApplicationActivationPolicyNone and NSApplicationActivationPolicyAccessory may be changed to NSApplicationActivationPolicyRegular, but other modifications are not supported.
-
Al Zonke over 10 yearsSingle right way. Other solutions are some kinds of hacks. Also it's possible to modify dock behavior on the fly.
-
slboat about 9 yearsa great way!simplest way!
-
csiu over 7 yearsThe latest header comment says: "In OS X 10.9, any policy may be set; prior to 10.9, the activation policy may be changed to NSApplicationActivationPolicyProhibited or NSApplicationActivationPolicyRegular, but may not be changed to NSApplicationActivationPolicyAccessory. This returns YES if setting the activation policy is successful, and NO if not."
-
Ky - almost 6 yearsThis seems to be the Apple-intended way
-
Pedro Paulo Amorim about 5 yearsThe link is dead.
-
meMadhav almost 5 yearsThis is showing app icon in doc and removing it from dock immediately. I dont want to make any changes in dock. Any hint.
-
meMadhav almost 5 yearsThis is showing app icon in doc and removing it from dock immediately. I dont want to make any changes in dock. Any hint.
-
boraseoksoon over 3 yearsAmazingly helpful
-
moonmonkey almost 3 yearsHi, i'm trying to use this, I placed the extension in a file and the function in the view controller. But the function errors with "Type 'Self' has no member 'Dock'" and "Cannot infer contextual base in reference to member 'viaMenuVisibilityToggle'". any idea where i'm going wrong?
-
Vlad almost 3 yearsTry to call
NSApplication.Dock.setAppIconVisibleInDock(...)
instead ofSelf.Dock.setAppIconVisibleInDock(...)
. -
Vlad almost 3 years@moonmonkey Don't forget to upvote if it works :)