[Pythonmac-SIG] Dock: adding/removing/checking

brad.allen at omsdal.com brad.allen at omsdal.com
Fri Oct 15 19:07:42 CEST 2004


What is the most appropriate way to interact with the Dock using Python? I
don't think my current method using the "defaults" command is optimal. It
only works when run as the user; however, most of my scripts are designed
to run as root/sudo because they are used for administering Macs on our LAN
at work. I'm wondering if there is a way to use a native API instead, but
didn't find what I was looking for at
http://developer.apple.com/documentation.

Thanks!

Below is my currently somewhat misguided approach:

(this relies on a function called getCommandOutput which I found in the
Python Cookbook at ActiveState)

def restartDock(username):
      cmd = 'killall -u ' + username + ' Dock'
      common.getCommandOutput(cmd)


def addAppToDock(pathToApp,username,verbose=False):
      #example: addAppToDock('/Applications/Microsoft Office 2004/Microsoft
Word','bonnie')
      assert os.path.exists(pathToApp)
      dockPrefPath =
os.path.join('/Users',username,'Library/Preferences/com.apple.dock')
      cmd = (
            "defaults write " + dockPrefPath + " persistent-apps
-array-add" + SPACE + SINGLEQUOTE +

"<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>"
            + pathToApp +

"</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"
            + SINGLEQUOTE
            )
      if verbose: print 'Executing:',cmd
      common.getCommandOutput(cmd)
      restartDock()



More information about the Pythonmac-SIG mailing list