Difference between revisions of "Mac Preferences and About Menu"

From Free Pascal wiki
Jump to navigationJump to search
m (Added syntax highlighting wiki markup)
(Expanded content and updated out of date images)
Line 1: Line 1:
 
{{Platform only|Mac OS X|Mac OS X|Mac OS X}}
 
{{Platform only|Mac OS X|Mac OS X|Mac OS X}}
These items should appear on the default menu that is titled with an applications' name.
 
The problem is that Lazarus doesn't currently provide direct access to this auto-generated
 
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);
 +
    begin
 +
          MenuItem1.Caption := #$EF#$A3#$BF;  //Unicode Apple logo char
 +
    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"?
 +
 +
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".
  
Then, to get these items on the MyApp menu, we must name the first menu item as follows:
+
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">
 
<syntaxhighlight lang="pascal">
 
     procedure TForm1.FormCreate(Sender: TObject);
 
     procedure TForm1.FormCreate(Sender: TObject);
 
     begin
 
     begin
 +
          Application.Title := 'My App';
 
           MenuItem1.Caption := #$EF#$A3#$BF;  //Unicode Apple logo char
 
           MenuItem1.Caption := #$EF#$A3#$BF;  //Unicode Apple logo char
 
     end;  
 
     end;  
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Once your program is compiled and ran, you will notice that "About MyApp" and
+
And here is the final result:
"Preferences..." have been added to the MyApp menu, which
+
 
is where they should be.
+
[[File:macOSMenuFinal.png]]
  
[[File:MenuItems4.jpg]]
 
  
 
== See also ==
 
== See also ==
 +
 
*[[Introduction to platform-sensitive development]]
 
*[[Introduction to platform-sensitive development]]
 
*[[Mac Show Application Title, Version, and Company]]
 
*[[Mac Show Application Title, Version, and Company]]
 +
*[[Add an Apple Help Book to your macOS app]]
 +
*[[OS X Programming Tips]]
  
 
[[Category:Mac OS X]]
 
[[Category:Mac OS X]]
 
[[Category:Platform-sensitive development]]
 
[[Category:Platform-sensitive development]]

Revision as of 12:04, 3 July 2019

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