Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rmurphy07

macrumors newbie
Original poster
Apr 9, 2007
7
0
I've written this applescript to mount an AFP volume whenever I am on a certain wireless network. It works just fine, but I am trying to keep from having to hit enter every time it mounts the designated share. My login information is saved in the keychain, I just can't seem to get the Applescript to press "Connect" for me. I know it might sound lazy, but isn't that the point of Applescript? I used UI inspector to figure out the parameters of the "Connect" button.

Does anyone know how to fix this script so that I do not have to hit enter every time it mounts? Thanks.

set AirportName to do shell script ¬
"/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | sed '/ SSID: /!d; s/^.* SSID: //'"

tell application "Finder"
activate
if AirportName is "Home" then mount volume "afp://Volume/Share" as user name ¬
"user"
tell application "System Events"
tell process "AFP Client"
click button "connect" of window "Connect to Server"
end tell
end tell
end tell
 

rmurphy07

macrumors newbie
Original poster
Apr 9, 2007
7
0
nope

I tried the thing mentioned in the macosxhints article, but it still doesn't work. I looked at the mount_afp command, but I don't want to put my username and password in an unencrypted document.

I have also tried to replace the

click button "connect" of window "Connect to Server"

with

keystroke enter

and it still didn't work.

Thanks for your help, though. I'm still looking for a solution.
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
I tried the thing mentioned in the macosxhints article, but it still doesn't work. I looked at the mount_afp command, but I don't want to put my username and password in an unencrypted document.

<snip>

Thanks for your help, though. I'm still looking for a solution.
Did some poking around, and found this, which deals specifically with scripting the Keychain. I'm almost certain that you could use that, combined with the mount_afp command (using the do shell script command in AppleScript to run it), to create a script that retrieves the username and password from the Keychain, then passes those as parameters to the mount_afp command. This avoids storing the username and password insecurely, as you wish.
 

xUKHCx

Administrator emeritus
Jan 15, 2006
12,583
9
The Kop
I cobbled this together from a script my housemate uses. Don't know where he got it from because i know he can't script.

It uses the keychain to get the username and password out. Change XX to the number of the item in the keychain. Change YY to the ip of the share you want to mount, can be the bonjour name i.e "richs-imac.local" Set ZZ to the name of the share. Works perfectly on my setup.

set serverIP to "YY" --enter the ip of the server here
set ShareName to "ZZ" -- enter the share name here ***Make sure it is in "CAPS"

try
tell application "Keychain Scripting"

set myChain to keychain "login.keychain"
set username to the account of key XX of myChain
set pass to the password of key XX of myChain
end tell
end try

tell application "Finder"
try
set server_Off to "Server Not On"
set server_On to "Server Mounted"
set server_AlreadyOn to "Server Already On"

(*try
set ping_result to (do shell script "ping -c 1 -q " & serverIP)

on error
display dialog server_Off buttons "OK" default button 1 with icon 1
error number -128 --user cancelled

end try -- Commented out because it kept throwing up an error for no reason*)

mount volume "afp://" & username & ":" & pass & "@" & serverIP & "/" & ShareName
on error number -55
display dialog server_AlreadyOn buttons "OK" default button 1 with icon 1
error number -128 --user cancelled
end try
end tell
 

rmurphy07

macrumors newbie
Original poster
Apr 9, 2007
7
0
got it!

Hi friends,
Someone suggested using Keychain scripting to get this to work without me having to put my username and password into an unencrypted document. Here is the final script. It works beautifully. Thanks for all of your help. I wanted to post the script for everyone's benefit and elation.

set AirportName to do shell script ¬
"/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | sed '/ SSID: /!d; s/^.* SSID: //'"

tell application "Finder"
activate
if AirportName is "VLAN" then tell application "Keychain Scripting"
set serverKey to first key of current keychain whose name is "Handel"
set acct to account of serverKey
set pswd to password of serverKey
end tell

tell application "Finder"
try
mount volume "afp://Handel/DataCenter" as user name acct with password pswd
end try
end tell
end tell
 

xUKHCx

Administrator emeritus
Jan 15, 2006
12,583
9
The Kop
Hi friends,
Someone suggested using Keychain scripting to get this to work without me having to put my username and password into an unencrypted document. Here is the final script. It works beautifully. Thanks for all of your help. I wanted to post the script for everyone's benefit and elation.

Just being paranoid but i would strip your post with "sensitive" data such as the the various names of things. Just a thought.
 

rmurphy07

macrumors newbie
Original poster
Apr 9, 2007
7
0
Thanks for your concern. I am not worried about those particular details.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.