A lot of people have asked me lately about mounting file servers based on certain user criteria. Usually this is based on whether or not a user is a member of certain user groups (faculty, graphics design, engineering…). This is actually a pretty easy task for a quick AppleScript and I’m going to present a template script that you can use as well as instructions for customizing it to suit your particular needs.

The basic AppleScript command mount volume is part of the Standard Additions. It accepts one main argument, the URL for the server including share point; as a string. You can also pass a username and password.

For example:

mount volume “afp://server.ip/share/”

If you have integrated into a single sign on solution like Open Directory, MIT Kerberos, or Active Directory (yes, they are all really Kerberos-based) then you don’t need a username or password at all. The System will try to use the user’s Kerberos credentials and the volume will mount without any additional user interaction needed.

Mounting the server volume is easy, the more difficult part is determining what volumes to mount. Really, if you’re going to have all users mount the same volume all the time you are better off using Managed Client with Workgroup Manager (a part of Mac OS X Server). Barring that you’ll need to build some logic into the system.

Generally there is some kind of identifier attached to a user’s directory entry. Usually this is based on which groups a user is in. For example, all engineers are in the ‘engineering’ group and connect to the ‘Engineering’ share and the designers are in the “Design” group and connect to the “Graphics_Design” and “ClipArt” shares.

For my sample, I will use the above logic.

First off, lets get info about the currently logged in user. We’ll use the Unix command id. id will respond with something that looks like this:

uid=501(brett) gid=20(staff) groups=20(staff), 98(_lpadmin), 81(_appserveradm), 101(com.apple.sharepoint.group.1), 79(_appserverusr), 80(admin)

The following AppleScript code will get that result into the variable userInfo:

set theScript to “/usr/bin/id”
set userInfo to do shell script theScript

AppleScript supports the if… then… else… construct. In my example no user is both and engineer and designer, but you can easily use multiple if… then… blocks to handle users that could be part of multiple groups.

To mount volumes as per my above example, the following code would be added:

if (userInfo contains “Engineering”) then
   mount volume “afp://server.ip/Engineering/”

else if (userInfo contains “Engineering”) then
   mount volume “afp://server.ip/Graphics_Design/”
   mount volume “afp://server.ip/ClipArt/”

end if

It’s pretty easy to put it all together in Script Editor (in /Applications/AppleScript/) then save it as an Application. You can deploy this script to all of your systems using Remote Desktop and use Workgroup Manager to set it as a Login Item.

Enjoy!

Technorati Tags: , ,

Share