Sign in Join
Wiki copytemporary

Mac Preferences and About Menu: what changed

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

OldNewLine
11*(macOS only)*
22
33There are certain items that should appear in the default application menu on the macOS menu bar of your application. While Lazarus does automatically add:
44
55- Services
66- Hide MyApp
77- Hide Others
88- Show All
99- Quit MyApp
1010
1111Lazarus does not add an "About MyApp" menu item or a "Preferences..." menu item. To add these two items on the same application menu as Quit, you have to perform a little trick.
1212
1313Add a MainMenu component from the Standard tab of the component palette to your main form. Create a menu item and change its caption property in the Object Inspector to the name of your application just for reference. Then, under that menu add three sub-menu items with the following captions: About MyApp, "-" (a separator) and "Preferences..."
1414
15-<figure>
16-<img src="MenuItems3new.jpg" title="MenuItems3new.jpg" />
17-<figcaption>MenuItems3new.jpg</figcaption>
18-</figure>
15+![](/wiki-images/3/32/MenuItems3new.jpg)
1916
2017Assign the "Preferences..." Menu Item Shortcut "Command ," using the ShortCut Editor (choose the Menu item ShortCut property in the Object Inspector and click on the ellipsis dots next to it). After checking the Meta key, change the Unknown key to a comma, this is how your shortcut will appear:
2118
22-<figure>
23-<img src="MenuItemsGrab.png" title="MenuItemsGrab.png" />
24-<figcaption>MenuItemsGrab.png</figcaption>
25-</figure>
19+![](/wiki-images/5/50/MenuItemsGrab.png)
2620
2721Finally, to get these items on the MyApp application menu, we must change the caption of the first menu item in the main form's OnCreate event handler as follows:
2822
2923``` pascal
3024 procedure TForm1.FormCreate(Sender: TObject);
3125 var
3226 AppMenu : TMenuItem;
3327 begin
3428 AppMenu := TMenuItem.Create(Self); // Application menu
3529 AppMenu.Caption := #$EF#$A3#$BF; // Unicode Apple logo char
3630 MainMenu.Items.Insert(0, AppMenu);
3731 end;
3832```
3933
4034After recompiling your application and running it, you will notice that "About MyApp" and "Preferences..." have been added to the MyApp menu, which is where they should be.
4135
42-<figure>
43-<img src="MenuItems4new.png" title="MenuItems4new.png" />
44-<figcaption>MenuItems4new.png</figcaption>
45-</figure>
36+![](/wiki-images/0/05/MenuItems4new.png)
4637
4738Did you notice the problem? Do you really want to call your application "MyApp" and not "My App"?
4839
4940No, you cannot change the project file "program MyApp" to "program My App" or you are told "MyApp.lpr(1,11) Fatal: Syntax error, ";" expected but "identifier APP" found".
5041
5142The solution is to change your application Info.plist file by opening your application bundle, navigating to Contents, double-clicking on Info.plist and using the Xcode plist editor to change the value of the "Bundle name" key to the string "My App" and saving it. Now when you run "MyApp" it displays as "My App".
5243
5344Of course you would also now need to change the "About MyApp" caption too :-)
5445
5546Finished? No. The Lazarus-generated menu items still show "Hide MyApp" and "Quit MyApp". To change these to "My App" you need to modify the main form's OnCreate event handler as below:
5647
5748``` pascal
5849 procedure TForm1.FormCreate(Sender: TObject);
5950 var
6051 AppMenu : TMenuItem;
6152 begin
6253 Application.Title := 'My App';
6354 AppMenu := TMenuItem.Create(Self); // Application menu
6455 AppMenu.Caption := #$EF#$A3#$BF; // Unicode Apple logo char
6556 MainMenu.Items.Insert(0, AppMenu);
6657 end;
6758```
6859
6960And here is the final result:
7061
71-<figure>
72-<img src="macOSMenuFinal.png" title="macOSMenuFinal.png" />
73-<figcaption>macOSMenuFinal.png</figcaption>
74-</figure>
62+![](/wiki-images/9/92/MacOSMenuFinal.png)
7563
7664To connect the menu options to an action, add a TActionList to the main form, add actions for About and Preferences (edit the properties and then create the appropriate OnExecute event handlers for each action), and then modify the main form's OnCreate event handler as below:
7765
7866``` pascal
7967procedure TForm1.FormCreate(Sender: TObject);
8068 var
8169 AppMenu : TMenuItem;
8270 AppAboutCmd : TMenuItem;
8371 AppSepCmd : TMenuItem;
8472 AppPrefCmd : TMenuItem;
8573 begin
8674 Application.Title := 'My App';
8775
8876 AppMenu := TMenuItem.Create(Self); // Application menu
8977 AppMenu.Caption := #$EF#$A3#$BF; // Unicode Apple logo char
9078 MainMenu.Items.Insert(0, AppMenu);
9179
9280 AppAboutCmd := TMenuItem.Create(Self);
9381 AppAboutCmd.Action:= ActionList1.Actions[1];
9482 AppMenu.Add(AppAboutCmd); // Add About as item in application menu
9583
9684 AppSepCmd := TMenuItem.Create(Self);
9785 AppSepCmd.Caption := '-';
9886 AppMenu.Add(AppSepCmd); // Add menu separator
9987
10088 AppPrefCmd := TMenuItem.Create(Self);
10189 AppPrefCmd.Action:= ActionList1.Actions[2];
10290 AppMenu.Add(AppPrefCmd); // Add Preferences as item in application menu
10391
10492 AppSepCmd := TMenuItem.Create(Self);
10593 AppSepCmd.Caption := '-';
10694 AppMenu.Add(AppSepCmd); // Add menu separator
10795 end;
10896```
10997
11098## See also
11199
112100- [[Mac Show Application Title, Version, and Company|Mac About Box with one line of code]]
113101- [[Introduction to platform-sensitive development]]
114102- [[Mac Preferences Read and Write]]
115103- [[Mac Show Application Title, Version, and Company]]
116104- [[Add an Apple Help Book to your macOS app]]
117105- [[macOS Programming Tips]]
118106
119107---
120108*From the Free Pascal wiki, page "Mac Preferences and About Menu", as it stood on 2021-12-09 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