blog advertising is good for you


blog advertising is good for you
User login

Adding Items To The Dock

As an admin, sometimes you want to tweak the dock a bit when you can’t sit at the Mac or VNC in to it. Luckily, the Dock is governed by a plist that’s a bit cryptic, but is otherwise fairly easy to tweak. I used this quite a bit, back in the day, but when a friend asked me for it recently, I had to dig around quite a bit to find the darn thing.

The Basics

The com.apple.dock.plist file is binary, but responds to defaults as you would expect. At first, the CFURLAliasData field looks a bit intimidating, but luckily we don’t really have to worry about it. In _persistent-apps, a typical Docked item looks about like this:

        {
            GUID = 1883183727; 
            "tile-data" = {
                "file-data" = {
                    "_CFURLAliasData" = <00000000 00b40003 00010000 c107bb6f 0000482b 00000000 0000013d 00000a22 0000bfee 53de0000 00000920 fffe0000 00000000 0000ffff ffff0001 00080000 013d0000 013c000e 001a000c 00540065 0072006d 0069006e 0061006c 002e0061 00700070 000f001a 000c004d 00610063 0069006e 0074006f 00730068 00200048 00440012 00234170 706c6963 6174696f 6e732f55 74696c69 74696573 2f546572 6d696e61 6c2e6170 70000013 00012f00 ffff0000 >; 
                    "_CFURLString" = "/Applications/Utilities/Terminal.app/"; 
                    "_CFURLStringType" = 0; 
                }; 
                "file-label" = Terminal; 
                "file-mod-date" = 3220067294; 
                "file-type" = 41; 
                "parent-mod-date" = 3250793941; 
            }; 
            "tile-type" = "file-tile"; 
        }, 

Thankfully, we don’t care about roughly half of that. We just need to build something that looks like this:

        {
            "tile-data" = {
                "file-data" = {"_CFURLString" = "/Applications/Terminal.app/"; "_CFURLStringType" = 0; }; 
            }; 
        }

It’s a little much to have to type in every time, what with matching brackets and escaping chars here and there, but the basic command to accomplish this is as follows:


defaults write com.apple.dock persistent-apps -array-add "<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Applications/Terminal.app/</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>";

The Dock doesn’t have any notifications set looking for external changes to this file, so it’ll ignore what you do until you logout or kill the Dock.

Getting Funky

All that nested XML is a pain in the ass to type in repeatedly, so I made a bash funct in my profile to handle both the defaults and the (sane) killing of the Dock. Sadly, bash aliases won’t take args like csh does, but since we have functs, we don’t really care too much. The following creates a bash function called “dockadd” that adds the first argument to the Dock, then uses osascript to quit (not kill) the Dock. This is done because killing the Dock will prevent it from writing out any changes the user has made on their own. Lossy techniques suck.


dockadd () {
        defaults write com.apple.dock persistent-apps -array-add "<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>$1</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>";
        osascript -e "tell application \"Dock\" to quit"
}

If you really want to, $1 can be changed to $* and it’ll take multiple arguments. There’s no sanity checking by the Dock, so you can violate typical usage here. Say, adding a document to the application side of the dock, or visa versa. Once you open a new bash (or reload your profile file), usage is quite simple.

$ dockadd /Application/Foo.app

When the Dock comes back up, your new item should be on the Dock.

Average rating
(1 vote)
About JC
JC's picture

Author Biography

JC is a former Mac Genius and Mac-centric IT worker with a background in print advertising. He earned a reputation as a miracle worker when he saved the day at a new business pitch with the arcane knowledge that Apple’s ADB cables were nothing more than poorly shielded S-Video cables.

JC runs the Heroic Efforts Data Recovery Service and writes Ungenius, a tawdry tale of the life and times of a former Mac Genius.

You can contact JC via IM or via the contact form.

Sorry for the stupid question, but how can I modify the script to add stuff to the documents side of the Dock?

JC's picture

just change the “persistent-apps” to “persistent-others”.

http://lucidsystems.org/luciddocktools.html

Works quite nicely in my experience, and it adds things to the correct location based upon whether they’re an app or a folder/file.

JC's picture

ah yes. i meant to mention additemtodock. It’s a nice tool… I just don’t want to have to install software packages to all the Macs I admin, and copying my .bash_profile is already part of my routine.

…the first argument to the Dock, then uses osascript to quit (not kill) the Dock. This is done because killing the Dock will prevent it from writing out any changes the user has made on their own. Lossy techniques suck.

Agreed, but is this still necessary? I can add an item to the Dock, then immediately run killall Dock and the changes are preserved. I can even run killall -9 Dock.

fseventer suggests that the Dock writes out changes just as soon as you make them.

It’s not really a big deal; both ways work. But I prefer to stick with shell when in a shell script (or function), and using kill will execute a little bit faster.

JC's picture

The Dock usually writes changes immediately, but is not consistent about it. My bad on implying that it never does, but it’s still safest to ‘quit’ it.

And I think you’ll find that the speed difference between them is negligible to the extreme.

How to get the list of apps currently in the Dock

How about removing items?

I am trying to find out the same thing. But removing is pretty easy to do manually just the same, as long as the machine is accessible.

Adding an item takes far longer than removing one.

I have not been able to find a “remove” so far.

JC, excellent script! Any idea on how to remove items from the array? So far the only idea I have is to read both the persistent apps and others, cull the offending app from the list and then recreate the whole array which seems a bit overkill. From the man page it seems there is only a -array-add flag and no tool to remove, other than the whole array…

We’re on network homes so this script would be very useful when we push new images out and there are app changes…

Post new comment
The content of this field is kept private and will not be shown publicly.
4 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.