Mac Applescript. If..then statement not working.

urello

macrumors newbie
Original poster
Feb 28, 2014
7
0
Hello,
I have the following simple code, but it doesn't work for me
Code:
#!/usr/bin/osascript
tell application "SystemUIServer"
set username to text returned of (display dialog "Enter your name" with icon caution default answer ""  buttons{"Continue"})
set macname to text returned of (display dialog "Enter name of your Mac" with icon caution default answer ""  buttons{"Continue"})
end tell
if (length of username is more than 1) and (length of macname is more than 1) then display alert "Alert" message "Too short"
end if
The error
Code:
./new.command:351:355: script error: Expected "," but found identifier. (-2741)
The problem in if statement but i dunno what is exactly wrong.
 

Red Menace

macrumors 6502
May 29, 2011
390
53
Littleton, Colorado, USA
There is no operator "more than" - it should be "greater than". Also, a simple (single line) if statement doesn't use the end if, so you need to either stick with the single line statement and not use the end if, or place the conditional statements between the if and end if. See the AppleScript Language Guide Operators and Control Statements sections.
 

urello

macrumors newbie
Original poster
Feb 28, 2014
7
0
I relaced 'more' with 'greater' and removed 'end if'. Now it exits on timeout, no input dialog appears.

Code:
./new.command:89:178: execution error: SystemUIServer got an error: AppleEvent timed out. (-1712)
 

Red Menace

macrumors 6502
May 29, 2011
390
53
Littleton, Colorado, USA
I've tested your code in BBEdit, as a Terminal .command, and as a shell script from AppleScript, and it works OK for me. Is there more to it? Is there a particular reason you are using osascript?
 

Supermacguy

macrumors 6502
Jan 3, 2008
322
496
I believe you're not seeing a dialog because the terminal can't display a dialog. What context is the "display alert" running in? If I use Script Debugger or Applescript Editor, the editor itself shows the final dialog, because no target is explicitly stated. I think if you were to use this it might work better.

tell application "SystemUIServer"
set username to text returned of (display dialog "Enter your name" with icon caution default answer "" buttons {"Continue"})
set macname to text returned of (display dialog "Enter name of your Mac" with icon caution default answer "" buttons {"Continue"})
if (length of username is greater than 1) and (length of macname is greater than 1) then display alert "Alert" message "Too short"
end tell