Connect to server monterey

claudio_provini
New Contributor III

Hi to all,

with catalina i used a python script to add favourite server under finder (command -k) now with monterey that has no python2 this script doesn't work. Here the script:

#!/usr/bin/python

"""
Overwrites server favorites with servers.
Run as root to update all users or as normal user to update just that user.
"""

import os
import getpass
import subprocess
import uuid

import Foundation

favorites_path = "/Users/{user}/Library/Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.FavoriteServers.sfl2"

# Use a tuple: ("<name>", "<path>") to set a name for the favorite.
# Otherwise just use a string and the path will be used as the name
servers = (( "smb://server1"), "smb://server2","smb://server3","smb://server4" )

def get_users():
    "Get users with a home directory in /Users"

    # get users from dscl
    dscl_users = subprocess.check_output(["/usr/bin/dscl", ".", "-list", "/Users"]).splitlines()

    # get home directories
    homedir_users = os.listdir("/Users")

    # return users that are in both lists
    users = set(dscl_users).intersection(set(homedir_users))
    return [u.strip() for u in users if u.strip() != ""]

def set_favorites(user, servers):
    "Set the Server Favorites for the given user"

    # generate necessary structures
    items = []
    for server in servers:
        name = server[0] if len(server) == 2 else server
        path = server[1] if len(server) == 2 else server
        item = {}
        # use unicode to translate to NSString
        item["Name"] = unicode(name)
        url = Foundation.NSURL.URLWithString_(unicode(path))
        bookmark, _ = url.bookmarkDataWithOptions_includingResourceValuesForKeys_relativeToURL_error_(0, None, None, None)
        item["Bookmark"] = bookmark
        # generate a new UUID for each server
        item["uuid"] = unicode(uuid.uuid1()).upper()
        item["visibility"] = 0
        item["CustomItemProperties"] = Foundation.NSDictionary.new()

        items.append(Foundation.NSDictionary.dictionaryWithDictionary_(item))

    data = Foundation.NSDictionary.dictionaryWithDictionary_({
        "items": Foundation.NSArray.arrayWithArray_(items),
        "properties": Foundation.NSDictionary.dictionaryWithDictionary_({"com.apple.LSSharedFileList.ForceTemplateIcons": False})
    })

    # write sfl2 file
    Foundation.NSKeyedArchiver.archiveRootObject_toFile_(data, favorites_path.format(user=user))

# loop through users and set favorites
if __name__ == "__main__":
    # if running as root, run for all users. Otherwise run for current user
    user = getpass.getuser()
    if user == "root":
        users = get_users()
    else:
        users = [user]

    for user in users:
        try:
            set_favorites(user, servers)
            # fix owner if ran as root
            if user == "root":
                os.system(("chown {user} " + favorites_path).format(user=user))
            print "Server Favorites set for " + user
        except Exception as e:
            # if there's an error, log it an continue on
            print "Failed setting Server Favorites for {0}: {1}".format(user, str(e))

    # kill sharedfilelistd process to reload file. Finder should be closed when this happens
    os.system("killall sharedfilelistd")

  I don't want to use composer, there is someone that have an alternately script or solution?

Thanks a lot to everyone

Claudio

7 REPLIES 7

howie_isaacks
Valued Contributor II

You could install Python on all of your managed systems that you want to be able to run this script. I know several Mac/Jamf Admins who have done this because they did not want to give up using Python. I never used Python much, so I was able to pivot quickly and just remove Python from my scripts. That's not something everyone else can do. When I used to manage a lot of Macs that needed to always connect to a server and mount shares, I used a configuration profile to do it. It worked really well. That might be a good alternative.

Hey Howie,

Do you have a resource I can use to do just that? With a Config Profile?

I don't know why but also with python installed (2.7 or 3) doesn't work anymore.

Configuration profile works but for our confort we want to make server available but not mounted automatically on startup, so i'm looking for a script that add favourite server after the enroll.

Thanks for your kindly reply

obi-k
Valued Contributor II

claudio_provini
New Contributor III

Thanks mvu,

my question is not oriented to mount server, this is possible with applescript and other bash, my question is to add our server to favourite window (command-k).

Is there anyway to do this without python?

i've notice that with python 2.7 installed my script doesn't work for "<module> import Foundation ImportError: No module named Foundation"

Thanks to all

obi-k
Valued Contributor II

Yes, you asked for an alternative solution above. This is the alternative I'm using.

claudio_provini
New Contributor III

Hi,

nobody has found another solutions to add favourite servers?

Thanks a lot

Claudio