Mac Show Application Title, Version, and Company/ru

From Lazarus wiki
Revision as of 14:43, 18 August 2020 by Zoltanleo (talk | contribs) (Created page with "{{Platform only|macOS}} {{LanguageBar}} {{Warning|This only works '''if''' the software has an application bundle; otherwise please see Show Application Title, Version, and...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
macOSlogo.png

Эта статья относится только к macOS.

См. также: Multiplatform Programming Guide

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

Warning-icon.png

Предупреждение: This only works if the software has an application bundle; otherwise please see Show Application Title, Version, and Company

Для тех, кто хочет показать название приложения, версию и компанию для приложения в Mac OSX, это можно сделать с помощью следующего метода.

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

See also