Adrian Nier Code

Back to overview

Toggle menu bar transparency

Last modification: Monday, September 01, 2008 09:48 pm

Toggles the preference to disable the menu bar transparency that was introduced in Mac OS X 10.5.2.

Implementation

-- Check requirements
if requireSystemVersion({__major:10, __minor:5, __revision:2}) is false then return false

-- Get the current value for the preference setting
try
   set _bitSet to do shell script "defaults read .GlobalPreferences AppleEnableMenuBarTransparency"
   set _bitSet to _bitSet as integer
   set _bitSet to _bitSet as boolean
on error
   set _bitSet to true
end try

-- Quit System Preferences if it is running
tell application "System Events" to set _sysPrefWasRunning to (exists process "System Preferences")
if _sysPrefWasRunning then
   tell application "System Preferences" to quit
   repeat 30 times
      tell application "System Events" to if (exists process "System Preferences") is false then exit repeat
      delay 0.1
   end repeat
end if

if _bitSet then
   do shell script "defaults write .GlobalPreferences AppleEnableMenuBarTransparency -boolean NO"
else
   do shell script "defaults write .GlobalPreferences AppleEnableMenuBarTransparency -boolean YES"
end if

-- Launch System Preferences to make changes visible
do shell script "open /System/Library/PreferencePanes/DesktopScreenEffectsPref.prefPane"
if _sysPrefWasRunning is false then tell application "System Preferences" to quit

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 _stringKey to _string & "/" & systemLanguage()
         
         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 unterstuetztes System"
         
         if _stringKey is "requirements_not_met_dialog_message/en" then return "This script requires at least system version " & _var & ". "
         if _stringKey is "requirements_not_met_dialog_message/de" then return "Dieses Skript erfordert mindestens 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