Sign in Join
Wiki copytemporary

Mac Show Application Title, Version, and Company: what changed

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

OldNewLine
11*(macOS only)*
22
33> **Warning:** This only works **if** the software has an application bundle; otherwise please see [[Show Application Title, Version, and Company]]
44
55An About window, also called an About Box or About Panel, is an optional window that contains your application’s version and copyright information and may also optionally include credits acknowledging people and code you did not write. Unlike other windows, an About window combines some of the behaviors of panels and windows: Like a panel, an About window is not listed in the application’s Window menu and like a window, it remains visible when the application is inactive. An About window should be modeless so the user can leave it open and perform other tasks in the application.
66
77Give your macOS application a professional look by using an About window which can be created in one of several different ways: the complex way, the simple way or the overridden simple way. Read on...
88
99## The simple way
1010
1111Thanks 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.
1212
1313``` pascal
1414...
1515
1616Uses
1717 CocoaAll,
1818 ...;
1919
2020 ...
2121 NSApp.orderFrontStandardAboutPanel(Nil);
2222 ...
2323
2424...
2525```
2626
2727Sample output:
2828
29-<figure>
30-<img src="Easy_macOS_About_Box.jpg" title="Easy_macOS_About_Box.jpg" />
31-<figcaption>Easy_macOS_About_Box.jpg</figcaption>
32-</figure>
29+![](/wiki-images/1/11/Easy_macOS_About_Box.jpg)
3330
3431Where does this information come from? Your `Info.plist` [[macOS property list files|property list file]] provides the following details:
3532
3633- **Application Icon**
3734
3835Uses 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.
3936
4037- **Application Name**
4138
4239Uses 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.
4340
4441- **Application Version String**
4542
4643Uses 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.
4744
4845- **Build Version String**
4946
5047Uses 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).
5148
5249- **Copyright**
5350
5451Uses the string value of the `NSHumanReadableCopyright` key from the Info.plist file. If not found, the copyright area is left blank.
5552
5653The **Credits section** comes from a file named `Credits.html` in the Resources directory of your [[Application Bundle|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).
5754
5855The `Info.plist` [[macOS property list files|property list file]] used in the sample above was:
5956
6057``` xml
6158<?xml version="1.0" encoding="UTF-8"?>
6259<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
6360<plist version="1.0">
6461<dict>
6562 <key>CFBundleDevelopmentRegion</key>
6663 <string>English</string>
6764 <key>CFBundleExecutable</key>
6865 <string>project1</string>
6966 <key>CFBundleName</key>
7067 <string>My Amazing Application</string>
7168 <key>CFBundleIdentifier</key>
7269 <string>com.company.project1</string>
7370 <key>CFBundleInfoDictionaryVersion</key>
7471 <string>6.0</string>
7572 <key>CFBundlePackageType</key>
7673 <string>APPL</string>
7774 <key>CFBundleSignature</key>
7875 <string>proj</string>
7976 <key>CFBundleShortVersionString</key>
8077 <string>1.0</string>
8178 <key>CFBundleVersion</key>
8279 <string>11</string>
8380 <key>CFBundleIconFile</key>
8481 <string>ss4200utility.icns</string>
8582 <key>CSResourcesFileMapped</key>
8683 <true/>
8784 <key>CFBundleDocumentTypes</key>
8885 <array>
8986 <dict>
9087 <key>CFBundleTypeRole</key>
9188 <string>Viewer</string>
9289 <key>CFBundleTypeExtensions</key>
9390 <array>
9491 <string>*</string>
9592 </array>
9693 <key>CFBundleTypeOSTypes</key>
9794 <array>
9895 <string>fold</string>
9996 <string>disk</string>
10097 <string>****</string>
10198 </array>
10299 </dict>
103100 </array>
104101 <key>NSHighResolutionCapable</key>
105102 <true/>
106103 <key>NSHumanReadableCopyright</key>
107104 <string>Copyright 2021, Your Full Name</string>
108105</dict>
109106</plist>
110107```
111108
112109The HTML file used in the sample above was:
113110
114111``` html
115-<center>
116-<p>
112+
117113<h3>Credits</h3>
118-<p>
114+
119115Free Pascal Team
120-<br/>
116+
121117Lazarus Team
122-<br/>
118+
123119Free Pascal and Lazarus Forums
124-<br/>
120+
125121Free Pascal and Lazarus Wiki
126-<br/>
127-</center>
122+
128123```
129124
130125If the HTML text is short enough, no white scrollbox will appear and the text will appear on the default grey background.
131126
132127Full project source code is available from [SourceForge](https://sourceforge.net/p/lazarus-wiki-projects/code/ci/master/tree/laz_about_boxes/).
133128
134129## The overridden simple way
135130
136131This is a variation on the simple way described above, but allows us to override some or all of the information which was previously extracted from the `Info.plist` file in the [[Application Bundle]].
137132
138133> **Note:** In versions of macOS before 10.13 (High Sierra) only the Copyright information can be overridden - from macOS 10.13 onwards, all of the information extracted from the `Info.plist` file can be overridden.
139134
140135``` pascal
141136...
142137{$mode objfpc}{$H+}
143138{$modeswitch objectivec1}
144139...
145140
146141Uses
147142 CocoaAll,
148143 ...;
149144
150145procedure TForm1.MenuItemAboutClick(Sender: TObject);
151146var
152147 dict: NSDictionary;
153148 val1: NSString;
154149 val2: NSString;
155150 val3: NSString;
156151 val4: NSString;
157152 val5: NSImage;
158153 key1: NSString;
159154 key2: NSString;
160155 key3: NSString;
161156 key4: NSString;
162157 key5: NSString;
163158begin
164159 key1 := NSStr('Copyright');
165160 val1 := NSStr('Copyright 2021, An Amazing Author');
166161 Key2 := NSStr('ApplicationName');
167162 val2 := NSStr('My Overriden Application');
168163 key3 := NSStr('Version');
169164 val3 := NSStr('Beta v1.29c');
170165 key4 := NSStr('ApplicationVersion');
171166 val4 := NSStr('Version 1.0.0');
172167
173168 key5 := NSStr('ApplicationIcon');
174169 val5 := NSImage.alloc.init;
175170 val5 := val5.imageNamed(NSStr('PICinfo.icns'));
176171
177172 dict := NSDictionary.alloc.initWithObjectsAndKeys(Val1, Key1, Val2, Key2, Val3, Key3, Val4, Key4, Val5, Key5, Nil);
178173
179174 NSApp.orderFrontStandardAboutPanelWithOptions(Dict);
180175
181176 dict.release;
182177 dict := Nil;
183178end;
184179```
185180
186181The sample output using the same `Credits.html` and `Info.plist` files from The Simple Way example above, plus the new icon file `PICinfo.icns` located in the Resources directory of the application bundle:
187182
188-<figure>
189-<img src="Overidden_Easy_macOS_About_Box.jpg" title="Overidden_Easy_macOS_About_Box.jpg" />
190-<figcaption>Overidden_Easy_macOS_About_Box.jpg</figcaption>
191-</figure>
183+![](/wiki-images/8/8f/Overidden_Easy_macOS_About_Box.jpg)
192184
193185Full project source code is available from [SourceForge](https://sourceforge.net/p/lazarus-wiki-projects/code/ci/master/tree/laz_about_boxes2/).
194186
195187## The complex way - now outdated
196188
197189**Beware** *CFBundleGetMainBundle* does not really return **nil** if the application has no bundle. Instead it tries to create that handle. See [Apple's documentation](https://developer.apple.com/documentation/corefoundation/1537085-cfbundlegetmainbundle?language=objc). So we should check for the existence of *ValueRef* too.
198190
199191``` pascal
200192// CODE FOR SHOWING APPLICATION TITLE, VERSION, AND COMPANY
201193uses MacOSAll, CarbonProc, StrUtils;
202194
203195var
204196 BundleID: String;
205197 BundleName: String;
206198 BundleRef: CFBundleRef;
207199 BundleVer: String;
208200 CompanyName: String;
209201 KeyRef: CFStringRef;
210202 ValueRef: CFTypeRef;
211203
212204// Retrieve information from the Info.plist file in the application bundle
213205function GetInfoPlistString(const KeyName : string) : string;
214206begin
215207 try
216208 Result := '';
217209 BundleRef := CFBundleGetMainBundle;
218210 if BundleRef = nil then Exit; {Executable not in an app bundle?}
219211 KeyRef := CFStringCreateWithPascalString(nil,KeyName,kCFStringEncodingUTF8);
220212 ValueRef := CFBundleGetValueForInfoDictionaryKey(BundleRef, KeyRef);
221213 if ValueRef = nil then Exit; {Executable not in an app bundle!}
222214 if CFGetTypeID(ValueRef) <> CFStringGetTypeID then Exit; {Value not a string?}
223215 Result := CFStringToStr(ValueRef);
224216 except
225217 on E : Exception do
226218 ShowMessage(E.Message);
227219 end;
228220 FreeCFString(KeyRef);
229221end;
230222
231223// Display the information in an About Box
232224procedure TForm1.FormCreate(Sender: TObject);
233225begin
234226 try
235227 Form1.Caption := 'About '+Application.Title;
236228 StaticTextAppTitle.Caption := Application.Title;
237229 BundleID := GetInfoPlistString('CFBundleIdentifier');
238230 // CompanyName is presumed to be in the form of: com.Company.AppName
239231 CompanyName := AnsiMidStr(BundleID,AnsiPos('.',BundleID)+1,Length(BundleID));
240232 CompanyName := AnsiMidStr(CompanyName,0,AnsiPos('.',CompanyName)-1);
241233 BundleVer := GetInfoPlistString('CFBundleVersion');
242234 StaticTextAppVer.Caption := Application.Title+' version '+BundleVer;
243235 StaticTextCompany.Caption := CompanyName;
244236 except
245237 on E : Exception do
246238 ShowMessage(E.Message);
247239 end;
248240end;
249241```
250242
251243Sample output:
252244
253-<figure>
254-<img src="About1.png" title="About1.png" />
255-<figcaption>About1.png</figcaption>
256-</figure>
245+![](/wiki-images/a/a7/About1.png)
257246
258247Note that Apple-compliant About Boxes should not contain a caption in the title bar. Oops.
259248
260249## See also
261250
262251- [[Mac Preferences and About Menu]]
263252- [[Show Application Title, Version, and Company]].
264253- [[Locating the macOS application resources directory]]
265254
266255## External links
267256
268257- [Apple: CFBundle Overview](https://developer.apple.com/documentation/corefoundation/cfbundle-s0a).
269258
270259- [Apple: orderFrontStandardAboutPanel](https://developer.apple.com/documentation/appkit/nsapplication/1428724-orderfrontstandardaboutpanel).
271260
272261- [Apple: orderFrontStandardAboutPanelWithOptions](https://developer.apple.com/documentation/appkit/nsapplication/1428479-orderfrontstandardaboutpanelwith).
273262
274263- [Apple: NSAboutPanelOptionKey](https://developer.apple.com/documentation/appkit/nsaboutpaneloptionkey).
275264
276265---
277266*From the Free Pascal wiki, page "Mac Show Application Title, Version, and Company", 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).*
278267Category: Code Snippets
279268Category: macOS

Back to history