简体   繁体   中英

How to make a Finder-like Dock menu?

When a user clicks on a Dock icon with the right mouse button (Or a command button + mouse click) on a running application - he can see a Dock menu. Which usually consists of 3 parts:

http://i55.tinypic.com/i1for6.png

a list of all opened documents by this application (red), a custom application's part of the menu (yellow) and default items that are added implicitly to all the items (blue).

Here apple explains that we can define these custom items (yellow) by implementing an application delegate's method ‑(NSMenu *)applicationDockMenu:(NSApplication *)sender , which should return a valid menu (or by defining this Dock menu in Interface Builder).

If you try to open a dock menu for Finder, you can see an unusual menu:

不寻常的菜单

It is unusual because it does not have ordinary items for quitting and "Options" submenu - as any other launched application would have had by default. Instead it has only "Hide".

I am pretty sure an application or a nib file can override this default "system" (blue) part of a Dock menu. Maybe anyone has any idea how that can be achieved?

I am developing a security application that should not be allowed to quit or selected to be launched "at Login", but it should run all the time when the user is logged in (just like Finder, it may be halted only using Force application to Quit or Activity monitor). I have good reasons to want to get rid of it. I am aware that this is not exactly a Mac OS way, but this is not exactly an ordinary application. Have anyone done anything similar?

Thank you

The Dock menus are actually put up by the Dock process itself and it just proxies each application's dockMenus, so you can't do anything directly about it.

What would work is: install a Quartz Event Tap, intercept clicks routed to the Dock process, check if it's on your own app's icon, and then put up your own custom menu. Tricky, and not possible at all for a sandboxed app.

It's pretty simple: create a menu in the nib file, and then connect NSApplication 's dockMenu outlet to that menu. You can find more information here .

Or you can add menu items to the existing menu via code. You can use -[NSApplication applicationDockMenu:] to get the existing dock menu and add items programmatically.

The Finder's menu is special because the Finder itself is special: it cannot be quit, it must remain in the Dock at all times, it must be opened on login, and you cannot reveal the location of the Finder in the Finder.

What you're looking for simply does not exist. There is no way to do this. No application can override this area.

Instead of messing with the dock menu, can you just add the LSUIElement key set to YES to your app's Info.plist file? This will stop your app from appearing in the dock at all.

It will, however, also prevent it from having a menu bar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM