AppleScript n00b needs troubleshooting help
November 2, 2007 6:28 AM   Subscribe

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 Tiny 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!
posted by electric_counterpoint to Computers & Internet (6 answers total)
 
I think the problem is with the equal sign for the conditional.

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


Response by poster: Oh, the HTML entity in the pastebin version is my fault (I should have corrected it to a "≠" by hand), but it's not what's screwing me up.
posted by electric_counterpoint at 6:41 AM on November 2, 2007


Best answer: randphrase needs to be a string. Add, after the 'end repeat', the line: set randphrase to randphrase as string
posted by beniamino at 6:42 AM on November 2, 2007


sorry, I meant "add the line"
posted by beniamino at 6:43 AM on November 2, 2007


Response by poster: Voila! I knew it was something small. Thanks, benjamino!
posted by electric_counterpoint at 6:46 AM on November 2, 2007


You could also just build randphrase as a string in the first place. Initialize it to "" rather than {}, then in the loop do:

set randphrase to randphrase & some item of "..."

Then it's a string the whole time.
posted by kindall at 8:16 AM on November 2, 2007


« Older Interpol in Berlin! One night only!   |   Breakdown of the global software industry? Newer »
This thread is closed to new comments.