Mounting servers

For years I’ve been using file commands to mount server volumes. Lately I’ve noticed that it’s not very reliable, and the connection isn’t as robust as when I use “Connect to Server…” from Finder’s “Go” menu.

So I’m wondering what is the best practice these days for mounting server volumes via appleScript?

Are there AppleScriptObjC commands; Shell scripts?

mount volume "smb://server/volume" as user name Username with password Password```

You could try this:

use framework "Foundation"
use framework "AppKit"
use scripting additions

set theURL to current application's NSURL's URLWithString:"smb://userName:password@server/volume"
current application's NSWorkspace's sharedWorkspace()'s openURL:theURL

Just make sure you encode the name and password correctly.

If you wanted to pass the name and/or password in at run time, you could use this:

my mountVolume:"smb://server/volume" userName:"Me" withPassword:"top^secret"

on mountVolume:hostAndPath userName:theName withPassword:thePwd
	set theName to current application's NSString's stringWithString:theName
	set theName to theName's stringByAddingPercentEncodingWithAllowedCharacters:(current application's NSCharacterSet's URLUserAllowedCharacterSet())
	set thePwd to current application's NSString's stringWithString:thePwd
	set thePwd to thePwd's stringByAddingPercentEncodingWithAllowedCharacters:(current application's NSCharacterSet's URLPasswordAllowedCharacterSet())
	set urlString to current application's NSString's stringWithString:hostAndPath
	set nameAndPwd to current application's NSString's stringWithFormat_("://%@:%@@", theName, thePwd)
	set urlString to urlString's stringByReplacingOccurrencesOfString:"://" withString:nameAndPwd
	set theURL to current application's NSURL's URLWithString:urlString
	current application's NSWorkspace's sharedWorkspace()'s openURL:theURL
end mountVolume:userName:withPassword:

That said.


Was there more to say?

So both work great. This is exactly what need, but I’m wondering which is better (more robust?) the version in the handler or the two lines.

Considering that I’ll be hard coding the servers/volumes in each script it do I need the character filtering in the handler? What other benefit is there?

With the handler if the username or password are incorrect I get a dialog, but not with the two lines. But they give me a trapable error and I can ask the user for the password and save it in a property (which I know isn’t that secure, and is probably what your “that said” was leading to!).

Either way, thanks, it’s exactly what I needed and gives me a better result than what I’ve been using for years.

Well I was going to say I’d be surprised if it gave a different result, but that seems to have been overtaken by events…

If the name or password contain illegal characters, they can mess up the whole URL, whereas the longer version effectively confines any problem to just the invalid name or password.

The longer version is probably going to save you time next time someone changes the passwords, because you won’t even have to think about encoding them.

Ed, would you elaborate on “robust”? Robust as in, sometimes it just doesn’t work, or…?

Any of these definitions work:

adjective (robuster, robustest)
strong and healthy; vigorous: the Caplans are a robust, healthy lot.
• (of an object) sturdy in construction: a robust metal cabinet.
• (of a process, system, organization, etc.) able to withstand or overcome adverse conditions: California’s robust property market.