Mac Show Application Title, Version, and Company

From Lazarus wiki
Revision as of 07:41, 22 July 2012 by Cringer000 (talk | contribs) (New Page about "Mac Show Application Title, Version, and Company")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

For those who want to show the Application Title, Version, and Company for an application on the Mac, this can be done using the following method:

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 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;

About1.png