
Photoshop 7.0 Scripting Guide
42
Scripting Photoshop
Object references
3
3.4 Object references
3.4.1 AppleScript
AppleScript uses object references to identify the target object for commands. When working
with Photoshop you can identify each item in an object reference using either index or name
form. For example, if you have a single document, named “My Document”, open, you could
target the document’s first layer, named “Cloud Layer” with either line:
layer 1 of document 1
or
layer "cloud layer" of document "My Document"
N
O T E
:
When scripting Phothshop a document's index is not always the same as its stacking
order in the user interface. It is possible for
document 1
to not be the front-most
document. For this reason Photoshop will always return object references identifying
documents by name. It is recommended that you always use the name form when
identifying documents in your scripts.
An object's index or name also may change as a result of manipulating other objects. For
example, when a new art layer is created in the document, it will become the first layer, and
the layer that was previously the 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:
1.
tell application "Adobe Photoshop 7.0"
2.
activate
3.
set newDocument to make new document with properties ¬
{ width: inches 2, height: inches 3}
4.
set layerRef to layer 1 of current document
5.
make new art layer in current document
6.
set name of layerRef to "My layer"
7.
end tell
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:
1.
tell application "Adobe Photoshop 7.0"
2.
activate
3.
set newDocument to make new document with properties ¬
{ width: inches 2, height: inches 3}
4.
make new art layer in current document with properties {name: "L1" }
5.
make new art layer in current document with properties {name: "L2" }
6.
set name of art layer "L1" of current document to "New Layer 1"
7.
end tell