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!
+2
A:
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.
smorgan
2009-07-04 15:42:26
Ah 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.
Joshua
2009-07-04 16:13:12
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.
Dave DeLong
2009-07-04 21:04:28
I'll take a look thanks.
Joshua
2009-07-05 06:55:55
+2
A:
(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);
}
Dave DeLong
2009-07-04 21:05:31
So If I put that code in my App Delegate and connect a checkbox to a User Defaults Controller. Will it work?
Joshua
2009-07-05 06:57:14
Thanks For The Help, Just tried putting the code in but i got quite a few errors. Here is a picture http://snapplr.com/jckq .
Joshua
2009-07-05 07:03:08
You 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.
smorgan
2009-07-05 14:26:43
Add an entry into your Info.plist file where the key is "LSUIElement" and the value is a boolean checkbox that's been checked. Xcode will give you an autocomplete popup with english descriptions of the key. "LSUIElement" corresponds to "Application is agent (UIElement)"
Dave DeLong
2009-07-05 17:08:47
I've done that but i still get these errors (http://snapplr.com/nqec). What code do I need to add to make it work? Also, does the code in your answer need to be in an action, so i can connect the action to the checkbox?
Joshua
2009-07-06 15:36:21
@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. =)
Dave DeLong
2009-07-06 16:20:44
Oh sorry, I didn't see that comment. I looked at the Docs but I am unsure what i Should put. Is it something like '#import Processes.h' ?
Joshua
2009-07-06 17:15:09
@Joshua look in the docs for TransformProcessType. Notice that at the top of the documentation, it says "Framework: <Carbon/Carbon.h>". This means you need to link the Carbon framework into your app and then #import <Carbon/Carbon.h>
Dave DeLong
2009-07-12 17:04:41
Just put that line of code in but it doesn't seem to change anything. Here's a picture of it http://snapplr.com/zgmf.
Joshua
2009-07-13 05:49:11
How How come I still get these errors even though I have imported the correct frameworks?
Joshua
2009-07-17 18:24:09
+1
A:
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?
Pupsor
2009-07-16 00:32:13
http://www.pupsor.com/wp-content/uploads/2009/07/Joshua.zip it's archive of project with example working showing. Try that and send result
Pupsor
2009-07-18 05:02:43
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. http://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.
Joshua
2009-07-18 07:21:52
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];
Pupsor
2009-07-18 17:26:56
Thanks, I just added that in but it doesn't seem to change anything, Do you know why?
Joshua
2009-07-19 06:33:13
Hmmm, i've just checked it - it works. New version of joshua_1.1.zip there http://www.pupsor.com/wp-content/uploads/2009/07/joshua_1.1.zipcheck this and make answer. does it work for you?
Pupsor
2009-07-19 17:43:40
Just checked it and it still doesn't work, I know why because on the second sample project you sent me it wasn't set as LSUIElement. When it is in mine and the first project you sent me.
Joshua
2009-07-19 17:58:31
Make sure at your end on the new test app you sent it is set as LSUIElement, if it is when the App is launched there will be no dock icon, the show button should work and you won't see the apps name in the Menubar. If it is not LSUIElement everything will work like a normal app and the Show button will do nothing.
Joshua
2009-07-20 10:59:27