I’m writing a script for server login - everything works great except for when the user chooses “Cancel” from the share volume select screen (after entering the correct username/password). Since the cancel button registers as an “error number -128” it falls into the on error block and gives a dialog box reporting incorrect credentials (which incidentally is written for just that).
My question:Is there a way AppleScript can register the “Cancel” button without tying it directly into an error block located thereafter in the script? (SEE BELOW)
-- set text returned from currentUser & currentPass along with beginning of script
repeat
tell application "Finder"
try
mount volume "afp://" & currentUser & ":" & currentPass & "@172.x.x.x"
on error
beep
tell application "Play Sound 2" to stop
tell application "Play Sound 2" to quit
set frontmost to true
set E to (display dialog "YOUR CREDENTIALS ARE INCORRECT!
It's also possible you're not connected
to the internet or the school's LAN.
Make sure your wireless icon is on
and you're attached to the network." buttons {"Exit", "Repeat"} default button 2 with title " ~~ERROR~~" with icon 0 giving up after 20)
if button returned of E is "Exit" then
tell me to quit
error number -128
else
if startup disk exists then
tell application "Play Sound 2" to stop
tell application "Play Sound 2" to quit
set frontmost to false
exit repeat
tell me to quit
error number -128
end if
end if
end try
tell application "Play Sound 2" to stop
tell application "Play Sound 2" to quit
tell me to quit
error number -128
end tell
end repeat
end if
end if
end repeat
.
try
mount volume "afp://" & currentUser & ":" & currentPass & "@172.x.x.x"
on error number n
if n is not -128 then
beep
.
.
end if
end try
.
BTW: this
exit repeat
tell me to quit
error number -128
is double useless, the quit line will never be executed after an exit repeat
and the error line will never ever be executed after a quit command.
To abort a script, just use return, if the code is on top level (i.e. not in a handler)
or error number -128, which has to be properly handled in a try block!
Thank you Stefan for the great advice :D. I knew a lot of that code was sloppy and appreciate the input.
Unfortunately though when the user enters the incorrect name/password it too flags the error as a -128. Do you know of a way AS can differentiate between false text input and a user choosing the “Cancel” button?
Not true. The ‘quit’ isn’t completed until the application’s finished running the script. The error line ensures that the execution stops at that point.
Another way is to evaluate the input. Only if the input passes a test, should the script try to fulfill its purpose.
property maxEfforts : 2 --permissible attempts before fail
repeat with counterVar from maxEfforts to 1 by -1
display dialog "What's the password?" default answer ""
--ID test:
if (text returned of the result) = "Chaka" then
--attempt mount:
try
mount volume "afp://" & currentUser & ":" & currentPass & "@172.x.x.x"
--if the mount fails:
on error
beep 2
display dialog "Disk mount failed. Check your network connections and stuff." buttons "Cancel"
end try
exit repeat
else
--consequences:
if counterVar - 1 ≠0 then display dialog ¬
"Wrong answer. You may try " & (counterVar - 1) & " more time(s)." buttons ¬
{"Cancel", "OK"} default button 2
if counterVar = 1 then display dialog ¬
"Your " & maxEfforts & " attempt(s) have failed, due to bad credentials." with title ¬
" ~~ERROR~~" with icon 0 giving up after 20
end if
end repeat
You have to regard the -128 error as coming from ‘mount volume’ itself rather than from any particular stage in the process it performs. It apparently means: “There’s nothing wrong with the network connection but, for some reason, no volume was mounted.” There’s a different error if there is a problem with the connection and a successful mount returns a file reference to the mounted volume, but otherwise the returned detail is a bit disappointing. I don’t know if this is an omission or a security measure.
If you don’t like Marc’s suggestion of having the password in the script, and you happen to know what volume names the user’s likely to be offered, you could less controversially present those before the ‘mount volume’ line:
set volumeChoice to (choose from list {"list", "of", "volume", "names"})
if (volumeChoice is false) then -- The user clicked the "Cancel" button.
error number -128
end if
set volumeName to item 1 of volumeChoice
try
mount volume "afp://" & currentUser & ":" & currentPass & "@172.x.x.x" & "/" & volumeName
on error number -128
-- The mount did not take place.
end try
Thank you both Marc Anthony and Nigel for posting.
Both of them present great ideas yet do not work within the realm of my clients. Having a list is great Nigel but I have a load of share points which would take up more then the entire screen. On the other hand I have a multitude of users (over 300) and each user has a different password so Marc’s suggestion isn’t going to fly.
I’m incredibly stubborn though and refuse to give up. I’ve read many postings on this site and also used UIElement Inspector to find the contrasts between wrong credentials and simply canceling from the share select box. Here’s the results that differed:
The AXRole
The AXRoleDescription
The AXValue (when mounted it has a AXValue which is the dialog text, while the invalid user/pass error does not)
The AXPosition
The AXSize
Along with this information, I also found a post which I thought would help me since neither error window has a title:
Some time awhile back I decided I didn’t like the mount volume command and started using open location instead. I can’t remember why though but maybe you should try this. It takes care of the cancel button and password errors for you. You don’t even have to present a list of disks either as that’s all taken care of for you.
set ipaddress to "192.168.1.203"
open location "afp://" & ipaddress