I have probably a ridiculously easy question that I’m sure you can help me with. I’m writing some quick code regarding a few different directions a user can go based on their decisions. The program launches a networked application but the user is asked beforehand (in a text box with three buttons) whether they want to “Continue”, “Need More Help!” or “Cancel”. Where I’m having problems is on the help button - it opens the file (path) just fine but it continues to execute the remainder code in the script (WHICH I DONT WANT IT TO DO!). I simply need the AppleWorks file to open and that’s all.
Here’s what I have thus far:
tell application “Finder”
say “program loading” using “Victoria”
display dialog " TEXT INSTRUCTIONS."
buttons {“Continue…”, “I Need More Help!”, “Cancel”} default button 3
if the button returned of the result is "I Need More Help!" then open file "Macintosh HD:Users:teacher_mi:Desktop:HELP!.rtfd"
– I NEED HELP HERE
mount volume "afp://user:password@ipaddress:548/server directory"
– REST OF SCRIPT
end tell
=======
Now as you can probably tell it’s easy to see why the program continues after pressing help (there’s nothing to say that the script should end if this happens). I’m sure there’s an “else” command here i’m missing or maybe this code can be a bit tighter, but unfortunately i’m still very much new and learning the ropes headfirst.
if button returned of result is "I Need More Help!" then
tell application "System Events" to open ((path to desktop as Unicode text) & "HELP!.rtfd")
return
end if
Unfortunately my version of AppleScript does not support “end if” statements. Of course end tells and end trys are great, but no end if’s. I’m developing for a Mac-Intel (10.4.9) with AppleScript version 1.10.7 and Script Editor 2.1.1 (81).
PS - I can use a return function but ultimately outside the if/then statement it renders the Continue button as a cancel command.
this is rather impossible.
Check whether you have always the proper nesting of blocks
this doesn’t work:
try
if
end try
end if...
this works:
try
if
end if
end try
try this:
set b to button returned of (display dialog "" buttons {"Continue...", "I Need More Help!", "Cancel"} default button 3)
if b is "I Need More Help!" then
tell application "System Events" to open ((path to desktop as Unicode text) & "HELP!.rtfd")
return
else
mount volume "afp://user:password@ipaddress:548/server directory"
activate application "FFAST Math - Manager"
tell application "System Events" to tell process "FFAST Math - Manager"
delay 0.5
key code 53
delay 22
keystroke tab
keystroke "password"
keystroke tab
end tell
end if
I used both of your advice with a proper if/end if statement. I learned late last night that end if scripts work fine in my version of AppleScript (on a mac-intel); the only complication is that they have to be preceded by a true or try statement (or other variable prefix).
Things are smooth now. Thanks again
Model: iMac
AppleScript: 2.1.1 (81)
Browser: Safari 419.3
Operating System: Mac OS X (10.4)
I think you’re still a bit confused about if statements. You can have:
if (some logical test producing a true or false [boolean] result) then
do something for the positive test
else
do something for the negative test
end if
They can also take the form:
if (boolean test 1) then
do first thing if test 1 is true
else if (boolean test 2) then
do something if test 2 is true, but test 1 was false
else if (boolean test 3) then
do something if test three is true, but test 1 was false
else
do something if test 1, 2 and 3 were false
end if
They can also be one liners:
if (some true/false test) then do something right away – no end if here.
All the "do something"s in the examples above can include try blocks, tell blocks, etc. The only constraint is that the whole block (beginning to end) has to be inside the part of the if statement where it applies.
The boolean tests can also be combined and negated:
if A > B and C < D then…
if Name is “Jane” or Sex is “Female” then…
if word 1 of N does not contain “Dr” then set N to "Mr " & N