Script to edit 'Connect to Servers' saved favourites

MattT
New Contributor III
New Contributor III

Hey all,

Trying to find a less invasive way of editing users Connect to Servers saved favourite addresses. Currently we swap out the com.apple.sidebarlists.plist which does the job but the user of course loses any customisation made to their Finder Sidebar.

Im assuming we use the defaults write command, but I am unsure of the syntax to specify the addresses to be saved.. It would also be ideal if the saved list was reset before adding the addresses.

Has anyone had any experience with this?

Alternatively, is there a better way to do this?

19 REPLIES 19

timdambrosio
Contributor

You can have a single server mount script with variables for server name, share, afp/smb.

Put that script in a policy and make it available in self service.

Make a policy for each share you want to make available for users.

cdev
Contributor III

Depending to what level of customization you want to use, I've been able to successfully use plistBuddy to modify just those items:

/usr/libexec/PlistBuddy -c 'delete favoriteservers:CustomListItems' "${USER}"/Library/Preferences/com.apple.sidebarlists.plist
/usr/libexec/PlistBuddy -c 'add favoriteservers:CustomListItems array' "${USER}"/Library/Preferences/com.apple.sidebarlists.plist
/usr/libexec/PlistBuddy -c 'add favoriteservers:Controller string "CustomListItems"' "${USER}"/Library/Preferences/com.apple.sidebarlists.plist
/usr/libexec/PlistBuddy -c 'add favoriteservers:CustomListItems:0:Name string "afp://servername"' "${USER}"/Library/Preferences/com.apple.sidebarlists.plist
/usr/libexec/PlistBuddy -c 'add favoriteservers:CustomListItems:0:URL string "afp://servername"' "${USER}"/Library/Preferences/com.apple.sidebarlists.plist

The first couple of items will clear the list of any items, then re-create the entry. The last two lines add the bookmark name and URL. For additional entries, just update the 0 to 1, 2, 3, etc.

rtrouton
Release Candidate Programs Tester

I have a post on how I've updated existing Connect to Server bookmarks available from here:

https://derflounder.wordpress.com/2013/01/10/updating-server-bookmarks-in-com-apple-sidebarlists-pli...

Aaron
Contributor II

I remember looking into this before, and not being able to find a clean solution (at the time). So I made my own little AppleScript app which I pushed out to all clients (and made a shortcut on the desktop/dock). Basically, you run it, choose what you want from the list, and it mounts it for you. I've found it to be very handy for those that don't even know about the "Connect to Server" option.

My very simple script goes as such:

set serverList to {"(S:)   Research", "(Q:)  CancerBiol", "(T:)    CancerGenomics", "(U:)    CoreFac"}
choose from list serverList with prompt "Choose a server..."
if result is not false then open location "smb://server/" & (characters 6 thru -1 of (result as text))

It's simple in the way that I only have one server with multiple shares to mount (for the departments I look after, anyway). You would probably need to fiddle with it to suit your needs.

external image link

bentoms
Release Candidate Programs Tester

@cdev I used to be a big fan of PlistBuddy, but then ran into CFPRefsD: https://macmule.com/2014/02/07/mavericks-preference-caching/

When are you deploying your script?

cdev
Contributor III

@bentoms Our script is running during imaging time in the adobeinstall user. We have it as part of a set of configuration scripts. I've been moving as much as possible off of plistbuddy and onto the defaults write command, and it's definitely been a process...

bentoms
Release Candidate Programs Tester

@cdev, ah.. So it's writing to the user templates? Then that'll work fine (as you've seen). Editing in use plists fails with plistbuddy due to cfprefd.

MattT
New Contributor III
New Contributor III

Hey timjd4,

I am trying this, does the script in the MySQL database need to be bash? Have tried with AppleScript and get the following:

Executing Policy Year 11 Student Drives...
[STEP 1 of 1]
Running script Network Drives - Graduating 2016 - Mount...
Script exit code: 127
Script result: /Library/Application Support/JAMF/tmp/Network Drives - Graduating 2016 - Mount: line 2: username: command not found /Library/Application Support/JAMF/tmp/Network Drives - Graduating 2016 - Mount: line 3: tell: command not found /Library/Application Support/JAMF/tmp/Network Drives - Graduating 2016 - Mount: line 4: try: command not found mount: You must specify a filesystem type with -t. /Library/Application Support/JAMF/tmp/Network Drives - Graduating 2016 - Mount: line 6: end: command not found /Library/Application Support/JAMF/tmp/Network Drives - Graduating 2016 - Mount: line 7: end: command not found

Do you have an example I could take a look at? Bit of a novice at this, cheers.

timdambrosio
Contributor

@mtaylor934 this is the AFP version. It will mount using kerberos if they have a valid ticket, if not it will prompt for password. Define your server and share values in the policy itself.

#!/bin/sh

#Set Parameters in policy
# $4 = Server name or IP address of Server
# $5 = Share name

#Find the logged in user
user=`ls -la /dev/console | cut -d " " -f 4`

#Run Applescript to check for active kerberos ticket
sudo -u $user osascript<<END
try
    -- test for Kerberos ticket presence and attempt to renew
    do shell script "/usr/bin/klist | /usr/bin/grep krbtgt"

on error
    try
        set thePassword to text returned of (display dialog "Connecting to $5 on $4 requires you to enter your password" default answer "" with icon file "System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:GenericFileServerIcon.icns" giving up after 60 with hidden answer)
        do shell script "/bin/echo '" & thePassword & "' | /usr/bin/kinit -l 10h -r 10h --password-file=STDIN"
    on error
        try
            set thePassword to text returned of (display dialog "Password incorrect. Please try again:" default answer "" with icon 2 with hidden answer)
            do shell script "/bin/echo '" & thePassword & "' | /usr/bin/kinit -l 10h -r 10h --password-file=STDIN"
        on error
            display dialog "Too many incorrect attempts. Stopping to avoid account lockout." with icon 2 buttons {"OK"} default button 1
        end try
    end try

end try
END

#Mount share
sudo -u $user /usr/sbin/jamf mount -server $4 -share $5 -type afp -username $user -visible

MattT
New Contributor III
New Contributor III

Thanks for that Tim. If I wish to mount via smb, can I simply swap out afp for smb or is it more complicated?

timdambrosio
Contributor

It's slightly different with smb. You don't need the username and it won't work if you include it from memory. You could combine these 2 scripts to allow for that but I never got around to it.

sudo -u $user /usr/sbin/jamf mount -server $4 -share $5 -type smb -visible

bentoms
Release Candidate Programs Tester

For a full AppleScript method: click here

jacob_salmela
Contributor II

I use something similar to @cdev :

#!/bin/bash
bud='/usr/libexec/Plistbuddy'
plist=$HOME'/Library/Preferences/com.apple.sidebarlists.plist'

servers=('afp://servername'
'smb://servername'
'vnc://servername'
'ftp://servername')

killall cfprefsd
echo "Setting servers for $plist"
echo "Removing previous entries..."
${bud} -c "Delete favoriteservers" ${plist}

echo "Creating new list..."
${bud} -c "Add favoriteservers:Controller string CustomListItems" ${plist}
${bud} -c "Add favoriteservers:CustomListItems array" ${plist}

for i in "${!servers[@]}"
do
    echo "Adding to Favorite Servers: ${servers[$i]}..."
    ${bud} -c "Add favoriteservers:CustomListItems:$i:Name string ${servers[$i]}" ${plist}
    ${bud} -c "Add favoriteservers:CustomListItems:$i:URL string ${servers[$i]}" ${plist}
done

echo "Finalizing settings..."
killall cfprefsd
defaults read ${plist} favoriteservers > /dev/null

It takes effect on logout/login. I'm not sure what process to kill to get the changes to happen right away.

eWhizz
New Contributor II

Someone with more scripting skills than I could use the following psuedo code to get this done

Get existing favorites using defaults read com.apple.sidebarlists favoriteservers

Create a new correctly structured plist file from the result of the above defaults command and adding any new one using plistbuddy, then use
defaults import com.apple.sidebarlists new_plist_file.plist to import the plist file.

All the while satisfying cfprefsd, and keeping the users existing 'Connect To Server' favorites.

eWhizz
New Contributor II

This should work, but will obliterate any existing favoriteservers.
You could always defaults read, modify the results and then defaults write in this format.

The white space below is for clarity and defaults write handles it just fine too, so long as you contain keys in quotes.

defaults write com.apple.sidebarlists favoriteservers '{ Controller = CustomListItems; CustomListItems =  (
{
            Name = "Share";
            URL = "afp://<username>:<password>@server.domain.com/Share";
            }
      );
}'

fh27317
New Contributor

Hello,
very interesting information found here, and makes a lot of sense, but running OSX 10.11.6, i still find no means to make the modifications effective. The "com.apple.sidebarlists favoriteservers" array has all the entries with the proper syntax and share names, and i did kill Finder and cfprefd after kodifying the preference, even logged out the user and rebooted the machine, even applied all that to an local administrative account, thus the voodoo section should be covered, but the entries of the new com.apple.sidebarlists favoriteservers simpy never show up in the GUI. Otherway around: entries applied to the "Favorite Servers" via CMD&K never show up in any file under ~/Library/Preferences...
Do i miss s.th. essential, or has the direct editing of preferences been further locked down by APPLE in the meantime?
Would tremendously appreciate any hint, as i can't stand adding all the servers for each user interactively any longer ;-)

Best

fh27317
New Contributor

Hello,
i know it's an old post...
Just wanted to ask the other way around, whether anyone out there on OSX 10.11.6 is able to apply the measure discussed here successfully?
Any information highly appreciated...
Best

LukeMason
New Contributor III

Anyone trying to figure out how to edit Connect to Server favorites on 10.11.6 & up should look here:

https://www.jamf.com/jamf-nation/discussions/20218/the-slftool-thread

The relevant command is:

/usr/bin/sfltool add-item -n "foobar" com.apple.LSSharedFileList.FavoriteServers "afp://foo.bar"

credit to @djdavetrouble

Maxalot
New Contributor III

Apple changed some things. That's why it doesn't work. @djdavetrouble 's link care of @LukeMason is a good place to start and of course, Google is your friend. I have been fiddling with single plist edits and have decided I need to shift into a higher gear on this and automate logins to mount home and OU shares.