Sign in Join
Wiki copytemporary

Mac Show Application Title, Version, and Company/ru

English · Русский

(macOS only)

Warning: Это работает, только если у программного обеспечения есть пакет приложений; в противном случае см. пожалуйста Show Application Title, Version, and Company

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

Осторожно CFBundleGetMainBundle на самом деле не возвращает nil, если у приложения нет пакета. Вместо этого он пытается создать этот дескриптор. См. документацию Apple. Таким образом, мы должны также проверить наличие ValueRef.

pascal
// КОД ДЛЯ ПОКАЗА НАЗВАНИЯ, ВЕРСИИ И КОМПАНИИ ПРИЛОЖЕНИЯ
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');
     '''// Предполагается, что имя компании имеет формат: 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;

Пример вывода:

См.также


From the Free Pascal wiki, page "Mac Show Application Title, Version, and Company/ru", as it stood on 2020-08-18 after a change by Zoltanleo, 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.

Revision 2, last changed by Zoltanleo .

Edited on wiki.freepascal.org by 1 person, 2020

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
ZoltanleoCreated page
Zoltanleo
Zoltanleo
Zoltanleo

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.