PDA

View Full Version : Applescript: interacting with dialog boxes




rmurphy07
Apr 9, 2007, 11:34 AM
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



kainjow
Apr 9, 2007, 01:45 PM
Check out this (http://www.macosxhints.com/article.php?story=20050620224703127&query=mount%2Bafp) article.

Also check out the mount_afp (http://developer.apple.com/documentation/Darwin/Reference/Manpages/man8/mount_afp.8.html) command.

rmurphy07
Apr 10, 2007, 11:57 AM
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
Apr 10, 2007, 12:35 PM
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 (http://docs.info.apple.com/article.html?artnum=301858), 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
Apr 10, 2007, 01:06 PM
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
Apr 10, 2007, 02:49 PM
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
Apr 10, 2007, 03:11 PM
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
Apr 10, 2007, 04:05 PM
Thanks for your concern. I am not worried about those particular details.