Conditionals
Now that you know how to create mathematical equations, you need to learn about Conditionals. Conditionals are "if" and "then" statements. The "if" statement simply tests to see if something is true or false. If it is true, the script inside the "then" statement will play. Here's a script that uses
var sun = ; |
1. We'll create a script that tells us about the weather. Begin by creating a few variables. We'll define the value of the variable at the end so don't type the value in yet. | |
| |
||
var sun = ; |
2. Before the script can tell us what
the weather is outside, it needs to know what the conditions are. This is when we use conditionals.
Lets start off with a simple one
|
|
| |
||
var sun = ; |
3. We now need to type in some more conditional statements. It's the same thing as the previous step but you need to type in the keyword "else" before the "if" statement. You can add more than one conditions inside the "if statement" by adding "& &". For the if statement, you may simply type in "else" instead of "if". This "else" statement will tell the script what to do if all the "if" and "else if" conditions above are not true. If you do not provide an "else" statement, the script will simply do nothing if all the "if" and "else if" conditions above are not true. | |
| |
||
|
|
4. Before we type in the variables, there are errors with this script. If you try entering true/false values for the variables and run it, it never seems to say "It's windy" or "There's a storm". This is because it checks to see if the first conditional on the list is true. If it isn't, then it'll skip to the next conditional. To fix this, we need to sort the conditionals by their complexity. Put the most complicated ones such as "There's a storm outside" on the top, simple ones like "It's sunny" on the bottom and leave the "else" statement below all the conditionals. Now when you run the script, it'll work fine. | |
| |
||
![]() |
5. Save your file as a *.jsx file on your desktop. You can name it whatever you want. Open Photoshop and run the file (File> Scripts> Browse). an alert pop-up should pop-up with the answer to 6+4. | |
FAQ:
Q: Why do you have to put two = signs for them to work? Why can't I use a single = sign
instead?
A: Using a single = or & sign will work, but won't work the way you want it to. When you type in "sun
= true", the script sets "sun" as "true" instead of checking to see if "sun" equals "true". successfully
it sucessfully set "sun" as "true", it plays the "then" statement.













More Photoshop: