Difference between revisions of "Mac Show Application Title, Version, and Company"
From Lazarus wiki
Jump to navigationJump to searchJwdietrich (talk | contribs) (Platform template updated.) |
(Described and fixed no-app-bundle detection.) |
||
Line 5: | Line 5: | ||
This only works '''if''' the application has an application bundle; otherwise please see [[Show Application Title, Version, and Company#Mac OS X and iOS]]. | This only works '''if''' the application has an application bundle; otherwise please see [[Show Application Title, Version, and Company#Mac OS X and iOS]]. | ||
+ | |||
+ | ---- | ||
+ | '''Beware!''' ''CFBundleGetMainBundle'' does not really return '''nil''' if application has no bundle. | ||
+ | |||
+ | Instead it tries to create that handle. See [[https://developer.apple.com/documentation/corefoundation/1537085-cfbundlegetmainbundle?language=objc Apple's documentation ]]. So we should check ''ValueRef'' existance too. | ||
<syntaxhighlight> | <syntaxhighlight> | ||
Line 27: | Line 32: | ||
KeyRef := CFStringCreateWithPascalString(nil,KeyName,kCFStringEncodingUTF8); | KeyRef := CFStringCreateWithPascalString(nil,KeyName,kCFStringEncodingUTF8); | ||
ValueRef := CFBundleGetValueForInfoDictionaryKey(BundleRef, KeyRef); | 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?} | if CFGetTypeID(ValueRef) <> CFStringGetTypeID then Exit; {Value not a string?} | ||
Result := CFStringToStr(ValueRef); | Result := CFStringToStr(ValueRef); |
Revision as of 14:44, 18 December 2018
This article applies to Mac OS X only.
See also: Multiplatform Programming Guide
Also see Show Application Title, Version, and Company.
For those who want to show the Application Title, Version, and Company for an application on Mac OSX, this can be done using the following method.
This only works if the application has an application bundle; otherwise please see Show Application Title, Version, and Company#Mac OS X and iOS.
Beware! CFBundleGetMainBundle does not really return nil if application has no bundle.
Instead it tries to create that handle. See [Apple's documentation ]. So we should check ValueRef existance 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: