Mac Preferences and About Menu

From Free Pascal wiki
Revision as of 12:04, 3 July 2019 by Trev (talk | contribs) (Expanded content and updated out of date images)
Jump to navigationJump to search
Stock-dialog-warning.svg

This article applies to Mac OS X only.

See also: Multiplatform Programming Guide

There are certain items that should appear in the default application menu on the macOS menu bar of your application. While Lazarus does automatically add:

  • Services
  • Hide MyApp
  • Hide Others
  • Show All
  • Quit MyApp

Lazarus 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.

Add 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..."

MenuItems3new.jpg

Assign 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:

MenuItemsGrab.png

Finally, 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:

     procedure TForm1.FormCreate(Sender: TObject);
     begin
          MenuItem1.Caption := #$EF#$A3#$BF;  //Unicode Apple logo char
     end;

After 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.

MenuItems4new.png

Did you notice the problem? Do you really want to call your application "MyApp" and not "My App"?

No, 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".

The 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".

Of course you would also now need to change the "About MyApp" caption too :-)

Finished? 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:

     procedure TForm1.FormCreate(Sender: TObject);
     begin
          Application.Title := 'My App';
          MenuItem1.Caption := #$EF#$A3#$BF;  //Unicode Apple logo char
     end;

And here is the final result:

macOSMenuFinal.png


See also