views:

459

answers:

3

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
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
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
I'll take a look thanks.
Joshua
+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
So If I put that code in my App Delegate and connect a checkbox to a User Defaults Controller. Will it work?
Joshua
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
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
How would I make the app an LSUIElement in the plist?
Joshua
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
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
@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
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
How would I import the Headers?
Joshua
How would I import the Headers?
Joshua
@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
Thanks, I didn't see that.
Joshua
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
Another Picture http://snapplr.com/jh3d .
Joshua
How How come I still get these errors even though I have imported the correct frameworks?
Joshua
+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
Isn't that exactly the same as the Code in the other answer?
Joshua
It comes up with same errors anyway, http://snapplr.com/jh3d.
Joshua
Why am I getting those errors even when I have imported the frameworks?
Joshua
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
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
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
Thanks, I just added that in but it doesn't seem to change anything, Do you know why?
Joshua
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
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
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