Putting Dockutil add/removes in arrays in Bash

Most of the time when you see scripts using Dockutil to add and/or remove items from the Dock in Mac OS X, they look like this:

/usr/local/bin/dockutil –add ‘/Applications/NAMEOFAPP1.app’ –no-restart
/usr/local/bin/dockutil –add ‘/Applications/NAMEOFAPP2.app’ –no-restart
/usr/local/bin/dockutil –add ‘/Applications/NAMEOFAPP3.app’ –no-restart
/usr/local/bin/dockutil –add ‘/Applications/NAMEOFAPP4.app’ –no-restart
/usr/local/bin/dockutil –add ‘/Applications/NAMEOFAPP5.app’ –no-restart
/usr/local/bin/dockutil –add ‘/Applications/NAMEOFAPP6.app’ –no-restart
/usr/local/bin/dockutil –add ‘/Applications/NAMEOFAPP7.app’ –no-restart
That’s totally fine to do, but I figured putting the additions and removals into arrays may be a cleaner way to manage things, so I wrote up a sample script for that.

There is an array of Dock items to remove and then an array of Dock items to add. Then it loops through all the removal items, makes sure each item already exists (no point in removing something that doesn’t exist), and then removes it. Then the script loops through all the addition items, makes sure each item isn’t already there (otherwise, Dockutil warns that you should using the –replacing option) and that the source item exists (otherwise, the non-existent item gets added as a question mark), and then adds it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.