Photoshop  Home Photoshop Miscellaneous Photoshop Scripting Basics: Operators
rss

Photoshop Scripting Basics: Operators

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


Operators

Operators are the same thing as arithmetic operators (+, -, *, /) and can be use to create equations in a script. Lets create a simple script using variables and operators.

var myNum = 6;
var myNum2 = 4;
1. We'll need three parts to create this equation:
  1. Two number variables to define the numbers that will be added
  2. A variable with the equation
  3. An alert function to test the script

Begin by defining the two variables.


var myNum = 6;
var myNum2 = 4;
var answer
2. Now we need to type in an equation. For starters, lets create one that adds the two variables.

Begin by creating a variable. We'll call this variable "answer".


var myNum = 6;
var myNum2 = 4;
var answer = myNum + myNum2;
3. We now need to define what the sum variable is. Simply type in the first variable (myNum) followed by the operator (+) and finally the second variable (myNum2).

var myNum = 6;
var myNum2 = 4;
var answer = myNum + myNum2;
alert(answer);
4. Lets test the variable by creating an alert pop-up with the variable. To do this, type in alert(), add the answer variable inside the parenthesis and type a ; to instruction the instruciton.

image 3 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 popup with the answer to 6+4.

FAQ:
Q: What happens when you try to add strings instead of numbers?
A: JavaScript will try to convert the string to a number. For example, if your string is "5", it'll recognize that it's the number 5. However, if your string is "myString", it'll return an error with the NaN value which stands for "Not a Number".

Tip:
When only adding or subtracting one (ex. myVar + 1), you can simply type in "myVar ++" to increase "myVar" by 1 or "myVar decrease to decrase "myVar" by 1. This works the same as "myVar + 1" or "myVar - 1". Using increment or decrement will make your code cleaner and easier to read.



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: Operators"