Difference between revisions of "Mac Preferences and About Menu"

From Free Pascal wiki
Jump to navigationJump to search
(→‎See also: Add entry)
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
These items should appear on the default menu that is titled with an applications' name.
+
{{Platform only|macOS}}
The problem is that Lazarus doesn't currently provide direct access to this auto-generated
+
{{Mac Preferences and About Menu}}
menu, nor does it add these two items to it automatically.  To get them on the
 
same menu as Quit, you have to perform a little trick.
 
  
Create a menu item and name it your apps name, just for reference.
+
There are certain items that should appear in the default application menu on the macOS menu bar of your application.
Then, under that menu add the following:
+
While Lazarus does automatically add:
  
[[File:MenuItems3.jpg]]
+
* 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.
  
Assign the "Preferences..." Menu Item Shortcut "Command ," using the "Grab key" button.
+
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..."
  
[[File:MenuItems.jpg]]
+
[[File: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:
  
After you've pressed the "Command ," using "Grab key", this is how your shortcut will appear:
+
[[File:MenuItemsGrab.png]]
  
[[File:MenuItems2.jpg]]
+
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:
  
 +
<syntaxhighlight lang="pascal">
 +
    procedure TForm1.FormCreate(Sender: TObject);
 +
    var
 +
          AppMenu : TMenuItem;
 +
    begin
 +
          AppMenu := TMenuItem.Create(Self); // Application menu
 +
          AppMenu.Caption := #$EF#$A3#$BF;  // Unicode Apple logo char
 +
          MainMenu.Items.Insert(0, AppMenu);
 +
    end;
 +
</syntaxhighlight>
 +
 +
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.
 +
 +
[[File:MenuItems4new.png]]
 +
 +
Did you notice the problem? Do you really want to call your application "MyApp" and not "My App"?
  
Then, to get these items on the MyApp menu, we must name the first menu item as follows:
+
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:
 +
 +
<syntaxhighlight lang="pascal">
 
     procedure TForm1.FormCreate(Sender: TObject);
 
     procedure TForm1.FormCreate(Sender: TObject);
 +
    var
 +
          AppMenu : TMenuItem;
 
     begin
 
     begin
           MenuItem1.Caption := #$EF#$A3#$BF; //Unicode Apple logo char
+
           Application.Title := 'My App';
 +
          AppMenu := TMenuItem.Create(Self); // Application menu
 +
          AppMenu.Caption := #$EF#$A3#$BF;   // Unicode Apple logo char
 +
          MainMenu.Items.Insert(0, AppMenu);
 
     end;  
 
     end;  
 +
</syntaxhighlight>
 +
 +
And here is the final result:
 +
 +
[[File:macOSMenuFinal.png]]
 +
 +
To 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:
 +
 +
<syntaxhighlight lang="pascal">
 +
procedure TForm1.FormCreate(Sender: TObject);
 +
    var
 +
          AppMenu    : TMenuItem;
 +
          AppAboutCmd : TMenuItem;
 +
          AppSepCmd  : TMenuItem;
 +
          AppPrefCmd  : TMenuItem;
 +
    begin
 +
          Application.Title := 'My App';
 +
 +
          AppMenu := TMenuItem.Create(Self); // Application menu
 +
          AppMenu.Caption := #$EF#$A3#$BF;  // Unicode Apple logo char
 +
          MainMenu.Items.Insert(0, AppMenu);
 +
 +
          AppAboutCmd := TMenuItem.Create(Self);
 +
          AppAboutCmd.Action:= ActionList1.Actions[1];
 +
          AppMenu.Add(AppAboutCmd);  // Add About as item in application menu
  
Once your program is compiled and ran, you will notice that "About MyApp" and
+
          AppSepCmd := TMenuItem.Create(Self);
"Preferences..." have been added to the MyApp menu, which
+
          AppSepCmd.Caption := '-';
is where they should be.
+
          AppMenu.Add(AppSepCmd);  // Add menu separator
 +
 
 +
          AppPrefCmd := TMenuItem.Create(Self);
 +
          AppPrefCmd.Action:= ActionList1.Actions[2];
 +
          AppMenu.Add(AppPrefCmd);  // Add Preferences as item in application menu
 +
 
 +
          AppSepCmd := TMenuItem.Create(Self);
 +
          AppSepCmd.Caption := '-';
 +
          AppMenu.Add(AppSepCmd);  // Add menu separator
 +
    end;
 +
</syntaxhighlight>
  
[[File:MenuItems4.jpg]]
 
  
 
== See also ==
 
== See also ==
 +
 +
*[[Mac Show Application Title, Version, and Company|Mac About Box with one line of code]]
 
*[[Introduction to platform-sensitive development]]
 
*[[Introduction to platform-sensitive development]]
 
+
*[[Mac Preferences Read and Write]]
[[Category:Mac OS X]]
+
*[[Mac Show Application Title, Version, and Company]]
[[Category:Platform-sensitive development]]
+
*[[Add an Apple Help Book to your macOS app]]
 +
*[[macOS Programming Tips]]

Latest revision as of 06:04, 9 December 2021

macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

English (en) русский (ru)

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);
     var
          AppMenu : TMenuItem;
     begin
          AppMenu := TMenuItem.Create(Self); // Application menu
          AppMenu.Caption := #$EF#$A3#$BF;   // Unicode Apple logo char
          MainMenu.Items.Insert(0, AppMenu);
     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);
     var
          AppMenu : TMenuItem;
     begin
          Application.Title := 'My App';
          AppMenu := TMenuItem.Create(Self); // Application menu
          AppMenu.Caption := #$EF#$A3#$BF;   // Unicode Apple logo char
          MainMenu.Items.Insert(0, AppMenu);
     end;

And here is the final result:

macOSMenuFinal.png

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

procedure TForm1.FormCreate(Sender: TObject);
     var
          AppMenu     : TMenuItem;
          AppAboutCmd : TMenuItem;
          AppSepCmd   : TMenuItem;
          AppPrefCmd  : TMenuItem;
     begin
          Application.Title := 'My App';

          AppMenu := TMenuItem.Create(Self); // Application menu
          AppMenu.Caption := #$EF#$A3#$BF;   // Unicode Apple logo char
          MainMenu.Items.Insert(0, AppMenu);

          AppAboutCmd := TMenuItem.Create(Self);
          AppAboutCmd.Action:= ActionList1.Actions[1];
          AppMenu.Add(AppAboutCmd);  // Add About as item in application menu

          AppSepCmd := TMenuItem.Create(Self);
          AppSepCmd.Caption := '-';
          AppMenu.Add(AppSepCmd);   // Add menu separator

          AppPrefCmd := TMenuItem.Create(Self);
          AppPrefCmd.Action:= ActionList1.Actions[2];
          AppMenu.Add(AppPrefCmd);   // Add Preferences as item in application menu

          AppSepCmd := TMenuItem.Create(Self);
          AppSepCmd.Caption := '-';
          AppMenu.Add(AppSepCmd);   // Add menu separator
     end;


See also