Skip to main content
magikct
Known Participant
February 3, 2020
Question

How to select item by name using JavaScript

  • February 3, 2020
  • 2 replies
  • 5037 views

I need to select item "width" of layer "dimensions" (like on the attached pic) by js script. Anyone can help me?

This topic has been closed for replies.

2 replies

Braniac
February 4, 2020

Another annotation:

app.documents[0].layers.itemByName("NameOfLayer").pageItems

If called by layer you can gather page items on document spreads only!

It will not refer to page items on master spreads.

 

An old discussion: Some consider this a bug, some think this is by design.

But everyone will agree, I think, that this is very unfortunate.

 

Regards,
Uwe Laubender
( ACP )

Known Participant
January 30, 2024

Of course the problem being that layers.itemByName("layerName") doesn't work. It always returns with a layer object even if one with that name doesn't exist. Does anyone know of a valid way to check to see if a layer already exists?

leo.r
Braniac
January 30, 2024
quote

Of course the problem being that layers.itemByName("layerName") doesn't work. It always returns with a layer object even if one with that name doesn't exist. 


By @Ken Webster

 

I can't reproduce it - it returns an error if a layer with that name doesn't exist.

EDIT: Correct info below.

Braniac
February 3, 2020

Try the following

app.documents[0].layers.itemByName("dimensions").pageItems.itemByName("width")

 

In case the layer contains more than one element named width you will get just a single element

 

-Manan

magikct
magikctAuthor
Known Participant
February 3, 2020

No, but I modified your code a little bit, and it's working. Thank you very much.

 

 

 

app.select(NothingEnum.NOTHING);
app.documents[0].layers.itemByName("dimensions").pageItems.itemByName("width").select(SelectionOptions.ADD_TO);

 

Braniac
February 3, 2020

Ahh sorry it thought by select you meant getting a reference to the object to do an operation over it. Anyhow i see you have added the needed parts to make it work, just add in error checks as well to handle condtions like the layer or the object does not exist and then you should have a pretty robust code snippet

 

-Manan