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

How to select item by name using JavaScript

Explorer ,
Feb 03, 2020 Feb 03, 2020

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

TOPICS
Scripting
4.2K
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 ,
Feb 03, 2020 Feb 03, 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

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 ,
Feb 03, 2020 Feb 03, 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);

 

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 ,
Feb 03, 2020 Feb 03, 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

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 ,
Feb 03, 2020 Feb 03, 2020

I don't think I'm good enough at extendscript to do that yet 🙂

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 ,
Feb 03, 2020 Feb 03, 2020

I would like InDesign to look for an the item only on the active page. Do you have an idea how to do it?

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 ,
Feb 04, 2020 Feb 04, 2020

Hi,

you could access the active page like that:

app.documents[0].layoutWindows[0].activePage

 

Also possible, the active spread:

app.documents[0].layoutWindows[0].activeSpread

 

But since Layer is an object of Document and not of Spread or Page you can only look directly into a named page item on the page or spread without restricting it to a specific layer.

 

If there are different objects on the page with the same name you could access all page items on the page and loop until you find the one in your desired layer by checking the itemLayer property plus the name property of an object. If the named object is part of a nested structure like a group you could loop the allPageItems array of a page or a spread until your named object is found.

 

Regards,
Uwe Laubender

( ACP )

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 ,
Feb 04, 2020 Feb 04, 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 )

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 ,
Jan 30, 2024 Jan 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?

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 ,
Jan 30, 2024 Jan 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.

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 ,
Jan 30, 2024 Jan 30, 2024

What error do you get @leo.r ? I rechecked and I do get a layer object which is invalid.

-Manan

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 ,
Jan 30, 2024 Jan 30, 2024
quote

What error do you get leo.r ? I rechecked and I do get a layer object which is invalid.

-Manan


By Manan Joshi

 

Well, admittedly I called JS from AppleScript (it's just easier for me under my setup) so I assumed the result would be the same in pure JS:

 

 

 

do script "app.documents[0].layers.itemByName(\"fakeName\")" language javascript

 

 

 

I should have mentioned it - sorry!

 

I'm getting an "Object is invalid" error when using a fake name.

 

But I guess the issue of returning a layer with non-existing name is purely a JavaScript thing.

 

AppleScript always returns an error on attempt to get a layer (or another element) when using a non-existing name, for example:

 

 

 

tell document 1 to get layer "fakeName"

 

 

 

Error: Can’t get layer "fakeName" of document 1.

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 ,
Jan 31, 2024 Jan 31, 2024

Ok, never tried my hands much on Applescript and my explanation was entirely directed towards JS so yeah we both might be reporting correct behavior albeit the language is different.

-Manan

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 ,
Jan 30, 2024 Jan 30, 2024

Sure, you can check whether the returned layer is valid like so:

document.layers.itemByName("fdafdsf").isValid;

... which returns true if it is, and false if it isn't.

 

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 ,
Feb 01, 2024 Feb 01, 2024
LATEST
quote

Sure, you can check whether the returned layer is valid like so:

document.layers.itemByName("fdafdsf").isValid;

... which returns true if it is, and false if it isn't.

 


By Tá´€W


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
Community Expert ,
Jan 30, 2024 Jan 30, 2024

When you interact with collections like layers, paragraphs, pageItems etc to get a particular element they almost always return an object even if that object is not valid. It is because the code like layers.itemByName("layerName") does not hit the actual object unless you try and access a property of the object, that is when it is resolved and you get an exception if the object is invalid. So always check the isValid property before using the object as suggested by @Tá´€W 

-Manan

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