Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Defining multiple properties for a photoshop object using Javascript

Explorer ,
Apr 27, 2023 Apr 27, 2023

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:

 
var myDoc = app.documents.add()
var myLayer = myDoc.artLayers.add()
myLayer.properties = { name:"My New Layer", visible:false }
 
However, when I run it, it is just creating the new document and new layer, but not renaming the layer to "My New Layer" and also not making it invisible.
 
What am I doing wrong? 
 
Please help guys.
TOPICS
Actions and scripting
2.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 3 Correct answers

Community Expert , Apr 27, 2023 Apr 27, 2023

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

...
Translate
Community Expert , Apr 27, 2023 Apr 27, 2023

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?

Translate
People's Champ , Apr 27, 2023 Apr 27, 2023
quote
What am I doing wrong? 
By @Joseph25509234xr72

 

 

It looks like this is an example for Illustrator or InDesign, but not for Photoshop.

Translate
Adobe
Community Expert ,
Apr 27, 2023 Apr 27, 2023

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;
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 27, 2023 Apr 27, 2023

Hi Stephen_A_Marsh,

 

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}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 27, 2023 Apr 27, 2023

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;
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 27, 2023 Apr 27, 2023

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 27, 2023 Apr 27, 2023

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Apr 27, 2023 Apr 27, 2023
quote
What am I doing wrong? 
By @Joseph25509234xr72

 

 

It looks like this is an example for Illustrator or InDesign, but not for Photoshop.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 28, 2023 Apr 28, 2023
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines