Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Connect to servers with Applescript using Keychain Network
I've been using Quicksilver for a while, and it's great, but unfortunately, it doesn't successfully resolve aliases to servers. I found various AppleScripts online which connect to a server, but none appear to use the keychain, forcing me to log in every time. I came up with a solution which uses an AppleScript to call an alias (located in my Favorites folder) to open a share using the Keychain password. I imagine that other people who have struggled with getting AppleScript to open shares might find this useful also.

Here's the script:
set thePath to ((get path to startup disk) & "Users:" & 
 (do shell script "echo $USER") & 
 ":Library:Favorites:ALIAS to Server") as string

tell application "Finder"
  open alias thePath
  activate
end tell
Note that this script also shows a simple way to open files in a given user's home directory. Everything I had found online (including some hints here) pointed out that there's not easy way to do this -- but by using a shell script to get the $USER setting, it's relatively simple from there. To use the generic version, just replace ":Library:Favorites:ALIAS to Server" in the first line with the path to the folder you need to reach.

[robg adds: I haven't tested this one...]
    •    
  • Currently 2.25 / 5
  You rated: 2 / 5 (4 votes cast)
 
[25,733 views]  

Connect to servers with Applescript using Keychain | 15 comments | Create New Account
Click here to return to the 'Connect to servers with Applescript using Keychain' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Connect to servers with Applescript using Keychain
Authored by: Snaro on Feb 02, '05 10:11:15AM
set thePath to (path to favorites folder from user domain as string) & "Alias to server"
will do nicely and is less error prone, e.g. if someone has his home directory on another disk/partition

[ Reply to This | # ]
Connect to servers with Applescript using Keychain
Authored by: Eravau on Feb 02, '05 11:49:26AM
You could actually shorten this line three more words by eliminating the from user domain phrase. AppleScript appears to default to the user domain if no domain is specified. While we're at it, let's just put it all on one line and eliminate a few other extraneous commands. By the way, thanks for reminding me that the Favorites folder was a special AppleScript variable. I was originally heading down the path to home & "Library:Favorites..." trail.

Final Code (line split for the screen width impaired):
tell application "Finder" to open alias 
((path to favorites folder as string) & "Alias to Server")


[ Reply to This | # ]
Nice!
Authored by: jason mark on Feb 02, '05 07:56:01PM

Thanks for this!



[ Reply to This | # ]
Connect to servers with Applescript using Keychain
Authored by: bynkii on Feb 02, '05 11:13:22AM

You can also do the same thing with the keychain directly via Keychain Scripting, and the "mount volume" command. This allows you not need an alias. The big caveat is that in Panther, if you have SSL certificates in your keychain, Keychain Scripting will think that keychain is empty, and yes, there's a bug filed on it.

john



[ Reply to This | # ]
Connect to servers with Applescript using Keychain
Authored by: atlynch on Feb 02, '05 01:57:52PM

Can you post an example of how to do this please?

Thanks,
Drew



[ Reply to This | # ]
Connect to servers with Applescript using Keychain
Authored by: thecloud on Feb 04, '05 02:12:38AM
Let's say you have a password for the AppleShare server "Foobar" saved in your keychain. You can look it up like this:
tell application "Keychain Scripting" to get password of first AppleShare key of current keychain whose server is "Foobar"

Maybe you have more than one account on "Foobar"? Maybe more than one volume or machine? No problem, just narrow your search until you find the one you want:
tell application "Keychain Scripting" to get password of first AppleShare key of current keychain whose address is "192.168.1.69" and server is "Foobar" and volume is "Mac HD" and account is "My Name"

and so on...

[ Reply to This | # ]

Sigh.....
Authored by: alcor on Feb 02, '05 11:21:44AM

Fine, I'll fix it. GEEEZ.



[ Reply to This | # ]
Re: Sigh.....
Authored by: Krioni on Feb 02, '05 02:08:13PM

Alcor, you (and Quicksilver) rock!

For those who don't know, Alcor is the developer of Quicksilver. Pretty responsive to suggestions, no? Even if they are posted on someone else's site. :-)



[ Reply to This | # ]
Grrrr Quicksilver
Authored by: BulbVivid on Feb 02, '05 02:29:49PM

Damn you for making me hate machines without Quicksilver installed. They just don't cut it . . . I installed Quicksilver on all the machines that I support here at work, even if the users don't mess with it, just so that when I sit down, I can hit that command-space and feel right at home. . . .

Great stuff.



[ Reply to This | # ]
Thank you!
Authored by: jason mark on Feb 02, '05 08:03:55PM

Really! Thanks man. If i wasn't married, or my wife didn't object I'd propose to marry you right now!



[ Reply to This | # ]
Thank you!
Authored by: alcor on Feb 03, '05 11:02:55AM

Not a problem. It has been bothering me for a while too. Next time just don't go airing my dirty laundry for all to see...... ;)



[ Reply to This | # ]
Connect to servers with Applescript using Keychain
Authored by: hamarkus on Feb 02, '05 03:00:22PM
This Applescript works fine for me, upon first connect it is asking for the password and whether to save it in Keychain, after that it is just a doubleclick (or if you use some utility like DropDrawers just a hotkey). The important thing is to add the username to the URL (maybe this is only necessary if it your username on the Windows server is different from your OS X username).

tell application "Finder"
mount volume "smb://domain;username@servername/share"
end tell


[ Reply to This | # ]
another way to applescript server shortcuts....
Authored by: eoberle on Feb 05, '05 06:47:53PM
Maybe someone will find this useful....Here's a little applescript I wrote that allows me to mount servers from a favorite list built into the script. It assumes that the passwords themselves are in the keychain, but I don't understand why you'd do it any other way. The nice thing about this is that once you fill in your servers in the server list, it will quickly mount as many volumes as you wish. Right now it default to mounting them all, but you can tweak that as you wish by changing the default items variable. If someone wanted to, it wouldn't be hard to have these lists built dynamically from the aliases in your 'favorites' folder. cheers, Eric


property server_list : {
	{name:"chris", address:"afp://xxx.local", volume:"chris"}, 
	{name:"itunes", address:"afp://xxx.local", volume:"itunes-g1"}, 
	{name:"itunes-backup", address:"afp://xxx.local", volume:"xxx"}, 
	{name:"fred", address:"afp://social-thought-server.local", volume:"fred"}, 
	{name:"backup", address:"afp://xxx.local", volume:"data-n-backup-d1"}, 
		}

set display_list to {}
repeat with the_item in server_list
	set display_list to display_list & name of the_item
end repeat


set choice to choose from list display_list default items display_list 
with prompt "Please select servers to mount" with multiple selections allowed

if choice is not {} then
	repeat with the_name in choice
		repeat with the_server in server_list
			
			if (name of the_server) is equal to the_name as string then
				try
					tell application "Finder"
						set x to mount volume (address of the_server & "/" & volume of the_server)
						log address of the_server
					end tell
				end try
			end if
		end repeat
		
	end repeat
end if




[ Reply to This | # ]
Connect to servers with Applescript using Keychain
Authored by: Sesquipedalian on May 09, '06 05:42:40PM
Here's a nice simple version that will exit gracefully if the server can't be found.

tell application "Keychain Scripting"
	set serverKey to first key of current keychain whose name is "servername" --change servername to the name of the computer you are connecting to as it is listed in Keychain Access
	set acct to account of serverKey
	set pswd to password of serverKey
end tell

tell application "Finder"
	try
		mount volume "protocol://server/volume" as user name acct with password pswd --change the protocol and path to the appropriate data.  This can be found by mounting the server volume manually, and then doing a "Get Info" on it's desktop icon.
	end try
end tell


[ Reply to This | # ]
Connect to servers with Applescript using Keychain
Authored by: regulus on May 09, '06 05:58:02PM

A short script which first checks if a disk is already mounted, and then if it's not mounted will mount it.

tell application "Finder"
if not (exists disk "ShareVolume") then open location "afp://username@ipaddress/ShareVolume"
end tell

I inserted the ShareVolume mount check to eliminate any problems if a disk is already mounted. I use the script to open several volumes at once (with several if not... statements) and many times I already have one or more ShareVolume's mounted when I run the script.



[ Reply to This | # ]