How to select item by name using JavaScript
Copy link to clipboard
Copied
I need to select item "width" of layer "dimensions" (like on the attached pic) by js script. Anyone can help me?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
I don't think I'm good enough at extendscript to do that yet 🙂
Copy link to clipboard
Copied
I would like InDesign to look for an the item only on the active page. Do you have an idea how to do it?
Copy link to clipboard
Copied
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 )
Copy link to clipboard
Copied
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 )
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
What error do you get @leo.r ? I rechecked and I do get a layer object which is invalid.
-Manan
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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

