Help with scrpit

TheBigRed
New Contributor

Hi I'm trying to run the following script via a policy.

# Show Hard Drives On The Desktop

defaults write com.apple.finder ShowHardDrivesOnDesktop -bool true

# Show Mounted Servers On The Desktop

defaults write com.apple.finder ShowMountedServersOnDesktop -bool true

# Show External Hard Drives On The Desktop

defaults write com.apple.finder ShowExternalHardDrivesOnDesktop -bool true

killall Finder

exit 0

Script runs fine on my machine, but the test machine I'm scoping to does nothing. The logs show:

Executing Policy ShowIcons
Running script ShowIcons...
Script exit code: 0
Script result: No matching processes were found

Anyone able to help me?

4 REPLIES 4

mm2270
Legendary Contributor III

Anytime a defaults write (or read) command does not include the full path to a plist, but just references it by it's domain, like in the above defaults write com.apple.finder commands, defaults operates on the plist for the account that is running the script or shell command. In the case of a policy, that is usually going to be the root account, not the account of the user that's logged into the Mac. That's why it's not doing what you expect - because it's changing the settings on the root account, not on the logged in user.

What you need to do is either run those commands as the current user, so the shell knows which plists to affect, or include the full path to the plist in them.

So for example, using my second suggestion, you could do

#!/bin/sh

loggedInUser=$(stat -f%Su /dev/console)

defaults write /Users/$loggedInUser/Library/Preferences/com.apple.finder.plist ShowHardDrivesOnDesktop -bool true
defaults write /Users/$loggedInUser/Library/Preferences/com.apple.finder.plist ShowMountedServersOnDesktop -bool true
defaults write /Users/$loggedInUser/Library/Preferences/com.apple.finder.plist ShowExternalHardDrivesOnDesktop -bool true

chown $loggedInUser /Users/$loggedInUser/Library/Preferences/com.apple.finder.plist

In the above, I'm capturing the name of the user logged in and (assuming their home directory is in /Users/) direct the defaults write commands to their com.apple.finder.plist file. Finally, for good measure, I make sure they are still the owner of the plist in case the commands ended up messing up the permissions on it.

The other way is to run the same commands as you have above AS the user, instead of root. There are a few different ways to do this. Some are better than others, but may also be more complicated. I'll leave that to you to do some searches on here to find posts on how to run commands as the current user.

EDIT: I forgot to ask one thing. Why not do these as a Configuration Profile instead of using defaults write commands? A Config Profile can do these and would set the settings in a more permanent way, in case that's something of interest to you.

karthikeyan_mac
Valued Contributor

@TheBigRed You can try the below method as well.

#!/bin/sh
loggedInUser=$(stat -f%Su /dev/console)

# Show Hard Drives On The Desktop

sudo -u $loggedInUser defaults write com.apple.finder ShowHardDrivesOnDesktop -bool true

# Show Mounted Servers On The Desktop

sudo -u $loggedInUser defaults write com.apple.finder ShowMountedServersOnDesktop -bool true

# Show External Hard Drives On The Desktop

sudo -u $loggedInUser defaults write com.apple.finder ShowExternalHardDrivesOnDesktop -bool true

killall Finder

exit 0

-Karthikeyan

kerouak
Valued Contributor

without the 'sudo' if you are running the script as a policy.

TheBigRed
New Contributor

Thanks everyone, I did end up using a config profile to achieve this.