Keychain Server Mounter

This script will mount any servers whose passwords are stored on your keychain. The built-in OS X server auto-mounting seems a bit complicated/unreliable (depending on which method you use) so I developed this script. The first time the script runs, you’ll need to say “allow” etc. as the program accesses your keychain. Some parts of the script have been recycled from other posters in this forum… Thanks for your contribution if you recognise a snippet of your code!!

OS version: OS X

set theShortUserName to do shell script "/usr/bin/whoami" --get the short userid. This is how your default keychain is labled.

tell application "Keychain Scripting"
	try
		set myKeyChain to keychain theShortUserName
		set theKeyList to every AppleShare key of myKeyChain
	on error
		display dialog "Sorry, there don't appear to be any appletalk servers on your user keychain" buttons {"OK"} default button 1 with icon stop giving up after 15
	end try
	
	if length of theKeyList is 0 then
		display dialog "Sorry, there don't appear to be any appletalk servers on your user keychain" buttons {"OK"} default button 1 with icon stop giving up after 15
	else
		repeat with x from 1 to (length of theKeyList)
			set theKey to item x of theKeyList
			set theServername to name of theKey --grab the servername
			set thePassword to password of theKey --grab the password
			set theUserID to the account of theKey --grab the userid
			set theVolume to the volume of theKey --grab the Volume
			
			set pwd_chars to (characters of thePassword)
			set trimPassword to ""
			repeat with each_char in pwd_chars
				if each_char is not in keyboard_char then exit repeat
				set trimPassword to trimPassword & each_char
			end repeat
			
			try
				tell application "Finder"
					try
						mount volume "afp://" & theUserID & ":" & trimPassword & "@" & theServername & ".your.server.here/" & theVolume
					end try
				end tell
			end try
			
		end repeat
	end if
end tell

property keyboard_char : (characters of "§1234567890-=±!@£$%^&*()_+qwertyuiop[]QWERTYUIOP{}asdfghjkl;'ASDFGHJKL:|`zxcvbnm,./~ZXCVBNM<>?")