6

I am writing an application uninstaller in which I want to remove the icon of our app from Dock. During instalation the icon was added to dock using the following on commandline:

sudo -u "$USER" 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/MyApplication.app</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"
sudo -u "$USER" osascript -e 'tell Application "Dock"' -e 'quit' -e 'end tell'

During uninstallation I am using the following shell script to remove icon from Dock:

#!/bin/sh
# Get location of entry for our application in Dock
dloc=$(defaults read com.apple.dock persistent-apps | grep file-label\" | awk '/MyApplication/ {print NR}')
dloc=$((dloc - 1))

# Remove this entry from Dock's plist file : com.apple.dock.plist
/usr/libexec/PlistBuddy -c "Delete persistent-apps:$dloc" ~/Library/Preferences/com.apple.dock.plist

# Restart Dock to persist changes
osascript -e 'delay 3' -e 'tell Application "Dock"' -e 'quit' -e 'end tell' -e 'delay 3'
#killall Dock

I can see that the above script successfully removes the entry of MyApplication from the persistent-apps from com.apple.dock.plist plist. However after restarting the Dock, the dock still has the same icons as the previous.

Can someone please help?

Thanks,

4 Answers 4

5

Deleting the item twice will solve this issue:

#!/bin/sh -
#delete item from com.apple.dock.plist
dloc=$(defaults read com.apple.dock persistent-apps \
    | grep file-label \
    | awk '/Notes/  {printf NR}')
dloc=$[$dloc-1]
echo $dloc
sudo -u $USER /usr/libexec/PlistBuddy \
    -c "Delete persistent-apps:$dloc" \
    ~/Library/Preferences/com.apple.dock.plist

#must delete item from com.apple.dock.plist agian,or won't change
dloc=$(defaults read com.apple.dock persistent-apps \
    | grep file-label \
    | awk '/Photo Booth/  {printf NR}')
#dloc=$(defaults read com.apple.dock persistent-apps \
    | grep _CFURLString "PageManager%209.31.app")
dloc=$[$dloc-1]
echo $dloc
sudo -u $USER /usr/libexec/PlistBuddy \
    -c "Delete persistent-apps:$dloc" \
    ~/Library/Preferences/com.apple.dock.plist
sleep 3
# Restart Dock to persist changes
osascript -e 'delay 3' \
          -e 'tell Application "Dock"' \
          -e 'quit' \
          -e 'end tell'
1

Silly question, probably ;-) , but you are modifying the correct users plist in the delete part of the script, aren't you.

It looks as though it could be deleting from the /root/Library and not the /Users/username/Library....

1

I made a quick python script that you can run to delete any icon for all users in the system based on the solutions given here. It needs to be ran as root.

https://github.com/jcarm010/osx-dock-remover

0

I realize this is an old post, but I just found another link about PlistBuddy and thought I'd share: Things set with PlistBuddy on Mavericks need a little extra push. While this mentions items dealing with Finder, I took your script above, and tested a single iteration of the remove from dock, followed by killing cfprefsd with killall, and then killed the Dock, and the icon went away. I'm going to test a couple of times to make sure, but I believe that will do the trick for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.