Set Dock auto-hide state via AppleScript

Mar 19, '10 07:30:00AM

Contributed by: Andrew J Freyer

There are dozens of hints that programatically toggle the Dock's auto-hide state. Some suggest modifying com.apple.Dock, then killing the dock. Others rely on sending keystrokes to merely toggle the dock state.

Neither of these give the user elegant control over the state of the dock. So, I've written a small snippet of AppleScript combining these ideas, giving absolute control over the Dock's hiding state without killing the dock or modifying the plist. This is suitable for use in any situation where you want to allow the user to control the Dock's visibility state in your AppleScript code:

set weWantToHideTheDock to true

set currentDockHiddenState to (do shell script "defaults read com.apple.Dock autohide")

if (currentDockHiddenState is equal to "0") and (weWantToHideTheDock) then
  tell application "System Events"
    keystroke "d" using [command down, option down]
  end tell
else
  if (weWantToHideTheDock is false) and (currentDockHiddenState is equal to "1") then
    tell application "System Events"
      keystroke "d" using [command down, option down]
    end tell
  end if
end if
Your script will need to set weWantToHideTheDock. If set to true, the Dock will switch to auto-hide mode; setting it to false will disable auto-hiding. It's not a toggle, so if the Dock is already hidden and weWantToHideTheDock is set true, the Dock will stay hidden.

[robg adds: The above code isn't necessarily meant to be used as a standalone solution -- clearly it's much simpler to just press Command-Option-D if you want to toggle your Dock's visibility state. Instead, it's a snippet of code for use in larger programs.]

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20100316164551355