Vectorials
Flash Perfection
3D Lessons
Tutorialkit
Markup Tutorials
Learn PHP
network
Photoshop  Home Photoshop Miscellaneous Photoshop Scripting Basics: Conditionals
rss

Photoshop Scripting Basics: Conditionals

Author: FotoFects More by this author
Browse Pages: << <  1  2  3  4  5 > >>


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 = ;
var rain = ;
var wind = ;
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 = ;
var rain = ;
var wind = ;

if (sun == true) {
alert("It's sunny");
}
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
  1. Type in the "if" followed by a ( and ) parenthesis.
  2. We now need to know if there's a sun in the sky. First, we need to tell the script to check if there's a sun in the sky by using a conditional statement. Type in "if ()".
  3. Inside the parenthesis, type in "sun == true". You'll need to use two = signs.
  4. Type in "{}". Those braces will hold the script that will be played if the "if" statement is true. If not, it'll simply skip those steps.
  5. Now we need to tell it what to do if the "if" statement is true. Type in "alert("It's sunny") inside the braces {}.

var sun = ;
var rain = ;
var wind = ;

if (sun == true) {
alert("It's sunny");
} else if (rain == true) {
alert("It's raining");
} else if (sun == true & & wind == true) {
alert("It's windy");
} else if (rain == true & & wind == true) {
alert("There's a storm outside")
} else {
alert("The weather's crazy")
}
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.

var sun = false;
var rain = true;
var wind = true;

if (sun == true & & wind == true) {
alert("It's windy");
} else if (rain == true & & wind == true) {
alert("There's a storm outside")
} else if (sun == true) {
alert("It's sunny");
} else if (rain == true) {
alert("It's raining");
} else {
alert("The weather's crazy")
}

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.

image 4 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.



print this page tell a friend subscribe to newsletter subscribe to rss
Rate this Material: Bad 1 2 3 4 5 Excellent
Browse Pages: << <  1  2  3  4  5 > >>

Add comments to "Photoshop Scripting Basics: Conditionals"