Adrian Nier Code

Back to overview

Add spacers on right

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

Adds an element to the right side of the Dock that has a blank icon and does nothing when clicked. The element can be dragged around and can serve as a spacer.

Implementation

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

-- Ask the user how many spacers to add
set _spacerCount to askForIntegerWithPrompt(localizedString("spacer_count_prompt"), 1)

-- Quit the Dock, allowing recent changes by the user to be saved
tell application "Dock" to quit
repeat 30 times
   tell application "System Events" to if (exists process "Dock") is false then exit repeat
   delay 0.1
end repeat

-- Launch the Dock
tell application "Dock" to launch
repeat 30 times
   tell application "System Events" to if (exists process "Dock") then exit repeat
   delay 0.1
end repeat

-- Modify the Dock preferences and restart it
repeat _spacerCount times
   do shell script "defaults write com.apple.dock persistent-others -array-add '{tile-data={}; tile-type=\"spacer-tile\";}'"
end repeat
do shell script "killall -hup Dock"

on askForIntegerWithPrompt(_prompt, _default)
   activate
   repeat
      try
         return text returned of (display dialog _prompt default answer _default) as integer
      on error _eMsg number _eNum
         if _eNum = -128 then error _eMsg number _eNum
         beep
      end try
   end repeat
end askForIntegerWithPrompt


on localizedString(_string)
   
   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 "spacer_count_prompt/en" then return "Please specify how many spacers you want to add:"
   if _stringKey is "spacer_count_prompt/de" then return "Bitte geben Sie an wieviele Trennelemente hinzugefuegt werden sollen:"
   
   
end localizedString

on systemLanguage()
   try
      return first word of (do shell script "defaults read NSGlobalDomain AppleLanguages")
   on error
      return "en"
   end try
end systemLanguage

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 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