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.