mount volume with read/write file script

Hi,
Basically, I have to connect and reconnect to the same servers all day long and I got tired of having to command + k everytime, so I wrote an applescript to do this all for me:

property uName : "ryan"
property uPass : ""
tell application "Finder"
	-- Edit this to your settings
	display dialog "I am now going to mount the Server." buttons {"Mount", "Cancel"} default button 1 with icon 1
	set theServer to "afp:/at/CSG4:*"
	display dialog "Enter Your User Name:" default answer uName
	set theUser to the text returned of the result
	display dialog "Enter Your User Password:" default answer uPass
	set thePass to the text returned of the result
	try
		mount volume theServer as user name theUser with password thePass
		display dialog "The Server has been mounted." buttons {"OK"} default button 1 with icon 1
		---- launch the app.
	on error
		display dialog "I'm Sorry. CSG4 is not available right now."
	end try
end tell

And this works… except laziness gets the best of me again. I never change my username and pass and I am tiring of typing in my pass as it rarely changes.
So i changed it to read my pass from a text file:


property uName : "ryan"
property uPass : ""
tell application "Finder"
	-- Edit this to your settings
	set button_pressed to button returned of (display dialog "I am now going to mount the Server." buttons {"Mount", "Cancel", "Edit Password"} default button 1 with icon 1)
	if button_pressed is "Mount" then
		set theServer to "afp:/at/CSG4:*"
		set thePath to ":Volumes:Macintosh HD:Users:ryan:Library:Scripts:Mounter:"
		set theFile to (thePath & "csg4pass2.txt") as string
		set fileRef to (theFile) as alias
		set fileRef to (open for access theFile)
		set thePass to ""
		set thePass to (read fileRef)
		close access fileRef
		tell application "Finder"
			set theUser to uName
			mount volume theServer as user name theUser with password thePass
			--display dialog "The Server has been mounted." buttons {"OK"} default button 1 with icon 1
			---- launch the app.
		end tell
	else
		display dialog "Enter Your User Password:" default answer uPass
		set thePass to the text returned of the result
		set thePath to ":Volumes:Macintosh HD:Users:ryan:Library:Scripts:Mounter:"
		set theFile to (thePath & "csg4pass2.txt") as string
		set fileRef to (theFile) as alias
		set fileRef to (open for access theFile with write permission)
		set eof of fileRef to 0
		write thePass to fileRef
	end if
end tell

When looking at the event log, i see the same events being passed thru, but I get the normal dialog box asking me for my username and pass instead of just mounting the drive. Any ideas why? If not, it is no big deal as you would just be assiting in my laziness. Next step, Mojo, the monkey helper!
Thanks,
Ryan

I can’t see major problems in your code, except for some minor issues. What if you try the code directly?

mount volume "afp:/at/CSG4:*" as user name "ryan" with password "yourpwd"

--> or

mount volume "afp://ryan:yourpwd@afp.at/CSG4"

-- If this works, you can simply:

mount volume ("afp://ryan:" & (read alias "path:to:pwdfile.txt") & "@afp.at/CSG4")

I can connect via the first method, but the second two methods do no work.
I get an error saying the the server does not exsist. I have never used that method to connect, is it supposed to work?
I have used

mount volume "afp://ryan:yourpwd@myIPaddress"

and that has works.
any ideas why

mount volume "afp://ryan:yourpwd@afp.at/CSG4"

doesnt?
Thanks for your help,
ryan

No, your code is fine:

mount volume "afp://user:pwd@server/dir"

Now, you don’t need the Finder to “mount volume” and you don’t need open the file to read it. You can reduce it all to:

mount volume "afp://user:" & (read alias "path:to:pwdfile.txt") & "@ipaddress/volumeName"

(use volumeName only if you need it)

doh!
After I had opened the text file to check and see that the write part of the script had worked, textedit converted it into rich text, and that messed up the password. I changed it back to plain text and it works, via IP. Tried to change it back to the server name and not ip address, but that still didnt work. Dunno, but with ip it works.

**** Update****
well, I thought textedit was the evildooer, but it turns out that it is the write to file command. I create the text file, run the script, and everything works fine. Then I attempt to change the text file via the applescript and it doesnt properly write the new password. Whether i connect to a smb or an afp server, it re-asks me for the password, because I think the imported pass is wrong. When I try and delete the pass and retype it via simpletext, it seems that there are a bunch of extra unseen characters, ie say the pass is 7 character, it takes 14 taps of the delete key to actually delete the text. Weird… any ideas?

Thanks for you help!
Ryan

Hi there.

Thanks for the help on the code! This is exactly what I needed because I have a machine which I took to Ten which, for some reason, will not mount servers on startup. This helps, however I am wondering if there is anyway to tell the script to select all the volumes, and simply mount them without the dialog?

Also I am new to scripting, and I am trying to find some good resources to use learn.

I bought “Learn Applescript in 24 hours”, and i have the Definitive guide from O’Reilly on order. Any other suggestions?

Thanks

John Gibson