Photoshop CS2
Adobe Photoshop CS2 Scripting Guide
Scripting basics 26
Using Conditional Statements
Conditional statements give your scripts a way to evaluate something and then act according to the result.
For example, you may want your script to detect the blend mode of a layer or the name or date of a history
state.
Most conditional statements contain the word
if
, or the words
if
and
then
.
The following examples check whether any documents are open; if no documents are open, the scripts
display a dialog box that contains the message “No Photoshop CS2 documents are open!”. If one or more
documents are open, then no dialog is displayed.
AS
tell application "Adobe Photoshop CS2"
(*create a variable named docCount to contain the document count,
then use the count command to get the value*)
set docCount to count every document
if docCount = 0 then
display dialog "No Photoshop CS2 documents are open!"
end if
end tell
VBS
'create a variable named docCount for the document count, open Photoshop
Dim docCount As long
Dim appRef As New Photoshop CS2.Application
'use the count property of the Documents collection object to count the number of
open documents
docCount = appRef.Documents.Count
If docCount = 0 Then
Alert "No Photoshop CS2 documents are open!"
End If
JS
//create a variable named docCount,
//then get its value using
//the length property of the documents (collection) object*/
var docCount = documents.length
if (docCount == 0)
{
alert("No Photoshop CS2 documents are open!")
}
Loops
Loops are control structures that repeat a process until the script achieves a specific goal, status, or
condition.
Simple Loops
The simplest loops repeat a series of script operations a set number of times. Although you’ll find more
substantial uses for loops, the following scripts use a variable named
counter
to demonstrate how to
display a dialog box that contains the number 1, then display another dialog that contains the number 2,
and then display a third dialog that contains 3.