If then statement using applescript in mac terminal?

5,136

In bash (command line):

if [ "$foo" = "1" ] ; then
  foo=18
fi

in AppleScript:

if foo equals 1 then
    set foo to 18
end if

Responding to the comments:

set foo to 5
tell application "System Events"
    tell application "Firefox" to activate
    keystroke (foo as text) using command down
end tell

You can probably append this to my answer to your previous question as-is, minus the set foo line.

Share:
5,136
User19
Author by

User19

Updated on September 18, 2022

Comments

  • User19
    User19 over 1 year

    What would be the code for:

    "If $foo is one, then $foo should now equal 18" ?

    Thanks

    • HikeMike
      HikeMike about 13 years
      Didn't you want all values converted to their key codes? Or just 1?
    • HikeMike
      HikeMike about 13 years
      To others, FYI: This is a question following this other topic. @JShoe: I think you need to explain more fully what you want to do here. AppleScript and Terminal/bash scripting are two completely separate things and your questions don't make a whole lot of sense to me, to be honest.
    • User19
      User19 about 13 years
      Is there a way to convert them to their key codes? I'm only working with 1-10 in the program I'm making.
    • HikeMike
      HikeMike about 13 years
      10 is not a key. Do you mean 0?
    • User19
      User19 about 13 years
      Well yeah, but the input of my program is 1-10. It's going to open tabs in firefox. Tab 10 = command + 0.
    • HikeMike
      HikeMike about 13 years
      Then I suggest keystroke foo using command down. That's why I keep asking what it is you want to do.
    • User19
      User19 about 13 years
      But keystroke foo when foo = 1 will give me s. First I have to convert it to the correct key code, right?
  • HikeMike
    HikeMike about 13 years
    @JShoe It marks the end of the block of code only executed if the condition is true
  • User19
    User19 about 13 years
    Do I need "foo as text"? Wouldnt $foo work?
  • HikeMike
    HikeMike about 13 years
    $foo is bash syntax. This example is for when you add it directly to the end of the previous script. Or do you want to pipe the output of one AppleScript into another using bash, when you can just append one to the other? Replace any return with set foo to in the dialog script and it should work.