docklib instead of dockutil
I have a few posts about using dockutil to manage the macOS dock. dockutil is still a valid and working project, but I’m starting to migrate my scripts to docklib instead.
Installing docklib
The installation instructions for docklib say you can put the docklib.py file in the same directory as the scripts that invoke it or you can put it “in your Python path.” I’d recommend just grabbing the docklib .pkg from the releases page or using the AutoPkg docklib recipes to download it. The .pkg puts docklib.py in /Library/Python/2.7/site-packages/docklib.py.
Using docklib with Outset
docklib can be used in an Outset login-once or login-every script. There is no need to explicitly put in a delay to wait for the initial dock to appear before running your script. There is also no need, if you’re specifying a dock (rather than modifying an existing one) to remove the default applications Apple puts on the dock. If you’re specifying a dock, just say what you want to add. Use this suggested template:
from docklib import Dock
tech_dock = [
‘/Applications/Google Chrome.app’,
‘/Applications/App Store.app’,
‘/Applications/Managed Software Center.app’,
‘/Applications/System Preferences.app’,
‘/Applications/Utilities/Activity Monitor.app’,
‘/Applications/Utilities/Console.app’,
‘/Applications/Utilities/Disk Utility.app’,
‘/Applications/Utilities/Migration Assistant.app’,
‘/Applications/Utilities/Terminal.app’,
]
dock = Dock()
dock.items[‘persistent-apps’] = []
for item in tech_dock:
if os.path.exists(item):
item = dock.makeDockAppEntry(item)
dock.items[‘persistent-apps’].append(item)
dock.save()
Checking if an item exists before removing/adding via docklib?
Here’s an example of checking for something’s existence on the right side of the dock before adding it. To check on the left side, it’s a very similar process, except you just replace
with
For example, this will add Microsoft Word only if it’s not in the dock already:
dock = Dock()
if dock.findExistingLabel(‘Microsoft Word’, section=’persistent-apps’) == -1:
item = dock.makeDockAppEntry(‘/Applications/Microsoft Word.app’)
dock.items[‘persistent-apps’].append(item)
dock.save()
If you add an item using docklib that already exists in the dock, a second instance of it will be added to the dock, so you definitely should check for the existence of the item first.
However, if you want to remove an item, just use the standard removal procedure:
dock = Dock()
dock.removeDockEntry(‘Microsoft Word’)
dock.save()
If the item isn’t in the dock when you try to remove it, docklib won’t give any error or warning.
Leave a Reply