Sign in Join
Wiki copytemporary

macOS Application Dock Menu: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Overview
22
33Mac OS X v10.1 (Puma) added the ability to add custom menu items to an application's dock icon. Dock items like Options, Show All Windows, Hide ands Quit are provided automatically by the dock, so all we need to do is add our custom items. By adding menu items to your application's dock icon, you can provide the user with quick access to commonly used features.
44
55Another possibility is modifying the application's dock menu while the application is running to produce a dynamic menu with, for example, a status update. This is easily done by not assigning an action selector when creating the menu item. This also greys out the menu item so that it cannot be selected. There is an example included in the example code below.
66
77## Example code
88
99``` pascal
1010unit Unit1;
1111
1212{$mode objfpc}{$H+}
1313{$modeswitch objectivec1}
1414
1515interface
1616
1717uses
1818 Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
1919 CocoaAll;
2020
2121type
2222
2323 { TForm1 }
2424
2525 TForm1 = class(TForm)
2626 Button1: TButton;
2727 procedure Button1Click(Sender: TObject);
2828 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
2929 procedure FormCreate(Sender: TObject);
3030 private
3131
3232 public
3333
3434 end;
3535
3636 TMyAppDelegate = objcclass(NSObject)
3737 private
3838
3939 public
4040 function applicationDockMenu (sender: NSApplication): NSMenu;
4141 message 'applicationDockMenu:';
4242 procedure menuItemHello1;
4343 message 'menuItemHello1';
4444 procedure menuItemHello2;
4545 message 'menuItemHello2';
4646 end;
4747
4848var
4949 Form1: TForm1;
5050 myAppDelegate: TMyAppDelegate;
5151
5252implementation
5353
5454{$R *.lfm}
5555
5656{ TForm1 }
5757
5858procedure TMyAppDelegate.menuItemHello1;
5959begin
6060 NSLog(NSStr('hello menu item 1'));
6161end;
6262
6363procedure TMyAppDelegate.menuItemHello2;
6464begin
6565 NSLog(NSStr('hello menu item 2'));
6666end;
6767
6868function TMyAppDelegate.applicationDockMenu(sender: NSApplication): NSMenu;
6969var
7070 aSel, bSel: SEL;
7171 myMenu: NSMenu;
7272 myMenuItem0: NSMenuItem;
7373 myMenuItem1: NSMenuItem;
7474 myMenuItem2: NSMenuItem;
7575begin
7676 NSLog(NSStr('dock menu'));
7777
7878 aSel := ObjCSelector(TMyAppDelegate.menuItemHello1);
7979 bSel := ObjCSelector(TMyAppDelegate.menuItemHello2);
8080
8181 myMenu := NSMenu.alloc.init.autorelease;
8282 myMenuItem0 := NSMenuItem.alloc.initWithTitle_action_keyEquivalent(NSStr('My Dock Menu'), Nil, NSStr('')).autorelease;
8383 myMenuItem1 := NSMenuItem.alloc.initWithTitle_action_keyEquivalent(NSStr('Hello Item 1'), aSel, NSStr('')).autorelease;
8484 myMenuItem2 := NSMenuItem.alloc.initWithTitle_action_keyEquivalent(NSStr('Hello Item 2'), bSel, NSStr('')).autorelease;
8585 myMenu.addItem(myMenuItem0);
8686 myMenu.addItem(myMenuItem1);
8787 myMenu.addItem(myMenuItem2);
8888 Result := myMenu;
8989end;
9090
9191procedure TForm1.Button1Click(Sender: TObject);
9292begin
9393 NSLog(NSStr('Closing'));
9494 Close;
9595end;
9696
9797procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
9898begin
9999 NSLog(NSStr('Leaving...'));
100100
101101 if(NSApp.isActive) then
102102 begin
103103 NSLog(NSStr('exiting'));
104104 exit;
105105 end
106106 else
107107 // needed if any part of form is occluded or does not quit
108108 // until dock icon clicked when quitting via dock menu
109109 begin
110110 NSLog(NSStr('activating'));
111111 Application.BringToFront;
112112 CloseAction := TCloseAction.caFree;
113113 end;
114114end;
115115
116116procedure TForm1.FormCreate(Sender: TObject);
117117begin
118118 // NSApp
119119 // - The global variable for the shared application instance.
120120 // NSApplication.sharedApplication
121121 // - Returns the application instance, creating it if it doesn’t exist yet.
122122 NSApp := NSApplication.sharedApplication;
123123 NSApp.setDelegate(myAppDelegate);
124124end;
125125
126126Initialization
127127 myAppDelegate := TMyAppDelegate.alloc.init;
128128
129129Finalization
130130 myAppDelegate.Release;
131131end.
132132```
133133
134134Typical log output when the executable is started from an Applications \> Utilities \> Terminal and the two added menu items are clicked before using the dock menu to quit:
135135
136-`trev@macmini7 [/Users/trev/Programming/LAZARUS/laz_dockmenu] $ ./project1.app/Contents/MacOS/project1`\
137-`2021-03-02 19:35:58.366 project1[1483:110782] dock menu`\
138-`2021-03-02 19:36:05.700 project1[1483:110782] dock menu`\
139-`2021-03-02 19:36:10.862 project1[1483:110782] hello menu item 1`\
140-`2021-03-02 19:36:12.644 project1[1483:110782] dock menu`\
141-`2021-03-02 19:36:14.360 project1[1483:110782] hello menu item 2`\
142-`2021-03-02 19:36:15.915 project1[1483:110782] dock menu`\
143-`2021-03-02 19:36:17.061 project1[1483:110782] Leaving...`\
144-`2021-03-02 19:36:17.061 project1[1483:110782] activating`
136+```text
137+trev@macmini7 [/Users/trev/Programming/LAZARUS/laz_dockmenu] $ ./project1.app/Contents/MacOS/project1
138+2021-03-02 19:35:58.366 project1[1483:110782] dock menu
139+2021-03-02 19:36:05.700 project1[1483:110782] dock menu
140+2021-03-02 19:36:10.862 project1[1483:110782] hello menu item 1
141+2021-03-02 19:36:12.644 project1[1483:110782] dock menu
142+2021-03-02 19:36:14.360 project1[1483:110782] hello menu item 2
143+2021-03-02 19:36:15.915 project1[1483:110782] dock menu
144+2021-03-02 19:36:17.061 project1[1483:110782] Leaving...
145+2021-03-02 19:36:17.061 project1[1483:110782] activating
146+```
145147
146148Alternatively, you can view the output using the Applications \> Utilities \> Console.
147149
148150## See also
149151
150152- [[Apple-specific UI elements]]
151153- [[Application disable resize over Dock]]
152154- [[Bounce Application Icon in Dock]]
153155- [[Hiding a macOS app from the Dock]]
154156- [[macOS NSStatusBar]]
155157- [[Show Badge on Application Icon in Dock]]
156158
157159## External links
158160
159161- [Apple: NSApplication](https://developer.apple.com/documentation/appkit/nsapplication).
160162- [Apple: NSMenu](https://developer.apple.com/documentation/appkit/nsmenu).
161163- [Apple: NSMenuItem](https://developer.apple.com/documentation/appkit/nsmenuitem).
162164- [Apple: applicationDockMenu:](https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428564-applicationdockmenu).
163165
164166---
165167*From the Free Pascal wiki, page "macOS Application Dock Menu", as it stood on 2021-12-15 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history