Use docklib to manage macOS docks


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:

import os
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

section=’persistent-others’
with
section=’persistent-apps’

For example, this will add Microsoft Word only if it’s not in the dock already:

from docklib import Dock
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:

from docklib import Dock
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.


3 responses to “Use docklib to manage macOS docks”

  1. I get an error using this on Sierra 10.13.6. I’ve run the docklib .pkg from the releases page. It looks like the first line fails

    >python
    >>> import docklib
    >>> dock = Dock()
    Traceback (most recent call last):
    File “”, line 1, in
    File “/Library/Python/2.7/site-packages/docklib.py”, line 35, in __init__
    self.items[key] = section.mutableCopy()
    AttributeError: ‘NoneType’ object has no attribute ‘mutableCopy’

Leave a Reply to rm Cancel 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.