How would I make a checkbox hide the dock icon if it was checked? I have made a checkbox toggle a menubar item but how would you do it with the dock icon? Looking for some code to do this. Thanks!
5 Answers
Update for Swift, use both ways has been presented above (they give 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
}
-
Hiding the icon with this methods hides the app itself too. I have a timer which makes it appear again (without menu) but this is just bad UX Jan 12, 2017 at 6:31
i've use this code:
BOOL iconInDock = [[NSUserDefaults standardUserDefaults] boolForKey:smHideShowIcon];
if (iconInDock) {
ProcessSerialNumber psn = { 0, kCurrentProcess };
// display dock icon
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
}
ok, it's work for my application if I to set LSUIElement=1 in the Info.plist. That's code works only for show, but how I can hide icon?
-
1
-
Why am I getting those errors even when I have imported the frameworks?– JoshuaJul 17, 2009 at 5:41
-
pupsor.com/wp-content/uploads/2009/07/Joshua.zip it's archive of project with example working showing. Try that and send result– khakimovJul 18, 2009 at 5:02
-
Thanks! I've got that to work although when my Application is LSUIElement there is no way to get to the preferences window to change the settings so people can make it a normal app, this is because it is never in the menubar, it always has the name of another application. How would I get it to show the Menubar so people can actually change the app back to a normal app? For example. snapplr.com/93mm The App is the window in the bottom corner it's selected but it's still showing Finder as the selected app. Also whats odd since i entered this code I can't type 'a' when the app is open.– JoshuaJul 18, 2009 at 7:21
-
yea, i see. add this code after TransformProcessType... it works ;) // switch to Dock.app [[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier:@"com.apple.dock" options:NSWorkspaceLaunchDefault additionalEventParamDescriptor:nil launchIdentifier:nil]; // switch back [[NSApplication sharedApplication] activateIgnoringOtherApps:TRUE];– khakimovJul 18, 2009 at 17:26
(Posting this as an answer because comments don't have code formatting)
QSBApplicationDelegate.m:223-228
BOOL iconInDock = [[NSUserDefaults standardUserDefaults] boolForKey:kQSBIconInDockKey];
if (iconInDock) {
ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
}
-
So If I put that code in my App Delegate and connect a checkbox to a User Defaults Controller. Will it work?– JoshuaJul 5, 2009 at 6:57
-
Thanks For The Help, Just tried putting the code in but i got quite a few errors. Here is a picture snapplr.com/jckq .– JoshuaJul 5, 2009 at 7:03
-
1You need to include the right headers (look at the docs for TransformProcessType), use your own preferences rather than a constant from the QSB project, and make your app an LSUIElement in its plist.– smorganJul 5, 2009 at 14:26
-
-
1@Joshua "kQSBIconInDock" is a constant defined by the QSB project, and you still haven't imported the headers that define TransformProcessType. In short, you didn't read @smorgan's comment. =) Jul 6, 2009 at 16:20
You would want to set up your application as LSUIElement, and then use TransformProcessType to enable the Dock icon. The app will need to be relaunched for the change to take effect. See the Google Quick Search Box project for an example.
-
1Ah I See Thanks, Do you think you would be able to add some code to your answer because I had a look at the google project but there were so many files I couldn't see what they actually hd done.– JoshuaJul 4, 2009 at 16:13
-
QSBApplicationDelegate.m lines 223 - 228. They've got the preference itself hooked up to a NSShardDefaultsController. They turn the app into a Dock app on the lines I mentioned. Jul 4, 2009 at 21:04
Setup your application as an LSUIElement and then call:
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
on launch.
This works for the MAS too.