15-line AppleScript is giving this n00b nightmares. I think something's wrong with my "if ... then" syntax.
What I'm trying to do (embarrassingly enough) is write a "Pay attention, blockhead!" script that will disable my Macbook's AirPort wireless card and re-enable it only upon correctly typing in an arbitrarily-long string of characters. Good for maintaining focus in lectures, y'know?
Anyway, here's what's I've got after teaching myself AppleScript for an hour and snatching several snippets:
syntax-highlighted version, or T
iny Towne version below for future readers, after that link goes bad:
do shell script "scselect AirPort-Off" --This selects a network location for which I've disabled AirPort
set randphrase to {}
repeat (random number from 1 to 2) times --This loop generates the random phrase. It's set to give only a few characters now for testing purposes.
set end of randphrase to some item of "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()"
end repeat
set alert_message to ("Airport:" & return & return)
set alert_message to alert_message & randphrase
display dialog alert_message default answer "" buttons {"OK"} default button 1 with icon 1 --This shows the window with the challenge phrase and a text box
set userstypedphrase to text returned of the result
if randphrase = userstypedphrase then --This *should* reactivate wireless if the users' input is the same as the random phrase
do shell script "scselect 0"
else if randphrase ≠ userstypedphrase then --But, this one comes up no matter what.
display dialog "Wrong."
end if
So, basically, I'm trying to turn wireless off (this works), generate a random phrase (this works), bring up an input field (this works), and if the input matches the random phrase, turn wireless back on (this doesn't work). To troubleshoot, I've inserted a line, "
display dialog userstypedphrase & return & randphrase," which confirms that the two variables are the same. But nevertheless, it always evaluates them as different, and takes the "
else" fork.
What am I doing wrong here? I've scripted a few things before, so I thought I could handle this, but I just can't see how the two variables can't be made equal!
I've dabbled in Applescript but am not familiar with it enough to say for certain, but maybe there's a different way to compare strings. Try "If randphrase is userstypedphrase then", instead. See if that works.
posted by jeffxl at 6:40 AM on November 2, 2007