Adobe 23101764 - Photoshop CS - PC Manual
Adobe 23101764 - Photoshop CS - PC Manual

Adobe 23101764 - Photoshop CS - PC Manual

Scripting guide
Hide thumbs Also See for 23101764 - Photoshop CS - PC:

Advertisement

Quick Links

Photoshop CS
Scripting Guide
ADOBE SYSTEMS INCORPORATED
Corporate Headquarters
345 Park Avenue
San Jose, CA 95110-2704
(408) 536-6000
http://partners.adobe.com
October 2003

Advertisement

Table of Contents
loading

Summary of Contents for Adobe 23101764 - Photoshop CS - PC

  • Page 1 Photoshop CS Scripting Guide ADOBE SYSTEMS INCORPORATED Corporate Headquarters 345 Park Avenue San Jose, CA 95110-2704 (408) 536-6000 http://partners.adobe.com October 2003...
  • Page 2 Adobe, Photoshop, and PostScript are either registered trademarks or trademarks of Adobe Systems Incorporated in the United States and/or other countries. Apple, Macintosh, and Mac are trademarks of Apple Computer, Inc. registered in the United States and other countries.
  • Page 3: Table Of Contents

    Table of contents Chapter 1 Introduction ......1 1.1 About this manual ........1 1.2 What is scripting? .
  • Page 4 Table of contents 3.5 Object references ........55 3.6 Working with units .
  • Page 5: Chapter 1 Introduction

    Introduction 1.1 About this manual ® ® This manual provides an introduction to scripting Adobe Photoshop CS on Mac OS and ® Windows . Chapter one covers the basic conventions used in this manual and provides an overview of requirements for scripting Photoshop.
  • Page 6: What Is Scripting

    Introduction What is scripting? In this case, display dialogs refers to the AppleScript property, DisplayDialogs refers to the Visual Basic property and displayDialogs refers to the JavaScript property, as illustrated below. AppleScript The display dialogs (DisplayDialogs/displayDialogs) property is part of the Application object.
  • Page 7: Why Use Scripting

    Introduction Why use scripting? 1.3 Why use scripting? Graphic design is a field characterized by creativity, but aspects of the actual work of illustration and image manipulation are anything but creative. Scripting provides a tool to help save time spent on repetitive production tasks such as resizing or reformatting documents. Start with short, simple scripts such as those described in this manual and move on to more involved scripts as you become more proficient.
  • Page 8: System Requirements

    Introduction System requirements 1.5 System requirements The language you use to write scripts depends on your operating system: AppleScript for Mac; Visual Basic for Windows; or JavaScript, a cross-platform scripting language that can run on either Windows or Mac. While the scripting systems differ, the ways that they work with Photoshop are very similar.
  • Page 9: Javascript

    Introduction JavaScript 1.6 JavaScript In addition to writing AppleScripts and Visual Basic scripts, you can also write cross-platform JavaScripts (using the text editor or your choice) on either the Mac or Windows platform. Photoshop provides a built-in, platform-independent framework for executing JavaScripts. The easiest way to run your JavaScripts is to use the “File>Scripts”...
  • Page 10: Legacy Com Scripting

    Introduction Legacy COM scripting 1.8 Legacy COM scripting Photoshop CS supports legacy COM scripting as long as you modify the way that you refer to the Photoshop application object in your scripts. For example, instead of saying: Set appRef = CreateObject("Photoshop.Application") you must change the above code to read: Set appRef = CreateObject("Photoshop.Application.8.1") No other change is necessary for legacy COM scripts to run under Photoshop CS.
  • Page 11: What's Next

    Introduction What’s Next • PDF Presentation Photoshop Elements introduced the ability to save multiple images in a single PDF document for presentation as a slide show. Photoshop CS has this feature -- called PDF Presentation -- which you can access through the scripting interface. •...
  • Page 12: Chapter 2 Scripting Basics

    Scripting basics 2.1 Documents as objects If you use Photoshop, then you create documents, layers, channels and design elements and probably think of a Photoshop document as a series of layers and channels — or objects. Automating Photoshop with scripting requires the same object-oriented type of thinking. The heart of a scriptable application is the object model.
  • Page 13 Scripting basics Object model concepts 2.2.2 Object inheritance Object classes may also “inherit,” or share, the properties of a parent, or superclass. When a class inherits properties, we call that class a child, or subclass of the class from which it inherits properties.
  • Page 14: Object Model

    Scripting basics Object Model JavaScript app.activeDocument.layerSets[0].layers[0]; When you identify an object in this fashion, you’re creating an object reference. While AppleScript, Visual Basic and JavaScript use different syntax for object references, each gives the script a way of finding the object you want. 2.3 Object Model To create efficient scripts, you need to understand the containment hierarchy of the object model.
  • Page 15 Scripting basics Object Model Application Object Model Classes Document Document History Path Item Channel Selection Art Layer Layer Set Info State Histogram Text Item Path Point (Object) Array Art Layer Layer Set Selection Class The Selection class is used to specify an area of pixels in the active document (or in a selected layer of the active document) that you want to work with.
  • Page 16 Scripting basics Object Model Layer Classes Photoshop has 2 types of layers: an art layer that can contain image contents and a layer set that can contain zero or more art layers. An Art Layer is a layer class within a document that allows you to work on one element of an image without disturbing the others.
  • Page 17 Scripting basics Object Model Open and Save Options Options for opening and saving objects in Photoshop are illustrated below. Open Open Classes Options Generic Generic Photo CD Save Classes Save Options Pict Pict Photoshop JPEG File Resource Pixar TIFF DSC1 DSC2 Targa Solid Color Classes...
  • Page 18 Scripting basics Object Model 2.3.2 Hello World Sample Scripts When all is said and done, the Object Model is simply a means to an end -- writing Photoshop scripts that accomplish something useful using the classes provided. Traditionally, the first thing to accomplish in any programming environment is the display of a "Hello World"...
  • Page 19 The art layer is then changed to a text layer, whose contents are set to "Hello World!". tell application "Adobe Photoshop CS" set docRef to make new document with properties ¬...
  • Page 20 Scripting basics Object Model JavaScript Sample Code This code contains JavaScript commands that instruct Photoshop to remember current unit settings and then create a new document with height and width in inches. An art layer created within the document is then changed to a text layer, whose contents are set to "Hello World!". References are released after the script is run and original ruler unit settings are restored.
  • Page 21: Documenting Scripts

    Scripting basics Documenting scripts 2.4 Documenting scripts It’s recommended that you use comments within your scripts to explain what procedures are taking place. It’s a quick way to document your work for others and an important element to remember when writing scripts. Comments are ignored by the scripting system as the script executes and cause no run-time speed penalty.
  • Page 22: Values

    Scripting basics Values 2.5 Values Values are the data your scripts use to do their work. Most of the time, the values used in your scripts will be numbers or text. AppleScript Values ABLE Value type: What it is: Example: boolean Logical true or false.
  • Page 23 Scripting basics Values Visual Basic Values ABLE Value type: What it is: Example: Boolean Logical true or false True Long Whole numbers (no decimal points). Longs can be positive or negative. Double A number which may 13.9972 contain a decimal point. String A series of text characters.
  • Page 24: Variables

    Scripting basics Variables 2.6 Variables Variables are containers for data. A variable might contain a number, a string, a list (or array), or an object reference. Variables have names, and you refer to a variable by its name. To put data into a variable, assign the data to the variable.
  • Page 25 Scripting basics Variables 2.6.2 Using variables to store references Variables can also be used to store references to objects. In AppleScript, a reference is returned when you create a new object in an Photoshop document as shown below: set thisLayer to make new art layer in current document Or you can fill the variable with a reference to an existing object: set thisLayer to art layer 1 of current document Visual Basic works similarly, however, there is an important distinction to note.
  • Page 26: Operators

    Scripting basics Operators 2.7 Operators Operators perform calculations (addition, subtraction, multiplication, and division) on variables or values and return a result. For example: docWidth/2 would return a value equal to half of the content of the variable docWidth. So if docWidth contained the number 20.5, the value returned would be 10.25.
  • Page 27 The following examples check the number of currently open documents. If no documents are open, the scripts display a messages in a dialog box. tell application "Adobe Photoshop CS" set documentCount to count every document if documentCount = 0 then display dialog "No Photoshop documents are open!"...
  • Page 28 Scripting basics Commands and methods 2.8.2 Control structures Control structures provide for repetitive processes, or “loops.” The idea of a loop is to repeat some action, with or without changes each time through the loop, until a condition is met. Both AppleScript and Visual Basic have a variety of different control structures to choose from.
  • Page 29: Handlers, Subroutines And Functions

    Scripting basics Handlers, subroutines and functions flag = False Do While flag = False retVal = MsgBox("Quit?", vbOKCancel) If (retVal = vbCancel) Then flag = True End If Loop flag = False Do Until flag = True retVal = MsgBox("Quit?", vbOKCancel) If (retVal = vbOK) Then flag = True End If...
  • Page 30: Debugging And Error Handling

    Scripting basics Debugging and Error Handling set flag to DoConfirm("Are you sure?") display dialog flag as string on DoConfirm(prompt) set button to button returned of (display dialog prompt ¬ buttons {"Yes", "No"} default button 1) return button = "Yes" end DoConfirm Private Sub ScriptSample_Click(Index As Integer) result = DoConfirm("Are you sure?") MsgBox (result)
  • Page 31 Scripting basics Debugging and Error Handling 2.10.1 AppleScript debugging While the basic syntax of your script will be checked when compiled, it is possible to create and compile scripts in AppleScript that will not run properly. The Script Editor Application doesn’t have extensive debugging tools, but it does have the an Event Log window.
  • Page 32 Scripting basics Debugging and Error Handling 2.10.2 Visual Basic debugging In Visual Basic, you can stop your script at any point, or step through your script one line at a time. To stop your script at a particular line, select that line in your script and choose “Debug > Toggle Breakpoint”.
  • Page 33: Error Handling

    --Store a reference to the document with the name "My Document" --If it does not exist, display an error message tell application "Adobe Photoshop CS" set docRef to document "My Document" display dialog "Found 'My Document' " on error display dialog "Couldn't locate document 'My Document'"...
  • Page 34: What's Next

    Scripting basics What’s Next 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); 2.11 What’s Next The next chapter covers Photoshop-specific objects and components and describes advanced techniques for scripting the Photoshop application.
  • Page 35: Chapter 3 Scripting Photoshop

    For example, the following script would modify the color profile of all open documents named “MyDocument.” tell application "Adobe Photoshop CS" set color profile kind of document "MyDocument" to none end tell 3.2 Viewing Photoshop objects, commands and methods...
  • Page 36 Scripting Photoshop Viewing Photoshop objects, commands and methods 3.2.1 Viewing Photoshop’s AppleScript dictionary 1. Start Photoshop, then your “Script Editor.” 2. In Script Editor, choose “File > Open Dictionary”. Script Editor displays an “Open Dictionary” dialog. 3. Find and select the Photoshop application and click the “Open” button. Script Editor displays a list of Photoshop’s objects and commands and the properties and elements associated with each object, as well as the parameters for each command.
  • Page 37 1. In any Visual Basic project, choose “Project > References.” If you are using a built-in editor in a VBA application, choose “Tools > References.” 2. Turn on the “Adobe Photoshop CS Object Library” option from the list of available references and click the “OK” button.
  • Page 38 Scripting Photoshop Viewing Photoshop objects, commands and methods The singular advantage to writing JavaScripts is that the scripts run on any platform, regardless of the underlying hardware or operating system. JavaScript is consequently the language of choice for developers not wishing to lock themselves into a single proprietary platform.
  • Page 39: Your First Photoshop Script

    -- Sample script to create a new text item and change its -- contents. tell application "Adobe Photoshop CS" -- Create a new document and art layer. set docRef to make new document with properties ¬ {width:3 as inches, height:2 as inches} set artLayerRef to make new art layer in docRef -- Change the art layer to be a text layer.
  • Page 40 Your first Photoshop script 3.3.2 Visual Basic 1. Start Visual Basic and create a new project. Add the “Adobe Photoshop CS Object Library” reference to the project, as shown earlier. If you are using a built-in editor in a VBA application, skip to step 4.
  • Page 41 Scripting Photoshop Your first Photoshop script For now, enter the following code. The lines preceded by ' (single quotes) are comments, and will be ignored by the scripting system. They’re included to describe the operation of the script. As you look through the script, you’ll see how to create, then address each object.
  • Page 42 Scripting Photoshop Your first Photoshop script 3.3.3 VBScript You don't need to use Visual Basic to run scripts on Windows. Another way to script Photoshop is to use a VBA editor (such as the one that is included in Microsoft Word) or to use Windows Scripting Host.
  • Page 43 Scripting Photoshop Your first Photoshop script Here’s the "Hello, World!" script from Chapter 2, rewritten in VBScript with comments included. ' Hello World Script Dim appRef Set appRef = CreateObject( "Photoshop.Application" ) ' Remember current unit settings and then set units to ' the value expected by this script Dim originalRulerUnits originalRulerUnits = appRef.Preferences.RulerUnits...
  • Page 44 Scripting Photoshop Your first Photoshop script // Hello Word Script // Remember current unit settings and then set units to // the value expected by this script var originalUnit = preferences.rulerUnits; preferences.rulerUnits = Units.INCHES; // Create a new 4x4 inch document and assign it to a variable var docRef = app.documents.add( 4, 4 );...
  • Page 45: Advanced Scripting

    Scripting Photoshop Advanced Scripting 3.4 Advanced Scripting Having familiarized yourself with the basic "Hello, World!" script, you are now ready to move on to more advanced scripting techniques such as configuring document preferences, applying color to text items, and rasterizing text so that wrap an d blur processing can be applied to words.
  • Page 46 Scripting Photoshop Advanced Scripting Document Preferences In this code segment, default configuration settings for the application are first saved into variables so that they can be restored later when the script completes. These are the default configurations you most probably set up using the File/Edit/Preferences dialog when you initially installed and configured Photoshop.
  • Page 47 Scripting Photoshop Advanced Scripting Displaying Colored Text Having generated a default document, you can now display colored text. To do so, first set a local reference to the current document. Then create a SolidColor object and assign RGB color values to it. After defining the text for the current layer, create an art layer of type TEXT. For a complete listing of all JavaScript properties, methods and constants (such as O T E TEXT), please refer to the JavaScript Reference Guide, included as a separate...
  • Page 48 Scripting Photoshop Advanced Scripting Applying a Wave Filter Now that text displays on your document, you’re ready to apply some special effects. First, re- define the width and height of the document in pixels. Additionally, convert the text layer to pixels -- we do this because text is a vector graphic and we need a bitmap in order to manipulate the image.
  • Page 49 Scripting Photoshop Advanced Scripting Applying a MotionBlur Filter Similar code can be used to blur the text in a document. Again create an array of points to designate an area of the document. This time the width is defined as the distance from the middle of the document to the far right side.
  • Page 50 Scripting Photoshop Advanced Scripting 3.4.2 Advanced Visual Basic The following Visual Basic code sample is broken down into four sections, the first of which deals with configuring document preferences. Document Preferences In this code segment, variables are declared and a Photoshop Application object is created. Default configuration settings for the application are saved into variables so that they can be restored later when the script completes.
  • Page 51 Scripting Photoshop Advanced Scripting app.Preferences.RulerUnits = Photoshop.PsUnits.psInches app.Preferences.TypeUnits =_ Photoshop.PsTypeUnits.psTypePixels app.DisplayDialogs =_ Photoshop.PsDialogModes.psDisplayNoDialogs docWidthInInches = 4 docHeightInInches = 2 resolution = 72 helloWorldStr = "Hello, World!" If app.Documents.Count = 0 Then app.Documents.Add docWidthInInches, docHeightInInches,_ resolution, helloWorldStr End If This code produces the blank document displayed below. Displaying Colored Text Having generated a default document, you can now display colored text.
  • Page 52 Scripting Photoshop Advanced Scripting Notice that the position property of the text layer is an array whose values were chosen to roughly center the text in the dialog. A relatively large font size was chosen to increase the visibility of the text message. The color property is the SolidColor object created earlier, whose function is to display text in red.
  • Page 53 Scripting Photoshop Advanced Scripting You can now apply a wave filter to the selection. A truncated sin curve carries the text along for a roller-coaster-like ride. docWidthInPixels = docWidthInInches * resolution docHeightInPixels = docHeightInInches * resolution newTextLayer.Rasterize Photoshop.PsRasterizeType.psTextContents docRef.Selection.Select Array(Array(0, 0), _ Array(docWidthInPixels / 2, 0), _ Array(docWidthInPixels / 2, docHeightInPixels), _ Array(0, docHeightInPixels), Array(0, 0))
  • Page 54 Scripting Photoshop Advanced Scripting To finish up, set application preferences back to their original values. docRef.Selection.Select Array(Array(docWidthInPixels / 2, 0), _ Array(docWidthInPixels, 0), _ Array(docWidthInPixels, docHeightInPixels), _ Array(docWidthInPixels / 2, docHeightInPixels), _ Array(docWidthInPixels / 2, 0)) newTextLayer.ApplyMotionBlur 45, 5 docRef.Selection.Deselect app.Preferences.RulerUnits = startRulerUnits app.Preferences.TypeUnits = startTypeUnits app.DisplayDialogs = startDisplayDialogs...
  • Page 55 A display resolution is declared and the text "Hello, World!" is assinged to a string variable. Finally, a document object is created, if one does not already exist. tell application "Adobe Photoshop CS" activate set theStartRulerUnits to ruler units of settings...
  • Page 56 Scripting Photoshop Advanced Scripting Displaying Colored Text Having generated a default document, you can now display colored text. To do so, first set a local reference to the current document. Then create a SolidColor object and assign RGB color values to it. After defining the text for the current layer, create an art layer of type text layer.
  • Page 57 Scripting Photoshop Advanced Scripting Applying a Wave Filter Now that text displays on your document, you’re ready to apply some special effects. First, re- define the width and height of the document in pixels. Additionally, convert the text layer to pixels -- we do this because text is a vector graphic and we need a bitmap in order to manipulate the image.
  • Page 58 Scripting Photoshop Advanced Scripting This code snippet manipulates and bends the text on the left side of the document. Applying a MotionBlur Filter Similar code can be used to blur the text in a document. Again create an array of points to designate an area of the document.
  • Page 59: Object References

    first layer is now the 2nd layer. Therefore, any references made to layer 1 of current document will now refer to the new layer. Consider the following sample script: tell application "Adobe Photoshop CS" activate Photoshop CS Scripting Guide...
  • Page 60: Visual Basic And Javascript

    This script will not set the name of the layer referenced on the fourth line of the script. Instead it will set the name created on line five. Try referencing the objects by name as shown below: tell application "Adobe Photoshop CS" activate set newDocument to make new document with properties ¬...
  • Page 61: Working With Units

    Scripting Photoshop Working with units The following example shows how to create 2 layers and then rename the first one in JavaScript. // set ruler units and create new document originalRulerUnits = app.preferences.rulerUnits app.preferences.rulerUnits = Units.INCHES; app.documents.add(4,4,72,"My New Document"); docRef = app.activeDocument; layer1Ref = docRef.artLayers.add();...
  • Page 62 Scripting Photoshop Working with units appropriate for your script. After executing a script the original values of the rule settings should be restored if changed in the script. See section 3.6.3, “Changing ruler and type units” on page 61 for directions on setting unit values. AppleScript unit considerations AppleScript provides an additional way of working with unit values.
  • Page 63 Scripting Photoshop Working with units The points and picas unit value types are PostScript points, with 72 points per inch. The traditional points and traditional picas unit value types are based on classical type setting values, with 72.27 points per inch. When working with unit values, it is possible to convert, or coerce, a unit value from one value type to another.
  • Page 64 Scripting Photoshop Working with units 3.6.2 Unit value useage The following two tables list the properties of the classes and parameters of commands that are defined to use unit values. Unit values for these properties and parameter, with the exception of some text item properties, are based the graphics ruler setting.
  • Page 65 Scripting Photoshop Working with units * Unit values based on type ruler setting Command Parameters ABLE AppleScript Visual Basic JavaScript crop Document.Crop document.crop (bounds, height, width) (Bounds, Height, Width) (bounds, height, width) resize canvas Document.ResizeCanvas document.resizeCanvas (height, width) (Height, Width) (height, width) resize image Document.ResizeImage...
  • Page 66: Executing Javascripts From As Or Vb

    Scripting Photoshop Executing JavaScripts from AS or VB In Visual Basic and JavaScript ruler units and type units are properties of the Preferences, accessed through the application object's preferences property as shown below. appRef.Preferences.RulerUnits = PsInches appRef.Preferences.TypeUnits = PsTypePixels appRef.Preferences.PointSize = PsPostScriptPoints app.preferences.rulerUnits = Units.INCHES;...
  • Page 67 ( i = 0; i < arguments.length; ++i ) alert( arguments[i].toString() ) To pass arguments from AppleScript try this: tell application "Adobe Photoshop CS" make new document do javascript (alias <a path to the JavaScript shown above>) ¬ with arguments {1, "test text", (file <a path to a file>),¬...
  • Page 68: The Application Object

    To target the Photoshop application in AppleScript, you must use a tell..end tell block. By enclosing your Photoshop commands in the following statement, AppleScript will understand you are targeting Photoshop. tell application "Adobe Photoshop CS" … end tell In Visual Basic , you create and use a reference to the Application. Typically, you would write: Set appRef = CreateObject("Photoshop.Application")
  • Page 69 Scripting Photoshop The Application object AS: set docRef to current document VB: Set docRef = appRef.ActiveDocument docRef = app.activeDocument; You can also switch back and forth between documents by setting the active document. AS: set current document to document "My Document" VB: appRef.ActiveDocument = appRef.Documents("My Document") app.activeDocument = app.documents["My Document"];...
  • Page 70 Scripting Photoshop The Application object Opening a document When using the open command there are a number of specifiable options. These options are grouped by file type in the provided open options classes. Because the type and contents of the file you are working on affects how it is opened, some of the option values may not always be applicable.
  • Page 71 Scripting Photoshop The Application object The following example shows how to open a generic PDF document. tell application "Adobe Photoshop CS" set myFilePath to alias < a file path > open myFilePath as PDF with options ¬ {class:PDF open options, height:pixels 100, ¬...
  • Page 72: Document Object

    Scripting Photoshop Document object // Get a reference to the file that we want to open var fileRef = new File( < a file path > ); // Create a PDF option object var pdfOpenOptions = new PDFOpenOptions; pdfOpenOptions.antiAlias = true; pdfOpenOptions.height = 100;...
  • Page 73 There are many objects that allow you to specify how you want to save your document. For example, to save a file as a JPEG file, you would use the JPEG save options (JPEGSaveOptions/JPEGSaveOptions) class as shown below. tell application "Adobe Photoshop CS" make new document set myOptions to {class:JPEG save options, ¬...
  • Page 74 "http://www.adobe.com" Set docInfoRef = docRef.Info docInfoRef.Copyrighted = psCopyrightedWork docInfoRef.OwnerUrl = "http://www.adobe.com"...
  • Page 75 Scripting Photoshop Document object 3.9.3 Document manipulation The Document object is used to make modifications to the document image. By using the Document object you can crop, rotate or flip the canvas, resize the image or canvas, and trim the Image. Because unit values are passed in when resizing an image, it is recommended that you first set your ruler units prior to resizing.
  • Page 76: Layer Objects

    The following examples show how to create an art layer filled with red at the beginning of the current document tell application "Adobe Photoshop CS" make new art layer at beginning of current document ¬ with properties {name:"MyBlendLayer", blend mode:normal} select all current document fill selection of current document with contents ¬...
  • Page 77 Scripting Photoshop Layer objects Dim appRef As Photoshop.Application Set appRef = CreateObject("Photoshop.Application") ' Create a new art layer at the beginning of the current document Dim docRef As Photoshop.Document Dim layerObj As Photoshop.ArtLayer Set docRef = appRef.ActiveDocument Set layerObj = appRef.ActiveDocument.ArtLayers.Add layerObj.Name = "MyBlendLayer"...
  • Page 78 Layer objects The following examples show how to create a layer set after the first layer in the current document: tell application "Adobe Photoshop CS" make new layer set after layer 1 of current document end tell Dim appRef As Photoshop.Application Set appRef = CreateObject("Photoshop.Application")
  • Page 79 Scripting Photoshop Layer objects 3.10.1 Setting the Active layer Before attempting to manipulate a layer you must first select it. You can do this by setting the current layer (ActiveLayer/activeLayer) to the one you want to manipulate. set current layer of current document to layer "Layer 1" of ¬ current document docRef.ActiveLayer = docRef.Layers("Layer 1") docRef.activeLayer = docRef.layers["Layer 1"];...
  • Page 80 Scripting Photoshop Layer objects In Visual Basic and JavaScript you’ll have to duplicate and place the layer. Here’s how: Set layerSetRef = docRef.LayerSets.Add Set layerRef = docRef.ArtLayers(1).Duplicate layerSetRef,psPlaceAtEnd layerRef.MoveToEnd layerSetRef var layerSetRef = docRef.layerSets.add(); var layerRef = docRef.artLayers[0].duplicate(layerSetRef, ElementPlacement.PLACEATEND); layerRef.moveToEnd (layerSetRef); 3.10.3 Linking layers Scripting also supports linking and unlinking layers.
  • Page 81: Text Item Object

    Scripting Photoshop Text item object 3.10.4 Applying styles to layers Styles can be applied to layers from your scripts. The styles correspond directly to the styles in the Photoshop Styles palette and are referenced by their literal string name. Here is an example of how to set a layer style to the layer named “L1.”...
  • Page 82 Scripting Photoshop Text item object By changing an art layer's kind, you can also convert an existing layer to text as long as the layer is empty. For example, to create a new text layer, write: make new art layer in current document with properties ¬ { kind: text layer } set newLayerRef = docRef.ArtLayers.Add() newLayerRef.Kind = psTextLayer...
  • Page 83 Scripting Photoshop Text item object docRef.artLayers["my text"].textItem.justification = Justification.RIGHT; The text item object has a kind property, which can be set to either point IMPORTANT: text (psPointText/TextType.POINTTEXT) or paragraph text (psParagraphText/TextType.PARAGRAPHTEXT). When a new text item is created, its kind property is automatically set to point text. The text item properties height, width and leading are only valid when the text item's kind property is set to paragraph text.
  • Page 84 Scripting Photoshop Text item object 3.11.2 Setting text stroke colors Setting the stroke color in AppleScript is a bit different then setting it in Visual Basic or JavaScript. To set the stroke color in AppleScript, use one of the color classes: CMYK color, gray color, HSB color, Lab color, or RGB color.
  • Page 85: Selections

    Scripting Photoshop Selections 5. Create a script to get the font name of the text. An example JavaScript is below: var textLayer = activeDocument.artLayers[0]; if (textLayer.kind == LayerKind.TEXT) alert(textLayer.textItem.font); 6. The name that is displayed in the alert dialog is the PostScript name of the font. Use this name to set the font of your text For example, the above script returned the name “ArialMT.”...
  • Page 86 Scripting Photoshop Selections For example, you may apply effects to a selection or copy the current selection to the clipboard. But remember that you may have to set the active layer before acting on the selection. Here’s how: set current layer of current document to layer "Layer 1" of ¬ current document docRef.ActiveLayer = docRef.Layers("Layer 1") docRef.activeLayer = docRef.layers["Layer 1"];...
  • Page 87 Scripting Photoshop Selections Dim appRef As New Photoshop.Application 'remember unit settings; and set to values expected by this script Dim originalRulerUnits As Photoshop.PsUnits originalRulerUnits = appRef.Preferences.RulerUnits appRef.Preferences.RulerUnits = psPixels 'get selection and replace it Dim docRef As Photoshop.Document Set docRef = appRef.ActiveDocument docRef.Selection.Select Array(Array(50, 60), Array(150, 60), _ Array(150, 120), Array(50, 120)), Type:=psReplaceSelection 'restore unit setting...
  • Page 88 Scripting Photoshop Selections 3.12.2 Stroking the selection border The following examples show how to stroke the boundaries around the current selection and set the stroke color and width. stroke selection of current document using color ¬ {class:CMYK color,cyan:20, magenta:50, yellow:30, black:0}¬ width 5 location inside blend mode vivid light opacity 75 ¬...
  • Page 89 Scripting Photoshop Selections expand selection of current document by pixels 5 contract selection of current document by pixels 5 feather selection of current document by pixels 5 Dim appRef As Photoshop.Application Set appRef = CreateObject("Photoshop.Application") 'remember unit settings; and set to pixels Dim originalRulerUnits As Photoshop.PsUnits originalRulerUnits = appRef.Preferences.RulerUnits appRef.Preferences.RulerUnits = psPixels...
  • Page 90: Filling A Selection

    Scripting Photoshop Selections 3.12.5 Filling a selection You can fill a selection either will a color or a history state. To fill with a color: fill selection of current document with contents ¬ {class: RGB color, red:255, green:0, blue:0} blend mode ¬ vivid light opacity 25 without preserving transparency Set fillColor = CreateObject("Photoshop.SolidColor") fillColor.RGB.Red = 255...
  • Page 91 Scripting Photoshop Selections 3.12.6 Rotating selections You can rotate either the contents of a selection or the selection boundary itself. The first set of examples below shows how to rotate an existing selection 45 degrees. rotate selection of current document angle 45 docRef.Selection.Rotate(45);...
  • Page 92: Working With Filters

    Scripting Photoshop Working with Filters To restore a selection that has been saved to a selection, use the load (Load/load) method as shown below. load selection of current document from channel "My Channel" of ¬ current document combination type extended selRef.Load docRef.Channels("My Channel"), psExtendSelection selRef.load (docRef.channels["My Channel"], SelectionType.EXTEND);...
  • Page 93: Channel Object

    Scripting Photoshop Channel object Dim theChannels As Variant theChannels = Array(docRef.Channels("Red"), docRef.Channels("Blue")) docRef.ActiveChannels = theChannels theChannels = new Array(docRef.channels["Red"], docRef.channels["Blue"]); docRef.activeChannels = theChannels; Or you can easily select all component channels by using the “component channel” property on the document: set current channels of current document to component channels ¬...
  • Page 94 Scripting Photoshop Channel object If you have an RGB document you automatically get a red, blue and a green channel. These kinds of channels are related to the document mode and are called “component channels.” A Channel has a kind property you can use to get and set the type of the channel. Possible values are: component channel, masked area channel, selected area channel and spot color channel.
  • Page 95: Color Objects

    Scripting Photoshop Color objects • spot color channel (psSpotColorChannel, ChannelType.SPOTCOLOR). The examples below show how to create a new masked area channel. make new channel in current document with properties ¬ {kind:masked area channel } Set channelRef = docRef.Channels.Add channelRef.Kind = psMaskedAreaChannel var channelRef = docRef.channels.add();...
  • Page 96 Scripting Photoshop Color objects SolidColor class Visual Basic and JavaScript have an additional class called the SolidColor class. This class contains a property for each color model. To use this object, first create an instance of a SolidColor object, then set its appropriate color model properties. Once a color model has been assigned to a SolidColor object, the SolidColor object cannot be reassigned to a different color model.
  • Page 97 Scripting Photoshop Color objects 3.15.2 Getting and converting colors Here’s how to get the foreground color in AppleScript. get foreground color This may return an RGB color and in some cases you may want the CMYK equivalent. To convert an RGB color to CMYK in AppleScript you would write: convert color foreground color to CMYK VB/JS: The foreground color returns a SolidColor object.
  • Page 98: History Object

    Scripting Photoshop History object 3.15.3 Comparing Colors Using the equal colors (IsEqual/isEqual) commands, you can easily compare colors. These methods will return true if the colors are visually equal to each other and false otherwise. The examples below compare the foreground color to the background color. AS: if equal colors foreground color with background color then VB: If (appRef.ForegroundColor.IsEqual(appRef.BackgroundColor)) Then if (app.foregroundColor.isEqual(backgroundColor))
  • Page 99 Scripting Photoshop History object The code above sets the current history state to the top history state that is in the History palette. Using history states in this fashion gives you the ability to undo the actions that were taken to modify the document. The example below saves the current state, applies a filter, and then reverts back to the saved history state.
  • Page 100: Clipboard Interaction

    Scripting Photoshop Clipboard interaction 3.17 Clipboard interaction The clipboard commands in Photoshop operate on layers and selections. The commands can be used to operate on a single document, or to move information between documents. On Mac OS, Photoshop must be the front-most application when executing these O T E commands.
  • Page 101 Scripting Photoshop Clipboard interaction 3.17.3 Cut The Cut command operates on both art layers and selections, so you can cut an entire art layer or only the selection of a visible layer. The Cut method follows the same rules as the copy command.
  • Page 102 Scripting Photoshop Clipboard interaction 3.17.5 Paste into command The paste into command allows you to paste the contents of the clipboard into the selection in a document. The destination selection border is then converted into a layer mask. You must make the paste command's target document the current document before using the command.
  • Page 103 Index Commands and methods 22 AppleScript 22 Actions 3 Visual Basic 22 AppleScript Comments in scripts 17 advanced 50 AppleScript 17 AppleScript dictionary 32 JavaScript 17 AppleScript Values 18 Visual Basic 17 Application object 64 Conditional statements 23 display dialogs 65 Control structures 24 opening a document 66 Conventions in this guide 1...
  • Page 104 Index Filters 88 functions 25 Number 18, 19 Numeric value types 18, 19 handlers 25 Hello World Object classes 8 sample script 13 Object elements or collections 9 History object 94 Object inheritance 9 Object Model 8 Channel class 11 Containment classes 12 Integer 18 Document class 10...
  • Page 105 Index save 13 Value types save options 68 array 19 Scripting, advanced 41 boolean 18, 19 Scripts folder 5 double 19 Selections 81 integer 18 defining 82 list 18 expand, contract and feather 84 long 19 filling 86 number 18 inverting 84 real 18 loading and storing 87...

This manual is also suitable for:

Photoshop cs

Table of Contents