Applescript. If..then statement not working.

Discussion in 'Mac Programming' started by urello, Mar 4, 2014.

  1. urello macrumors newbie

    Joined:
    Feb 28, 2014
    #1
    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.
     
  2. Red Menace macrumors 6502

    Red Menace

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    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.
     
  3. urello thread starter macrumors newbie

    Joined:
    Feb 28, 2014
    #3
    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)
    
     
  4. Red Menace macrumors 6502

    Red Menace

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #4
    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?
     
  5. Supermacguy macrumors regular

    Joined:
    Jan 3, 2008
    #5
    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
     

Share This Page