Dockutil question

d_mccullough
New Contributor III

Hi all,

We're trying to use dockutil to remove some apps and add a few behind DEPNotify at first boot using this script. It's not working - anyone have any ideas why? I realized I could have used $3 for current user after I wrote it... go figure.

#!/bin/sh

# determines current user
currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' )

#kill preference cache
killall cfprefsd

# layout begins
sudo -u "$currentUser" /usr/local/bin/dockutil --remove 'Calendar' 
sudo -u "$currentUser" /usr/local/bin/dockutil --remove 'Contacts' 
sudo -u "$currentUser" /usr/local/bin/dockutil --remove 'Mail' 
sudo -u "$currentUser" /usr/local/bin/dockutil --remove 'Maps' 
sudo -u "$currentUser" /usr/local/bin/dockutil --remove 'iTunes' 
sudo -u "$currentUser" /usr/local/bin/dockutil --remove 'Reminders' 
sudo -u "$currentUser" /usr/local/bin/dockutil --remove 'Podcasts' 
sudo -u "$currentUser" /usr/local/bin/dockutil --remove 'TV' 
sudo -u "$currentUser" /usr/local/bin/dockutil --remove 'Music' 

#reload dock
sleep 5
killall dock
#!/bin/sh

# determines current user
currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' )

#kill preference cache
killall cfprefsd

# layout begins
sudo -u "$currentUser" /usr/local/bin/dockutil --add '/Applications/Microsoft Outlook.app'
sudo -u "$currentUser" /usr/local/bin/dockutil --add '/Applications/Microsoft Teams.app'

#reload dock after 5 seconds
sleep 5
killall dock
2 REPLIES 2

cbrewer
Valued Contributor II

Are you sure the Dock is loaded at the time you are trying to make these changes? You could add something like this to the beginning:

# Don't do anything until the Dock has started
if [[ ! $(pgrep -x Dock) ]]; then
  until [[ $(pgrep -x Dock) ]]; do
    echo "Dock not started. Waiting 1 second..."
    sleep 1
  done
  echo "Dock started at: $(date +"%r")"
else
  echo "Dock already started"
fi

As for running your dockutil command as the user, try it like this:

su -l ${currentUser} -c "/usr/local/bin/dockutil --add /Applications/Microsoft Teams.app --no-restart"

Also, try running your Dock restart as the user:

su -l ${currentUser} -c "killall Dock"

Lastly, I'm not sure you need to kill cprefsd. I never have.

christopher_bec
New Contributor

I dealt with this issue for a while and started using dockutil with scripts similar to yours. I ended up pairing my dockutil scripts with outset. If you use the dockutil script with the login-once folder of outset, that should take care of what you're trying to do, it ends up much easier, simpler, and reliable. Outset's login scripts would run in the user context too so you wouldn't need to care about running dockutil as the user or gathering the current user.

I would recommend modifying your script slightly too, it's better to start with a blank dock and add what you need. An example of one of mine for iMac labs below.

#!/bin/bash

/usr/local/bin/dockutil --remove all --no-restart # clear the dock
/bin/sleep 2 # sleep to allow dock to re-initialize a blank dock, dock will not be drawn correctly if not using sleep

/usr/local/bin/dockutil --add /Applications/Google Chrome.app/ --no-restart
/usr/local/bin/dockutil --add /Applications/Safari.app/ --no-restart
/usr/local/bin/dockutil --add /Applications/iMovie.app/ --no-restart
/usr/local/bin/dockutil --add /Applications/Microsoft Word.app/ --no-restart
/usr/local/bin/dockutil --add /Applications/Microsoft Excel.app/ --no-restart
/usr/local/bin/dockutil --add /Applications/Microsoft PowerPoint.app/ --no-restart
/usr/local/bin/dockutil --add /Applications/Google Earth Pro.app/ --no-restart
/usr/local/bin/dockutil --add ~/Downloads --view fan --display stack --no-restart
/usr/local/bin/dockutil --add http://myurl.org/page/ --no-restart
/usr/local/bin/dockutil --add http://www.myurl.com/page/resources

You could do login-every if you want to force the dock to reset to the standard every time too.