Mac OS X - app without menu? - cocoa

I'd like to build an app that does not have a menubar, a dock icon, or sits in the app switcher. Basically, it should be like Quicksilver: I'd active it through a global hot key, say from Safari, and a little window appears, but Safari does not get inactive, neither does a different menubar show. I hope you understand what I mean...
How would I do that? I can prevent the dock icon, the app switcher, but I do not know how I can prevent the other apps from becoming inactive when my app's window shows or how I can remove the menu.
Thanks for any hints!

Try searching for "LSUIElement". That should give you all the information you need.
(Specifically, this page in the documentation).

As Dave already said, add
LSUIElement YES
in your application's Info.plist file. That will get rid of icon and menu bar.
Then, to actually bring a window to the front at the appropriate time (e.g. when triggered through a global keyboard shortcut), you could do something like this:
ProcessSerialNumber psn = {0, kCurrentProcess};
SetFrontProcess(&psn);
[someWindow makeKeyAndOrderFront:nil];

Related

Cocoa : Repport click on dock icon

Is there a way to report every mouse click on the application dock icon?
Not completely safe (also activated by double-click on the application itself),
but definitely the most easy way to implement:
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
Quote from NSApplicationDelegate Protocol Reference:
These events are sent whenever the Finder reactivates an already running application because someone double-clicked it again or used the dock to activate it.
I would like to suggest an alternative solution to the answer provided by Anne, which avoids conflicting with the event in which the user double clicks on the application icon, instead of on the dock icon.
Thus, I suggest to use
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender;
See also the Apple's documentation.

OS X app that works without being active?

Recently i've tried ColorSnapper app, and I've noticed that it works keeping other applications active.
(Check the screen below, as you can see safari window is still active and ColorSnapper keeps being active too).
How can is possible reproduce this effect ?
I believe these types of apps use LSBackgroundOnly or LSUIElement as Launch Services keys in their plist files (reference).
Here's a bit more about it.
I just try it and LSBackgroundOnly is the solution, but you must set the level of the window.
Example :
[mySpecialNSWindow setLevel:NSMainMenuWindowLevel];
This will display the special window above the windows of other applications.
I think the right approach is a mixture of (1) making the app LSBackgroundOnly, (2) using a custom transparent window as described here and set its level to NSFloatingWindowLevel, (3) using something like this in your app delegate to monitor mouse movements though your app is not active and, for example, to let your window follow the mouse position:
[NSEvent addGlobalMonitorForEventsMatchingMask:NSMouseMovedMask handler:^(NSEvent *event) {
[window setFrameOrigin:[NSEvent mouseLocation]];
}];
You can then have views as you like in the (transparent) window, move them around and modify its contents according to the mouse position.

Background application with menu bar

I have an application which I would like to hide from the dock. Therefore Application is agent (UIElement) to YES.
Still, I would like to show a window to the user and therefore I would like to show a menu bar and have the window shown in the task switcher (CMD+Tab). Is there any way to do this?
Not as such, no. That's part of the meaning of UIElement. It can show windows, but not have a menu bar nor an icon in the Dock or application switcher (which is run by the Dock).
You can transform a UIElement to a normal application using -[NSApplication setActivationPolicy:] but not back again.
Your UIElement could launch a helper application to present the GUI, which would then quit when it's done. That might achieve the effect you're looking for, but will of course be more complicated.
I wouldn't do this. It's not the apple way and just gets confusing.
I guess NSMenuItem would be a good way to solve this.

NSStatusItem menu does not show up in lion full screen app

I have a foreground application that shows a NSStatusItem along with a menu (via NSStatusItem setMenu:(NSMenu *)menu). However, this menu does not display when I am looking at another app in fullscreen mode (say Safari) in Lion.
I know that I can make it work by setting NSBGOnly to true in the Info.plist file (or NSUIElement), but both methods will make my app icon disappear from the task switcher as well as hide the main menu once I manage to focus my app.
Finally, I have tried setting NSUIElement to true and do the following in my app upon startup (see also How to hide the Dock icon):
ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
This made the menubar appear again as well as the dock icon but the original problem (status item menu does not show up in another fullscreen app) is visible again. Whatever I try, I can't win.
Any suggestions would be highly appreciated!
Unfortunately I think this is expected behaviour. Your app is considered a foreground app, so all its UI is disabled while another app is in full screen. You should file a bug if you feel that status items in foreground apps should still be available to other apps in full screen mode.
Probably the best solution would be to split your app into two parts, an agent app which has LSUIElement set to true, which creates and manages the status item and its menu, and your main foreground app which does most of the work and which launches and manages the agent app.
There are a variety of inter-process communication methods that you can use to get the two apps talking to each other, such as Distributed Objects or Apple Events.

bring the application from in focus, by clicking the icon of corresponding application

I was surprised this doesn't happen automatically, but I would like my applications window to be in focus as I click its dock icon, when in minimized mode.
Just to clarify, when I minimize the app, the window goes to dock, but when I click the its corresponding Dock Icon, the window don't come in focus.
Is there anything I am missing?
I am using Qt 4.5.3 on Mac OS X 10.5, 10.6
Thanks for help.
Rahul
First answer: That's the expected behavior of a Mac app. Try Safari for example. An app can be active without showing any window. In that case, only the menu bar at the top shows that the active app is changed. So, unless absolutely necessary, you shouldn't bring the minimized window back unless the user explicitly does so. That's the Mac way!
Second answer: I understand that there are cases where you want to bring the minimized window up. In Cocoa, the application delegate method -applicationDidBicomeActive is called when the application gets the focus, and there you can bring the window up yourself. I'm sure Qt also has a similar event/callback/signal or whatever, but I don't know any Qt ... :p Sorry I can't be of any help.

Resources