Move and Hide/Unhide the Dock When an App Launches or Quits

This isn't exactly a macro, it is a script to be used in a macro, but I think it might help others.

Scenario

Usually I keep my dock on the left, and it is visible.

However, when editing in Fission, I want to maximize screen space, so I want to hide the dock, and since I don't want the dock popping up when I'm editing the beginning of a file, I want to move it to the bottom of the screen.

If you have instances where you might want to hide/show/move the dock for particular apps, this can be of use to you, too.

Here's the script that Keyboard Maestro runs when Fission launches:

#!/usr/bin/env zsh -f
# Purpose: Relocate the Dock and set it to auto-hide when Fission launches
#
# From:	Timothy J. Luoma
# Mail:	luomat at gmail dot com
# Date:	2020-06-29

PATH='/usr/local/scripts:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin'

	# is the dock already on the bottom? If yes, skip this
if [[ "`defaults read com.apple.dock orientation`" != "bottom" ]]
then
		# if we get inside this block, then the dock is not at the bottom

		# move dock to the bottom
	defaults write com.apple.dock orientation bottom

		# restart the Dock
	killall Dock

		# give the system a moment to restart the Dock automatically
	sleep 1

fi

	# is the dock already set to autohide? If yes, skip this
if [[ "`defaults read com.apple.dock autohide`" != "1" ]]
then
		# If we get inside this block them we need to tell the dock to hide.

		## Now, we _could_ do this by issuing this command:
		##
		## 		defaults write com.apple.dock autohide -bool true
		##
		## BUT, we can also do that by sending ⌘ ⌥ D
		##
		## which has a few advantages. First, it does not require killing the dock.
		## Second, apps that move/resize windows, such as Spectacle and Rectangle
		## keep track of the dock, and this can help them, too.
 		##
		## there are at least two ways to do this. One is to make a Keyboard Maestro macro
		## called 'Toggle Dock Visibility' which just sends the keyboard shortcut Command + Option + D
		##
		## 		osascript -e 'tell application "Keyboard Maestro Engine" to do script "Toggle Dock Visibility"'
		##
		## The 'easier' way is to use AppleScript to tell System Events to run that keyboard shortcut
		## Note: you may have to give 'Accessibility' permissions to osascript the first time you use it

	osascript \
		-e 'tell application "System Events"' \
		-e 'keystroke "d" using [command down, option down]' \
		-e 'end tell'

fi

exit 0
#EOF

And here is the script that Keyboard Maestro runs when Fission quits. You’ll notice that it does almost the exact same thing except that it checks for 'left' instead of 'bottom' and 0 instead 1 for the auto-hide value.

#!/usr/bin/env zsh -f

PATH='/usr/local/scripts:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin'

if [[ "`defaults read com.apple.dock orientation`" != "left" ]]
then
	defaults write com.apple.dock orientation left

	killall Dock

	sleep 1
fi

if [[ "`defaults read com.apple.dock autohide`" != "0" ]]
then

	osascript \
		-e 'tell application "System Events"' \
		-e 'keystroke "d" using [command down, option down]' \
		-e 'end tell'
fi

exit 0
#EOF

I hope this might help someone else who might want to control the dock when a particular app launches or quits.

1 Like

How did I know that if I look for help with a very unique problem the solution would be here written by @tjluoma??? :slight_smile:

Exactly what I've been looking for. When I'm on my laptop, I don't want to waste the screen real estate of the dock on the side. But when I plug my laptop into my 2 24" screens at the office, I like having the doc on the side. I knew how to automate Command, Option D, but it was the script to check the current dock state that was the secret sauce I needed.

I've tied it to wireless network triggers, as wifi seemed easier for me to get my head around. one macro when I connect to my work wifi, and a second macro when I connect to my home or Starbucks wifi. There's probably a way to trigger on the attachment and the disconnect of an external monitor, but I couldn't get my head around that as a trigger, so this is working so well. so well that after a few days I barely notice it. It just works!

1 Like

Hey TJ,

I think you can do what you want without having to fool with defaults and killing the Dock.

All of these are available for get/set:

Mojave (and pretty far back):

tell application "System Events"
   tell dock preferences
      animate
      autohide
      dock size
      magnification
      magnification size
      minimize effect
      screen edge
   end tell
end tell

Big Sur:

tell application "System Events"
   tell dock preferences
      animate
      autohide
      autohide menu bar
      dock size
      double click behavior
      magnification
      magnification size
      minimize effect
      minimize into application
      screen edge
      show indicators
      show recents
   end tell
end tell

-Chris

1 Like

Oh! Very nice! Much better, obviously. Thanks!

1 Like