Mac Show Application Title, Version, and Company

From Lazarus wiki
Revision as of 13:40, 19 March 2021 by Trev (talk | contribs) (Add simple way to create Cocoa Framework About Box)
Jump to navigationJump to search
macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

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

Warning-icon.png

Warning: This only works if the software has an application bundle; otherwise please see Show Application Title, Version, and Company

For those who want to show the Application Title, Version, and Company for an application on macOS in an About Box, this can be done the complex way or the simple way.

The complex way

Beware CFBundleGetMainBundle does not really return nil if the application has no bundle. Instead it tries to create that handle. See Apple's documentation. So we should check for the existence of ValueRef too.

// CODE FOR SHOWING APPLICATION TITLE, VERSION, AND COMPANY
uses MacOSAll, CarbonProc, StrUtils;

var
  BundleID: String;
  BundleName: String;
  BundleRef: CFBundleRef;
  BundleVer: String;
  CompanyName: String;
  KeyRef: CFStringRef;
  ValueRef: CFTypeRef;

function GetInfoPlistString(const KeyName : string) : string;
begin
  try
    Result := '';
    BundleRef := CFBundleGetMainBundle;
    if BundleRef = nil then Exit;  {Executable not in an app bundle?}
    KeyRef := CFStringCreateWithPascalString(nil,KeyName,kCFStringEncodingUTF8);
    ValueRef := CFBundleGetValueForInfoDictionaryKey(BundleRef, KeyRef);
    if ValueRef = nil then Exit;  {Executable not in an app bundle!}
    if CFGetTypeID(ValueRef) <> CFStringGetTypeID then Exit;  {Value not a string?}
    Result := CFStringToStr(ValueRef);
  except
  on E : Exception do
    ShowMessage(E.Message);
  end;
  FreeCFString(KeyRef);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   try
     Form1.Caption := 'About '+Application.Title;
     StaticTextAppTitle.Caption := Application.Title;
     BundleID := GetInfoPlistString('CFBundleIdentifier');
     '''// CompanyName is presumed to be in the form of: com.Company.AppName'''
     CompanyName := AnsiMidStr(BundleID,AnsiPos('.',BundleID)+1,Length(BundleID));
     CompanyName := AnsiMidStr(CompanyName,0,AnsiPos('.',CompanyName)-1);
     BundleVer := GetInfoPlistString('CFBundleVersion');
     StaticTextAppVer.Caption := Application.Title+' version '+BundleVer;
     StaticTextCompany.Caption := CompanyName;
   except
   on E : Exception do
          ShowMessage(E.Message);
   end;
end;

Sample output:

About1.png

The simple way

Thanks to the wonders of the Apple Cocoa Framework, the easy way to create an About Box for an application comprises just one line of code, one unit added to the Uses clause and one HTML file added to your Application Bundle Resources directory.

...

Uses
  CocoaAll,
  ...;

  ...
  NSApp.orderFrontStandardAboutPanel(Nil);
  ...

...

Sample output:

Easy macOS About Box.jpg

Where does this information come from? Your Info.plist property list file provides the following details:

  • Application Icon

Uses the string value of the CFBundleIconFile key. You don't have to include the filename extension in this string value, but you can if you want. If you do not, macOS is smart enough to search for a file of this name that ends with the .icns extension. If not available, the generic application icon is used.

  • Application Name

Uses the string value of the CFBundleName key which is localisable. If not found, the system uses the NSProcessInfo.processInfo.processName (ie the name of your executable). This string is what is displayed in the menu bar when your application is active.

  • Application Version String

Uses the value of the CFBundleShortVersionString key. If not found, the build version, if available, is printed alone as "Version x.x.x". This string is useful for actual version identification, but can be thought of as the "marketing" version. As a result, it is typically a more general version number, such as 1.0, and can contain alpha characters with no ill side effects. This value is displayed in the Finder preview too.

  • Build Version String

Uses the value of the CFBundleVersion key. Displays the build version number of the application displayed as "(v1.0.0)". If not found, the version area is left blank (the "(v)" is not displayed).

  • Copyright

Uses the string value of the NSHumanReadableCopyright key from the Info.plist file. If not found, the copyright area is left blank.

The Credits section comes from a file named Credits.html in the Resources directory of your bundle. If this file is not found, the system then looks for a file named Credits.rtf. If not found, the system looks for a file named Credits.rtfd. If not found, the credits area is left blank. Note that the capitalization of the file name is important. By using the HTML format, you can include links if necessary (eg to your web site).

The Info.plist property list file used in the sample above was:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CFBundleDevelopmentRegion</key>
	<string>English</string>
	<key>CFBundleExecutable</key>
	<string>project1</string>
	<key>CFBundleName</key>
	<string>My Amazing Application</string>
	<key>CFBundleIdentifier</key>
	<string>com.company.project1</string>
	<key>CFBundleInfoDictionaryVersion</key>
	<string>6.0</string>
	<key>CFBundlePackageType</key>
	<string>APPL</string>
	<key>CFBundleSignature</key>
	<string>proj</string>
	<key>CFBundleShortVersionString</key>
	<string>1.0</string>
	<key>CFBundleVersion</key>
	<string>11</string>
	<key>CFBundleIconFile</key>
	<string>ss4200utility.icns</string>
	<key>CSResourcesFileMapped</key>
	<true/>
	<key>CFBundleDocumentTypes</key>
	<array>
		<dict>
			<key>CFBundleTypeRole</key>
			<string>Viewer</string>
			<key>CFBundleTypeExtensions</key>
			<array>
				<string>*</string>
			</array>
			<key>CFBundleTypeOSTypes</key>
			<array>
				<string>fold</string>
				<string>disk</string>
				<string>****</string>
			</array>
		</dict>
	</array>
	<key>NSHighResolutionCapable</key>
	<true/>
	<key>NSHumanReadableCopyright</key>
	<string>Copyright 2021, Your Full Name</string>
</dict>
</plist>

The HTML file used in the sample above was:

<center>
<p>
<h3>Credits</h3>
<p>
Free Pascal Team
<br/>
Lazarus Team
<br/>
Free Pascal and Lazarus Forums
<br/>
Free Pascal and Lazarus Wiki
<br/>
</center>

If the HTML text is short enough, no white scrollbox will appear and the text will appear on the default grey background.

See also