Copy link to clipboard
Copied
Hi guys,
I am new to scripting for adobe photoshop. Learning how to do it by following 3 documents:
1. "ADOBE® INTRODUCTION TO SCRIPTING"
2. Photoshop scripting guide 2020
3. Photoshop javascript reference 2020
The version of Photoshop I am using is Photoshop 22.2.0 and I am scripting in VS Code, using the ExtendScript Debugger to evaluate the active file in Photoshop
The code I am trying to run is as shown below:
Ah, it wasn't clear that you were trying to use a with() statement, your previous code was wrong. You also need to add a new artLayers not layers
var myDoc = app.documents.add();
var myLayer = myDoc.artLayers.add();
with(myLayer) {
name = "My New Layer";
visible = false;
}
I personally don't use with() statements very often, but some folk do like them. They are OK for ExtendScript, but depreciated in modern JavaScript.
I have also found over time as I repurpose code that I no longe
...I don't believe that I have used .properties before and no, your sample didn't work for me, even when I change layers.add() to artLayers.add()
Perhaps somebody else has some insight?
By @Joseph25509234xr72What am I doing wrong?
It looks like this is an example for Illustrator or InDesign, but not for Photoshop.
Copy link to clipboard
Copied
Where did you find the following construct?
.properties = { name:"My New Layer", visible:false }
This works:
var myDoc = app.documents.add();
var myLayer = myDoc.artLayers.add();
myLayer.name = "My New Layer";
myLayer.visible = false;
Copy link to clipboard
Copied
Thanks for your response.
I got it from the "ADOBE® INTRODUCTION TO SCRIPTING" manual. It said it is a shorter way to add multiple properties to the object.
It also describe the method you have stated. Here is the excerpt from the manual (page 18) which touches on properties and how to change them using JavaScript (I have also attached the said manual - please check page 18 in it and advise. I will really appreciate.):
JS
To use a property in JS, you name the object that you want the property to define or modify, insert a
period (.), and then name the property. To specify the value, place an equal sign (=) after the property
name, and then type the value.
var myDoc = app.documents.add()
var myLayer = myDoc.layers.add()
myLayer.name = "My New Layer"
To define multiple properties, you can write multiple statements:
var myDoc = app.documents.add()
var myLayer = myDoc.layers.add()
myLayer.name = "My New Layer"
myLayer.visible = false
NOTE: Notice in the preceding script that only the string value "My New Layer" is enclosed in quotes. The
value for the visible property, false, may look like a string, but it is a Boolean value. To review value
types, see “Using properties” on page 16.
JS provides a shorthand for defining multiple properties, called a with statement. To use a with statement,
you use the word with followed by the object whose properties you want to define, enclosing the object
reference in parentheses (()). Do not type a space between with and the first parenthesis. Next, you type
an opening curly brace ({), and then press Enter and type a property name and value on the following
line. To close the with statement, you type a closing curly brace (}).
var myDoc = app.documents.add()
var myLayer = myDoc.layers.add()
with(myLayer){
name = "My New Layer"
visible = false
}
Using a with statement saves you the trouble of typing the object reference followed by a period (in this
case, myLayer.) for each property. When using a with statement, always remember the closing curly
bracket.
JS also provides a properties property, which allows you to define several values in one statement. You
enclose the entire group of values in curly braces ({}). Within the braces, you use a colon (:) to separate a
property name from its value, and separate property name/property value pairs using a comma (,).
var myDoc = app.documents.add()
var myLayer = myDoc.layers.add()
myLayer.properties = {name:"My New Layer", visible:false}
Copy link to clipboard
Copied
Ah, it wasn't clear that you were trying to use a with() statement, your previous code was wrong. You also need to add a new artLayers not layers
var myDoc = app.documents.add();
var myLayer = myDoc.artLayers.add();
with(myLayer) {
name = "My New Layer";
visible = false;
}
I personally don't use with() statements very often, but some folk do like them. They are OK for ExtendScript, but depreciated in modern JavaScript.
I have also found over time as I repurpose code that I no longer use variable names as much as I used to, as the variable names often change and the code breaks when I miss them, so keeping them generic unless otherwise necessary helps me.
app.documents.add();
app.activeDocument.artLayers.add();
with(activeDocument.activeLayer) {
name = "My New Layer";
visible = false;
}
Copy link to clipboard
Copied
Thanks for the tips and advice Stephen.
I really appreciate it as a newbie at this 🙂
just one more clarification about the part below:
JS also provides a properties property, which allows you to define several values in one statement. You
enclose the entire group of values in curly braces ({}). Within the braces, you use a colon (:) to separate a
property name from its value, and separate property name/property value pairs using a comma (,).
var myDoc = app.documents.add()
var myLayer = myDoc.layers.add()
myLayer.properties = {name:"My New Layer", visible:false}
does this work? using .properties as shown above?
Copy link to clipboard
Copied
I don't believe that I have used .properties before and no, your sample didn't work for me, even when I change layers.add() to artLayers.add()
Perhaps somebody else has some insight?
Copy link to clipboard
Copied
By @Joseph25509234xr72What am I doing wrong?
It looks like this is an example for Illustrator or InDesign, but not for Photoshop.
Copy link to clipboard
Copied
Hi r-bin,
You are right. I tried it in Illustrator but it didn't work.
I later tried it in InDesign and it worked. It is for InDesign.
Thanks.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more