Detailed instructions for use are in the User's Guide.
[. . . ] The software described in this document is furnished under license and may only be used or copied in accordance with the terms of such license. This publication and the information herein is furnished AS IS, is subject to change without notice, and should not be construed as a commitment by Adobe Systems Incorporated. Adobe Systems Incorporated assumes no responsibility or liability for any errors or inaccuracies, makes no warranty of any kind (express, implied, or statutory) with respect to this publication, and expressly disclaims any and all warranties of merchantability, fitness for particular purposes, and noninfringement of third party rights. Any references to company names in sample templates are for demonstration purposes only and are not intended to refer to any actual organization. [. . . ] and two buttons: Yes and No.
set flag to DoConfirm ("Are you sure?") display dialog flag as string 'create a handler named DoConfirm
Photoshop CS2 Adobe Photoshop CS2 Scripting Guide Scripting basics 30
on DoConfirm(prompt) set button to button returned of (display dialog prompt ¬ buttons {"Yes", "No"} default button 1) return button = "Yes" end DoConfirm
VBS
In VBScript, subroutines begin with the keyword Sub and do not return a value. If you would like your subroutine to return a value, you must make it a function. Functions begin with the keyword Function.
Subroutine The following subroutine, which is named HelloWorld(), simply displays a message box with the message Hello World.
Sub HelloWorld() Alert "Hello World" End Sub
To call the subroutine, you include it in a script. The following example displays a Hello World message when a user clicks CommandButton1.
Private Sub CommandButton1_Click() HelloWorld End Sub
Function The following script presents a form with one command button. When a user clicks the button, a dialog appears with the message Are you sure?When the user clicks a button, another dialog appears that displays the Boolean value of the clicked button: Yes = True; No = False.
'create a subroutine that calls the function DoConfirm 'and assigns it to the variable named Result Private Sub CommandButton1_Click() Result = DoConfirm("Are you sure?") Alert Result End Sub 'define the function Function DoConfirm(prompt) buttonPressed = Alert (prompt, vbYesNo) DoConfirm = (buttonPressed = vbYes) End Function
JS
In JavaScript, all subroutines are functions. The following sample script is a JavaScript version of the VBScript sample function in the previous example.
/*create a variable and assign its value as the return value of the function named DoConfirm*/ var theResult = DoConfirm( "Are you sure?" ) //display an alert box with the assigned value as its message alert(theResult) //define DoConfirm function DoConfirm(message) { var result = confirm(message) return result
Photoshop CS2 Adobe Photoshop CS2 Scripting Guide Scripting basics 31
}
Executing JavaScripts from AS or VBS
You can take advantage of JavaScript's platform-independence by running scripts from AppleScript or VBScript. You can execute either a single JavaScript statement or a complete JavaScript file.
AS
To run a JavaScript from AppleScript, you use the do javascript command. The following sample executes a single JavaScript command, which displays an alert box with the text alert text.
do javascript "alert('alert text')"
To pass a JavaScript file, you can create a reference to the file using as alias or to a reference to
file as shown in the following examples
set scriptFile to "applications: scripts: myscript" as alias do javascript scriptFile set scriptFile to a reference to file "applications: scripts: myscript" do javascript scriptFile
Note: Refer to an AppleScript language guide or text book for information on referencing a file using either as alias or to a reference to file.
VBS
In VBScript, use the DoJavaScript method to execute a single JavaScript command.
objApp. DoJavaScript ("alert('alert text')")
To open a JavaScript file, use the DoJavaScriptFile method. The following sample opens a file on the D:\\ drive.
Dim appRef As Photoshop. Application Set appRef = CreateObject("Photoshop. Application") appRef. DoJavaScriptFile ("D:\\Scripts\\MosaicTiles. jsx")
Passing AS or VBS Arguments to JavaScript
You can also pass arguments to JavaScript from either AppleScript or VBScript using the with arguments/(Arguments) parameter of the do javascript/DoJavaScript or DoJavaScriptFile command or methods. The following examples execute the following JavaScript, which is stored in a file named JSFile. jsx in your Applications\Scripts folder:
alert( "You passed " + arguments. length + " arguments" ) for ( i = 0; i < arguments. length; ++i ) { alert( arguments[i]. toString() ) }
AS
tell application "Adobe Photoshop CS2" make new document do javascript (alias a path to the JavaScript shown above) ¬ with arguments {1, "test text", (fileApplications:Scripts:JSFile. jsx), ¬ current document}
Photoshop CS2 Adobe Photoshop CS2 Scripting Guide Scripting basics 32
end tell
VBS
Dim appRef As Photoshop. Application Set appRef = CreateObject("Photoshop. Application") appRef. DoJavaScriptFile "C:\\Applications\Scripts\JSFile. jsx", _ Array(1, "test text", appRef. ActiveDocument)
When running JavaScript from AppleScript or VBScript you can also control the debugging state. This example stores a reference to the document named MyDocument in a variable named docRef. If a document named MyDocument does not exist in the current document, the script displays a message.
AS
--Store a reference to the document with the name "My Document" --If "My Document" does not exist, display an error message tell application "Adobe Photoshop CS2" try set docRef to document "My Document" display dialog "Found 'My Document' " on error display dialog "Couldn't locate document 'My Document'" end try end tell
Photoshop CS2 Adobe Photoshop CS2 Scripting Guide Scripting basics 34
VBS
Private Sub Command1_Click() ' Store a reference to the document with the name "My Document" ' If the document does not exist, display an error message. Dim appRef As New Photoshop. Application Dim docRef As Photoshop. Document Dim errorMessage As String Dim docName As String docName = "My Document" Set docRef = appRef. ActiveDocument On Error GoTo DisplayError Set docRef = appRef. Documents(docName) Alert "Document Found!" Exit Sub DisplayError: errorMessage = "Couldn't locate document " & "'" & docName & "'" Alert errorMessage End Sub
JS
try { for (i = 0; i < app. documents. length; ++i) { var myName = app. documents[i]. name; alert(myName) } } catch(someError) { alert( "JavaScript error occurred. Message = " + someError. description) }
Bibliography
AS
For further information and instruction in using the AppleScript scripting language, see these documents and resources:
"AppleScript for the Internet: Visual QuickStart Guide, " 1st ed. , Ethan Wilde, Peachpit Press, 1998. "AppleScript Language Guide: English Dialect, " 1st ed. , Apple Computer, Inc. , Addison-Wesley Publishing Co. , 1993. "Danny Goodman's AppleScript Handbook, " 2nd ed. , Danny Goodman, iUniverse, 1998. AppleScript website: www. apple. com/applescript
Photoshop CS2 Adobe Photoshop CS2 Scripting Guide Scripting basics 35
VBS
For further information and instruction in using VBScript and the VBSA scripting language, see these documents and resources:
"Learn to Program with VBScript 6, " 1st ed. , John Smiley, Active Path, 1998. "Microsoft VBScript 6. 0 Professional, " 1st ed. , Michael Halvorson, Microsoft Press, 1998. "VBS & VBSA in a Nutshell, " 1st ed. , Paul Lomax, O'Reilly, 1998. Microsoft Developers Network (MSDN) scripting website: msdn. microsoft. com/scripting
JS
For further information and instruction in using the JavaScript scripting language, see these documents and resources:
"JavaScript: The Definitive Guide, " David Flanagan, O'Reily Media Inc, 2002. "Adobe Scripting, " Chandler McWilliams, Wiley Publishing, Inc. , 2003. [. . . ] 0 indicates the top row in the document.
Upper right corner of the selection: theDocWidthInPixels / 2, 0
theDocWidthInPixels / 2 indicates the column in the middle of the document; that is, the
column whose coordinate is the total number of columns in the document divided by 2. Note: The value of theDocWidthInPixels is the total number of pixels that defines the document's horizontal dimension. Columns are arranged horizontally.
0 indicates the top row in the document.
Lower right corner: theDocWidthInPixels / 2, theDocHeightInPixels
theDocWidthInPixels / 2 indicates the middle of the document. theDocHeightInPixels indicates the bottom row in the document; that is row whose coordinate is the total number of rows in the document.
Note: The value of theDocHeightInPixels is the total number of pixels that determine the vertical dimension of the document. [. . . ]