Forum > macOS / Mac OS X

[SOLVED] Detect MacOS version

(1/2) > >>

ChrisR:
Prior to MacOS 11, the code below reported MacOS version correctly. It obviously will not work with MacOS 11, as the "10" is hard coded, but can anyone suggest the recommended backward compatible way to detect MacOS version.

Of reference, on MacOS 11.2.3, this program reports "MacOS: 10.16.3"

program ver;
{$mode objfpc}
uses Dos, sysutils;
//fpc ver.pas; ./ver
begin
   writeln('MacOS: 10.' + IntToStr(Lo(DosVersion) - 4) + '.' + IntToStr(Hi(DosVersion))  );
end.

Hansaplast:

The way I do it ... and maybe there is a better way ... but here an example project (just a TForm with 2 TLabels).




Don't forget:
- {$modeswitch objectivec1}
- uses CocoaAll



--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1;  {$mode objfpc}{$H+}{$modeswitch objectivec1}interface  uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, CocoaAll;  type    { TForm1 }    TForm1 = class(TForm)    Label1: TLabel;    Label2: TLabel;    procedure FormCreate(Sender: TObject);  private    public    end;  var  Form1: TForm1;  implementation  {$R *.lfm}  { TForm1 }  procedure TForm1.FormCreate(Sender: TObject);begin  Label1.Caption :=  NSProcessInfo.ProcessInfo.operatingSystemVersionString.UTF8String;    Label2.Caption := IntToStr( NSProcessInfo.ProcessInfo.OperatingSystemVersion.majorVersion ) + '.' +                    IntToStr( NSProcessInfo.ProcessInfo.OperatingSystemVersion.minorVersion ) + '.' +                    IntToStr( NSProcessInfo.ProcessInfo.OperatingSystemVersion.patchVersion );end;  end. 

trev:
Beware NSProcessInfo.ProcessInfo.OperatingSystemVersion is only available from Yosemite (10.10). Whereas operatingSystemVersionString is available from Jaguar (10.2).

See the Wiki article Accessing macOS System Information.

Hansaplast:
Thanks Trev for the heads up!
I wasn't aware ...

trev:
The final solution which works for Mac OS X 10.0 through macOS 11.2.3:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1;{$mode objfpc}{$H+}{$modeswitch objectivec2} interface uses  Classes, SysUtils, Forms, Dialogs, StdCtrls, ExtCtrls,  MacOSAll,  CocoaAll; type  TForm1 = Class(TForm)    Memo1: TMemo;    Button1: TButton;    procedure Button1Click(Sender: TObject);  private  public  end; var  Form1: TForm1;  counter: Int64; implementation {$R *.lfm} { TForm1 }procedure ProcessInfo;begin    Form1.Memo1.Lines.Clear;     Form1.Memo1.Lines.Add('macOS version: '      + IntToStr(NSProcessInfo.ProcessInfo.operatingSystemVersion.majorVersion)      + '.'      + IntToStr(NSProcessInfo.ProcessInfo.operatingSystemVersion.minorVersion)      + '.'      + IntToStr(NSProcessInfo.ProcessInfo.operatingSystemVersion.patchVersion)    );end; Procedure GestaltInfo;var    majorVersion: LongInt = 0;    minorVersion: LongInt = 0;    patchVersion: LongInt = 0;begin    Form1.Memo1.Lines.Clear;    Form1.Memo1.Lines.Add('Trying another way...');     Gestalt(gestaltSystemVersionMajor, majorVersion);    Gestalt(gestaltSystemVersionMinor, minorVersion);    Gestalt(gestaltSystemVersionBugFix, patchVersion);     Form1.Memo1.Lines.Add('macOS vedrsion: ' + IntToStr(majorVersion) + '.' +      IntToStr(minorVersion) + '.' + IntToStr(patchVersion));end; procedure TForm1.Button1Click(Sender: TObject);var  aSEL : SEL;begin  // Check whether OS version responds to this selector  aSEL := objcselector('operatingSystemVersion');  if NSProcessInfo.processInfo.respondsToSelector(aSEL) then    processInfo  // Fallback to the deprecated Gestalt function  else     GestaltInfo;end;end.

Navigation

[0] Message Index

[#] Next page

Go to full version