You can try adding an open command after the killall. Either of the examples below should work.
open /System/Library/CoreServices/Finder.app
open -a Finder
LSOpenURLsWithRole() failed with error -600 for the file /System/Library/CoreServices/Finder.app.
Received the following error using either "open" command.
Is the issue you're running into that the Finder won't restart at all, or is it restarting and failing to come back up fully?
It looks like the finder is restarting but the commands other than the killall Finder are not working.
When I run the custom trigger, the icons on the desktop disappear and come back but no HD or connected servers show up and the preferences remain unchecked.
I created the script in TrextWrangler and ran it on the machine it was created on and it worked just as it should.
Oh! I didn't even really look closely at your script. The problem is you're trying to run the command against the preferences for the account running the script. which is likely going to be either your management account, or root.
In short, whenever you use syntax with defaults like defaults write com.company.domain
without referencing the full path to the plist, defaults uses the path of the account running the script to locate the plist. In this case, you want it to affect the logged in user, not root or your Jamf management account. So in essence, it IS working, just not on the logged in account, which is why it doesn't appear to be working.
There are several ways to fix this.
- Find the logged in user and use a full path to the user's preference files to use with defaults write
- Try running the commands as the user, like
sudo -u $user <commands>
Personally I have mixed results with option 2, and much better results with option 1. However, there is another way to do option 2 that is more reliable, using the launchctl asuser
method. See this post for an example of how it works.
Lastly, have you considered using a Config Profile for this instead of trying to script it? Is it something you want to enforce, or just set once? A Configuration Profile can let you set and enforce the setting I believe.
The finder preferences are user specific, so the defaults write command has to be run as the logged in user to affect that user's settings. Luckily, since the script is being run as root, you can use su -l to run the command as the current user without a password prompt, though you will still need to target the plist directly.
#!/bin/bash
currentuser=$(/bin/ls -la /dev/console | /usr/bin/cut -d ' ' -f 4)
su -l $currentuser -c "defaults write /Users/$currentuser/Library/Preferences/com.apple.finder ShowExternalHardDrivesOnDesktop -bool true"
su -l $currentuser -c "defaults write /Users/$currentuser/Library/Preferences/com.apple.finder ShowHardDrivesOnDesktop -bool true"
su -l $currentuser -c "defaults write /Users/$currentuser/Library/Preferences/com.apple.finder ShowRemovableMediaOnDesktop -bool true"
su -l $currentuser -c "defaults write /Users/$currentuser/Library/Preferences/com.apple.finder ShowMountedServersOnDesktop -bool true"
killall Finder
I don't remember if this method does anything funny with the permissions on the plist, so you may need to throw a chown/chmod at the end.
Why not use a profile for this?

Thank for all the info. I will try messing around with some of this tomorrow.
I am relatively new to this but picking things up pretty quick.
How am i able to do this with a profile, bentoms? It looks like you tried to attach a pic but its not showing.
I'll do some research on the Config Profile and see if I can use that.
Thanks.
I actually am doing that now with a profile ...back in the day I converted my old MCX to a profile with mcxtoprofile. I don't have a choice ... some of my old Mac OS 9 users expect to see those there were they were a file I work ticke expect to see those there were they or they will file a ticket... I will gladly share my profile once I get back to the office .
I figured it out. Looks like there is a lot of power in Config Profiles and very easy to setup and scope out.
Thanks everyone.