Activating Dock auto-hide feature from command line

I'm trying to put a script together that enables autohide when I use my laptop as the main screen, and disable when it's connected to an external display. I've got the detection bit working now. But what I can't seem to get working, is turning the autohide feature on and off.


I find this all over the web ...

Hide-command: defaults write com.apple.Dock autohide -bool TRUE; killall Dock

Show-command: defaults write com.apple.Dock autohide -bool FALSE; killall Dock


And the hide-command works.

But the show-command seems to not be working for some reason.


Also, my desktop flashes when I run either command.

When I hit Opt-Cmd-D, this doesn't happen.


Any recommendations on how I can trigger that shortkey?

Or other thoughts on how to make this work?

MacBook Air, OS X Mountain Lion (10.8.3), 2GHz i7, 8GB RAM, 256GB SSD

Posted on May 10, 2013 2:58 AM

Reply
Question marked as Top-ranking reply

Posted on May 10, 2013 7:01 PM

I used to be a big fan of Marco Polo, but have not been able to find a good replacement for it. So I now have a script which runs a number of checks every 10 seconds, to see if anything in the environment has changed. I've just added the following to it, which I figured I'd share back to the community as the community helped me write it. Thanks, Mark!


#!/bin/bash

# Hides the dock when external display is connected


if [ `system_profiler SPDisplaysDataType | grep 'Cinema\|Thunderbolt' | wc -l` -eq "1" ]; then

osascript -e "tell application \"System Events\" to set the autohide of the dock preferences to false"

else

osascript -e "tell application \"System Events\" to set the autohide of the dock preferences to true"

fi


Make sure you change the grep statement for your own circumstances, the \| is an OR statement I need because I have a Cinema Display at home and a Thunderbolt Display at the office.

9 replies
Question marked as Top-ranking reply

May 10, 2013 7:01 PM in response to Rob de Jonge

I used to be a big fan of Marco Polo, but have not been able to find a good replacement for it. So I now have a script which runs a number of checks every 10 seconds, to see if anything in the environment has changed. I've just added the following to it, which I figured I'd share back to the community as the community helped me write it. Thanks, Mark!


#!/bin/bash

# Hides the dock when external display is connected


if [ `system_profiler SPDisplaysDataType | grep 'Cinema\|Thunderbolt' | wc -l` -eq "1" ]; then

osascript -e "tell application \"System Events\" to set the autohide of the dock preferences to false"

else

osascript -e "tell application \"System Events\" to set the autohide of the dock preferences to true"

fi


Make sure you change the grep statement for your own circumstances, the \| is an OR statement I need because I have a Cinema Display at home and a Thunderbolt Display at the office.

May 11, 2013 6:04 AM in response to Rob de Jonge

Hi Rob, I have a few suggestion for you

if [ `system_profiler SPDisplaysDataType | grep 'Cinema\|Thunderbolt' | wc -l` -eq "1" ]; then

# loose the backtick and write your command substition like $(command|command|.....)

if [ $(system_profiler SPDisplaysDataType | grep 'Cinema\|Thunderbolt' | wc -l) -eq "1" ]; then

# doesn't that read better


# Any time that I pipe more that once in a statement, 
# the little voices inside my head tell me there must be a better way

if [ $(system_profiler SPDisplaysDataType | grep -c 'Cinema\|Thunderbolt') -eq "1" ]; then

# All non-Apple command line tool produce a
# non-zero exit status if they fail. So the following
# should work without producing a sub-shell (faster execution).
# You don't have to test "[" either.


if system_profiler SPDisplaysDataType | grep -q 'Cinema\|Thunderbolt'; then

# Does the following write to the dock preference file every 10 seconds
# if the display is not connected?


else
  osascript -e "tell application \"System Events\" to set the autohide of the dock preferences to true"

# May be something like this if it is

else
     osascript <<-EOF
          tell application "System Events"
               set results to get the autohide of the dock preferences
               if resutls = false then
                    set the autohide of the dock preferences to true
               end if
          end tell
EOF

May 10, 2013 9:18 AM in response to Rob de Jonge

The display command is:


defaults read com.apple.Dock autohide


That's a read, and there's no need to restart the Dock.


The following (intentionally) isn't complete, but is pretty close (as I don't want to kill my Dock right now, due to an unrelated factor)...


if [[ `defaults read com.apple.Dock autohide` = 0 ]] ; then defaults write com.apple.Dock autohide 1 ; fi ;

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Activating Dock auto-hide feature from command line

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.