Hard drive on desktop

cyberspread
New Contributor

Trying to get my HD and connected servers to be on the desktop by default. I have a script that, when ran on my main computer, works great..... When I try to use the trigger on the test machine it doesnt seem to want to relaunch the finder even though I have the killall Finder command in the script.....

!/bin/sh

Enable Desktop Mounted Media

defaults write com.apple.finder ShowExternalHardDrivesOnDesktop -bool true
defaults write com.apple.finder ShowHardDrivesOnDesktop -bool true
defaults write com.apple.finder ShowRemovableMediaOnDesktop -bool true
defaults write com.apple.finder ShowMountedServersOnDesktop -bool true
killall Finder

I have that set in as a Policy in JSS. When I run the custom trigger, it doesn't fully relaunch the Finder as it does on my main machine.

2 ACCEPTED SOLUTIONS

mm2270
Legendary Contributor III

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.

  1. Find the logged in user and use a full path to the user's preference files to use with defaults write
  2. 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.

View solution in original post

bentoms
Release Candidate Programs Tester

Why not use a profile for this?

optional image ALT text

View solution in original post

11 REPLIES 11

bburdeaux
Contributor II

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

cyberspread
New Contributor

LSOpenURLsWithRole() failed with error -600 for the file /System/Library/CoreServices/Finder.app.

Received the following error using either "open" command.

mm2270
Legendary Contributor III

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?

cyberspread
New Contributor

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.

mm2270
Legendary Contributor III

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.

  1. Find the logged in user and use a full path to the user's preference files to use with defaults write
  2. 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.

bburdeaux
Contributor II

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.

bentoms
Release Candidate Programs Tester

Why not use a profile for this?

optional image ALT text

cyberspread
New Contributor

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.

cyberspread
New Contributor

I'll do some research on the Config Profile and see if I can use that.

Thanks.

blackholemac
Valued Contributor III

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 .

cyberspread
New Contributor

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.