How can I get the system version programatically?


How can I get the system version programatically?

I’ve been using this…

dim sys1, sys2 as Integer
//be more specific
call System.Gestalt("sys1", sys1)
call System.Gestalt("sys2", sys2)

select case sys1
case 10 //os="Mac OS X"
  select case sys2
  case 6
    OSX_Name="Snow Leopard"
  case 7
  case 8
    OSX_Name="Mountain Lion"
  case 9
  case 10
  end Select
end Select

but cannot get it for El Capitan.



case 11
OSX_Name=“El Capitan”

  Dim noerror As Boolean
  Dim result As Integer
  Dim sversion As String
  Dim os As String
  Dim sysMajorVersion,sysMinorVersion,sysBugVersion As Integer
  #If TargetMacOS
    Call System.Gestalt("sys1", sysMajorVersion)
    Call System.Gestalt("sys2", sysMinorVersion)
    Call System.Gestalt("sys3", sysBugVersion)
    sversion=Str(sysMajorVersion) + "." + Str(sysMinorVersion) + "." + Str(sysBugVersion)
    Select Case sysMajorVersion
    Case 10
      Select Case sysMinorVersion
      Case 0
      Case 1
      Case 2
      Case 3
      Case 4
      Case 5
      Case 6
        os="Snow Leopard"
      Case 7
      Case 8
        os="Mountain Lion"
      Case 9
      case 10
      case 11
        os="El Capitan"
      Case Else
      End Select
    Case Else
    End Select
    Return "Mac OSX "+os+" "+sversion
  #ElseIf TargetWin32
    OS = "Windows"
    //try to be more specific of windows version
    Soft Declare Sub GetVersionExA Lib "Kernel32" ( info As Ptr )
    Soft Declare Sub GetVersionExW Lib "Kernel32" ( info As Ptr )
    Dim info As MemoryBlock
    If System.IsFunctionAvailable( "GetVersionExW", "Kernel32" ) Then
      info =  New MemoryBlock( 20 + (2 * 128) )
      info.Long( 0 ) = info.Size
      GetVersionExW( info )
      info =  New MemoryBlock( 148 )
      info.Long( 0 ) = info.Size
      GetVersionExA( info )
    End If
    Dim Str As String
    If info.Long( 4 ) = 4 Then
      If info.Long( 8 ) = 0 Then
        os = "Windows 95/NT 4.0"
      Elseif info.Long( 8 ) = 10 Then
        os = "Windows 98"
      Elseif info.Long( 8 ) = 90 Then
        os = "Windows Me"
      End If
    Elseif info.Long( 4 ) = 3 Then
      os = "Windows NT 3.51"
    Elseif info.Long( 4 ) = 5 Then
      If info.Long( 8 ) = 0 Then
        os = "Windows 2000"
      Elseif info.Long( 8 ) = 1 Then
        os = "Windows XP"
      Elseif info.Long( 8 ) = 2 Then
        os = "Windows Server 2003"
      End If
    Elseif info.long(4) = 6 Then
      If info.long(8) = 0 Then
        os = "Windows Vista"
      Elseif info.long(8) = 1 Then
        os = "Windows 7"
      End If
    End If
    Str = " Build " + Str( info.Long( 12 ) )
    If System.IsFunctionAvailable( "GetVersionExW", "Kernel32" ) Then
      Str = Str + " " + Trim( info.WString( 20 ) )
      Str = Str + " " + Trim( info.CString( 20 ) )
    End If
    os = os + Str
    Return os

Thanks Dave,
Works great.

If memory serves, isn’t System.Gestalt ‘deprecated’? If so, it will go away at some point. It still works for now, but be aware that it might not be the safest way in the future.

If you have MBS use: SystemInformationMBS.OSVersionString as it works cross-platform/

No, these APIs won’t go away. When has Apple really made such core APIs go away before? Sure, Quicktime went away, but that’s not core tech. And some things may eventually break, such as Gestalt for “sysv” because it can’t represent the higher versions any more correctly. But all the old APIs are still around, for resources, for old file manager calls, and so on.

But for the record: The proper way on OS X to get the system version since OS X 10.10 (before that, it won’t work and you have to use the Gestalt operation) is to use NSProcessInfo.ProcessInfo.OperatingSystemVersion (should be in MacOSLib).

doesn’t the Windows version need to include 8 and 10??


other way to get systemversion without gestalt :

[code]Place this where you want your OS X Version information called from

Dim OSXVersion as String = fGetOS_Version(“System Version”,“SPSoftwareDataType”)

Function fGetOS_Version (optional Key as String, optional location as string) as String
Dim s as new Shell
Dim rvalue as string
s.Execute “system_profiler “+location+” | grep “+chr(34)+key+chr(34)
rvalue = trim(NthField(s.Result,”:”,2))
if rvalue <> “” then
Return rvalue
Elseif rvalue = “” Then
Return “unidentified”
end if


Sam made a Module that does it properly for Mac using Declares on systems that have them available:

Jean-Yves, that’s an awfully wasteful solution. It’s almost as bad as using AppleScript to open the “About this macintosh” window, making a screen copy, uploading the image to a website for text extraction and then search the text for the OS version. :wink:

Never say never, FSRefs were deprecated in 10.8 and almost gone in 10.10! Caught so many developers out, as no-one expected Apple to remove file system operation APIs.

[quote=260374:@Tim Parnell]Sam made a Module that does it properly for Mac using Declares on systems that have them available: [/quote]
Thanks Tim, it also falls back to Gestalt on a system where the more recent APIs are not available.

no because you only get a small part of systemprofiler : system_profiler SPSoftwareDataType
on my mac it return 12 lines that then go into grep to fetch the system version

Simple sell commands…
Cpu’s info

[code]192:~ ioannis$ time sysctl -n machdep.cpu.brand_string
Intel® Core™ i5-2400 CPU @ 3.10GHz

real 0m0.003s
user 0m0.001s
sys 0m0.002s

[code]192:~ ioannis$ time uname -a
Darwin 15.4.0 Darwin Kernel Version 15.4.0: Fri Feb 26 22:08:05 PST 2016; root:xnu-3248.40.184~3/RELEASE_X86_64 x86_64

real 0m0.002s
user 0m0.001s
sys 0m0.001s

System version

[code]192:~ ioannis$ time sw_vers
ProductName: Mac OS X
ProductVersion: 10.11.4
BuildVersion: 15E65

real 0m0.008s
user 0m0.004s
sys 0m0.003s

And no wasteful solution

MBS Plugin should detect Windows 8 and 10 probably.
Including build numbers.

yep systemprofiler is really longer than Loannis’s commands …

[code]$ time system_profiler SPSoftwareDataType

System Software Overview:

  System Version: OS X 10.9.5 (13F1712)
  Kernel Version: Darwin 13.4.0
  Boot Volume: disquedur
  Boot Mode: Normal
  Secure Virtual Memory: Enabled

real 0m0.250s
user 0m0.096s
sys 0m0.070s

A good old Unix command may not look as cute and fancy as a declare, but at least it won’t get deprecated on Apple’s whim.

If its one Appe added like system profiler it sure could
plutil has had significant changes over the years
And now for many things you need to use plist buddy instead of plutil

Why I say "good old Unix command " vs “Apple fancy addition to Unix”.

You mean like this ?

        time sw_vers

sw_vers is an apple addition as well :stuck_out_tongue:

uname gives you “something” but not the OS version