Adrian Nier Code

Back to overview

Require system version

Last modification: Thursday, June 11, 2009 08:29 pm

Checks if the specified minimum system version is available. Takes a single record as argument.

Required keys
__major (integer)
The major system version as integer

__minor (integer)
The minor system version as integer

__revision (integer)
The system revision as integer

Implementation

on requireSystemVersion(_args)
   
   -- Declare a few additional necessary functions
   script RequireSystemVersion_Extras
      
      on systemVersion()
         try
            -- Use system_profiler command line tool to get the information about the system version
            set _spOutput to paragraph 1 of (do shell script "system_profiler SPSoftwareDataType -detailLevel mini | grep 'System Version: '")
            
            -- Remove the label "System Version" from the line
            set _prvDlmt to AppleScript's text item delimiters
            set AppleScript's text item delimiters to "System Version: "
            set _versionString to text item 2 of _spOutput
            set AppleScript's text item delimiters to _prvDlmt
            
            -- Find the position of the first numerical character
            set _firstNumPosition to false
            repeat with _i from 1 to count of _versionString
               try
                  get (character _i of _versionString) as integer
                  set _firstNumPosition to _i
                  exit repeat
               end try
            end repeat
            if _firstNumPosition is false then error "No numerical character found."
            
            -- Break the string at the position of the first numerical character
            set _prvDlmt to AppleScript's text item delimiters
            set AppleScript's text item delimiters to ""
            set _versionString to characters _firstNumPosition thru -1 of _versionString as string
            
            -- Get the text before first space
            set AppleScript's text item delimiters to " "
            set _versionString to text item 1 of _versionString as string
            
            -- Get the text before first space
            set AppleScript's text item delimiters to "."
            copy (text items of _versionString) to {_major, _minor, _revision}
            set AppleScript's text item delimiters to _prvDlmt
            
            -- Transform strings to integers
            set _major to _major as integer
            set _minor to _minor as integer
            set _revision to _revision as integer
            
         on error _eMessage
            -- Reset the delimiters
            set AppleScript's text item delimiters to _prvDlmt
            
            -- This alternative will work for systems that do not support the do shell script command
            set _hex to system attribute "sysv"
            
            -- Get the revision
            set _revision to _hex mod 16
            set _hex to _hex div 16
            
            -- Get the minor version
            set _minor to _hex mod 16
            set _hex to _hex div 16
            
            -- Get the major version
            set _major1 to _hex mod 16 as string
            set _hex to _hex div 16
            set _major2 to _hex mod 16 as string
            set _hex to _hex div 16
            set _major to (_major2 & _major1 as string) as integer
            
            -- Concatenate the version string
            set _versionString to (_major as string) & "." & (_minor as string) & "." & (_revision as string)
            
         end try
         
         return {__major:_major, __minor:_minor, __revision:_revision, __string:_versionString}
         
      end systemVersion
      
      on localizedString(_string, _var)
         
         set _supportedLanguages to {"en", "de"}
         set _targetLanguage to systemLanguage()
         if _targetLanguage is not in _supportedLanguages then
            set _targetLanguage to item 1 of _supportedLanguages
         end if
         
         set _stringKey to _string & "/" & _targetLanguage
         
         if _stringKey is "requirements_not_met_dialog_title/en" then return "System not supported"
         if _stringKey is "requirements_not_met_dialog_title/de" then return "Nicht unterst" & (ASCII character 159) & "tztes System"
         
         if _stringKey is "requirements_not_met_dialog_message/en" then return "This script requires system version " & _var & ". "
         if _stringKey is "requirements_not_met_dialog_message/de" then return "Dieses Skript erfordert die Systemversion " & _var & ". "
         
      end localizedString
      
      on systemLanguage()
         try
            return first word of (do shell script "defaults read NSGlobalDomain AppleLanguages")
         on error
            return "en"
         end try
      end systemLanguage
   end script
   
   -- Get the current system version
   tell RequireSystemVersion_Extras to set _currentVersion to systemVersion()
   
   -- Check if requirements are met
   if (__major of _currentVersion) < (__major of _args) then
      set _requirementsMet to false
      
   else if (__major of _currentVersion) = (__major of _args) then
      
      if (__minor of _currentVersion) < (__minor of _args) then
         set _requirementsMet to false
         
      else if (__minor of _currentVersion) = (__minor of _args) then
         
         if (__revision of _currentVersion) < (__revision of _args) then
            set _requirementsMet to false
         else
            set _requirementsMet to true
         end if
         
      else
         set _requirementsMet to true
      end if
      
   else
      set _requirementsMet to true
   end if
   
   if _requirementsMet is false then
      -- Display a dialog if requirements are not met
      set _requiredVersionString to (__major of _args as string) & "." & (__minor of _args as string) & "." & (__revision of _args as string)
      tell RequireSystemVersion_Extras
         set _dialogTitle to localizedString("requirements_not_met_dialog_title", null)
         set _dialogMessage to localizedString("requirements_not_met_dialog_message", _requiredVersionString)
      end tell
      
      activate
      try
         if (__major of _currentVersion)10 and (__minor of _currentVersion)4 then
            display alert _dialogTitle message _dialogMessage buttons {"OK"} default button "OK" as warning
         else
            error 1
         end if
      on error
         display dialog _dialogTitle & return & return & _dialogMessage buttons {"OK"} default button "OK" with icon 2
      end try
      return false
   else
      return true
   end if -- _requirementsMet is false
   
end requireSystemVersion