network mount script- notify user of name/password errors

Hi all.

I am quite new to applescript and have a question if it is possible to extend the functionality of the script I am using.

At the moment I am using the script below to handle mounting different password protected network shares around my network.

In all honesty I have somewhat modified a simple script I found kicking about on the web for my own purposes and it seems to work really neatly, but I’d like to notify the user of an incorrect username/password in the event that credentials are incorrectly inputted.

Currently what happens is the finder connect to server dialogue will appear which is annoying. Ideally I’d like my little apple script based connection utility to reply back with an error message and send the user back to the top for a second go.

set user_name to ""
set Dialog_1 to display dialog "Please enter your user name for Cookie Share" default answer ""
set the user_name to the text returned of Dialog_1

set pass_word to ""
set Dialog_2 to display dialog "Please enter your password  for Cookie Share" default answer "" with hidden answer
set pass_word to the text returned of Dialog_2

tell application "Finder"
	try
		mount volume "afp://" & user_name & ":" & pass_word & "@3.9.55.1/"
	end try
end tell

I’ve looked into this as best I can as a complete newbie, but am now struggling to implement it.

you don’t have labels (goto) in applescript but you can use a loop to jump back in code like this:

repeat 3 times --after three attemps you'll stop
	set user_name to ""
	set Dialog_1 to display dialog "Please enter your user name for Cookie Share" default answer ""
	set the user_name to the text returned of Dialog_1
	
	set pass_word to ""
	set Dialog_2 to display dialog "Please enter your password for Cookie Share" default answer "" with hidden answer
	set pass_word to the text returned of Dialog_2
	
	try
		mount volume "afp://" & user_name & ":" & pass_word & "@3.9.55.1/"
		exit repeat
	end try
end repeat

Hello, thank you for the reply.

Your method of using ‘repeat’ works similarly to a differnt way I tried.

I don’t think I explained very well.

The problem I encounter is that on failure to connect or incorrect credentials supplied by a user Finder always pop’s up the default Apple ‘connect to server’ box, which effectively stops the script from running.

Now this wouldn’t be a problem, but I’d like to have the script display an error message and give the user the option to try again.

Now I can put in an ‘on error’ message but this is only actually displayed if the user clicks cancel on the Finder ‘connect to server’ box.

:smiley:

EDIT: It seems that this user had the same issue back in 2009, but his thread died.

http://macscripter.net/viewtopic.php?pid=122431#p122431

Picture of said ‘connect to server’ window here…

http://i21.photobucket.com/albums/b267/lucky_luc/Picture1.png

Sorry for misunderstanding. It was late when I read your post.

I must say that I never mount a volume with mount volume from standard additions. Instead I use shell commands mkdir, test, ls, wc, diskutil and mount. The following code should work for you but the code is bits and pieces of applescriptObjC classes I’ve written. The strange naming of handlers and some strange settings are the result of that. But this is the basis of how I mount a volume using shell applications.

The functions:

on mountVolume_onServer_asUser_withPassword_toNode_(serverPath, serverName, serverUser, serverPassword, localNode)
	if isMounted_(localNode) then
		return
	end if
	set networkProtocol to "afp"
	if createLocalNode_(localNode) then
		set mountURL to networkProtocol & "://" & serverUser & ":" & serverPassword & "@" & serverName & "/" & serverPath
		do shell script "mount -t " & networkProtocol & space & quoted form of mountURL & space & quoted form of localNode
	end if
end mountVolume_onServer_asUser_withPassword_toNode_

on createLocalNode_(localNode)
	if not fileExists_(localNode) then
		do shell script "mkdir " & quoted form of localNode & "; echo $?"
		return fileExists_(localNode)
	end if
	if (do shell script "ls -A " & quoted form of localNode & " | wc -l") as integer is 0 then
		return true
	else
		return false
	end if
end createLocalNode_

on unMount_(localNode)
	if not isMounted(localNode) then
		return
	end if
	do shell script "diskutil unmount " & quoted form of localNode
end unMount_

on fileExists_(posixPath)
	return not ((do shell script "test -e " & quoted form of posixPath & "; echo $?") as integer) as boolean
end fileExists_

on isMounted_(localNode)
	return (do shell script "mount") contains (" on " & localNode & " (")
end isMounted_


Usage:

mountVolume_onServer_asUser_withPassword_toNode_("", "3.9.55.1", "user_name", "pass_word", "/Volumes/mountPoint") 

Thanks for the reply again.

I’m a little lost as to how to implement this with Apple Script :smiley:

Copy everything from his first block of code (or hit the “Open this Scriplet in your Editor:” link), paste it into your AppleScript at the end (or after the “end run” line if you aren’t using an implicit “on run” block).

Then, wherever you want to connect to an AppleShare volume, do something like this:

mountVolume_onServer_asUser_withPassword_toNode_("YourVolumeName", "YourServerName.local", "someuser", "fakepass", "/Volumes/YourVolumeName")

where YourVolumeName is the name of the volume on the server and YourServerName.local is the Bonjour address of the server (or replace “YourServerName.local” with something like “192.168.1.133” or whatever the IP Address is). Note that this also allows you to specify the mount point (where it appears in the Finder). If you want the network volume to mount in the normal location, use “/Volumes/YourVolumeName” (replacing YourVolumeName with whatever your volume’s name is).

Newby question. Are the two applescripts above supposed to be combined in 1 applescript or run together? I’m getting the following error when trying to run the script (naturally, i’ve hidden the actual real answers to my specific server volume);

mountVolume_onServer_asUser_withPassword_toNode_(“a-real-volume-on-my-server”, “my-hidden-ip-addr”, “myusername”, “something-secure”, “/Volumes/a-real-volume-on-my-server”)

error msg;

error “«script» doesn’t understand the mountVolume_onServer_asUser_withPassword_toNode_ message.” number -1708 from «script»

Does the “mountVolume…” command exist on every mac or is this new program I’m creating?

Combine the two applescripts and it should work like expected. The first code shouldn’t do anything, see it as organized stored code that is waiting to be used. Then the second script (you need to combine the two or reference to the first script) is how you can mount with a single command by calling the stored pieces of code from above. If you don’t care about the Finder warning and also don’t care where your AFP volume will be mounted to you should ‘mount volume’ from standard additions instead and not the code I posted here.