Copy link to clipboard
Copied
Hi everyone,
I spent about half a day looking for what I want. I've been reading the Illustrator scripting guide, and browsed forums but I can't find an answer so here is my question :
I have a file in Illustrator.
I have 3 layers in this file called "L", "M" and "S" (for sizes).
In each layer, there are two text elements with something written inside.They are always named "Name" and "Number". I created everything using my mouse and keybord, no script.
It looks like :
L (layer)
Name (text)
Number (text)
M (layer)
Name (text)
Number (text)
S (layer)
Name (text)
Number (text)
I want to select one specific Name in layer M for example. I can't find a way to do it...
var myDoc = app.activeDocument
var myLayer = myDoc.layers[1] /* myLayer is "M" */
I read that it is possible to access the wanted textframe by using : myDoc.textFrames[2] /* 2 refers to the third text element in the document textframes. */
My problem is that I can have a bigger file with tons of other textframes, so I'd like to get the one called "Name" in the "M" Layer without knowing its rank in the document textframes.
I tried var myText = myDoc.myLayer.textFrame but obviously myLayer doesn't have textframes, it only works with documents, or I miss something.
I hope you guys understand my problem.
Thank you for your help.
Hi totom, welcome to the forum
Layers do have textFrames, you were really close, here's the syntax for doing what you want
var idoc = app.activeDocument;
var ilayer = idoc.layers["M"];
var iframe = ilayer.textFrames["Name"];
iframe.selected = true;
Copy link to clipboard
Copied
Hi totom, welcome to the forum
Layers do have textFrames, you were really close, here's the syntax for doing what you want
var idoc = app.activeDocument;
var ilayer = idoc.layers["M"];
var iframe = ilayer.textFrames["Name"];
iframe.selected = true;
Copy link to clipboard
Copied
Awesome, thank you very much CarlosCanto. It works great!
It kinda pisses me off having spent that much time on something so easy. Thanks again
Copy link to clipboard
Copied
you're welcome, don't get discouraged, that's how it works....very slow at the beggining, that's a normal learning curve.
Copy link to clipboard
Copied
I've been programming in quite a few languages so far, but never in Javascript and especially javascript for Illustrator ^^
I'm planning on writting a nice little script to save me quite some time. I'm really motivated so it should help. My main problem was to be able to select items. Now that I'm good to go, it should go faster
Thanks again.
Copy link to clipboard
Copied
There is very little need to use 'selection' you only need target an item correctly in order to manipulate it…
Copy link to clipboard
Copied
Oh, man.... this was driving me nuts. This script wasn't working for me. It was hanging on the line "var iframe = ilayer.textFrames["Name"];". Turns out I had to rename the text object before the script could see the name (even though it showed up as named in the AI object Options dialog). This appears to be true for any object. They all appear to get default names, but they aren't names a script will see. You have to rename the object for a script to see the name. Maybe someone can verify that for me, or perhaps that I'm just loopy.
Also, in the CS5 Scripting Reference it doesn't even show that textFrameItems have a name property. That was the first thing that through me off. When the script didn't work I thought, of course it doesn't work, textFrameItems don't have a name property. Turns out they do
Copy link to clipboard
Copied
You should look at 'pageItems' you will see that there are super classes from which other classes inherit properties…
Copy link to clipboard
Copied
Ahhh, yes.... still getting the hang of object oriented programming But still, in the Scripting Reference everything else (including legacyTextItems) shows a name property, but not for textFrameItems. That's what threw me off. Thanks for setting me straight.
Can you confirm the item naming anomoly? Items appear to be named, but not really until you enter a new name.
Copy link to clipboard
Copied
Red, sorry about that, I didn't think of it. I wasn't aware it is not in the Documentation, I come from VB and usually work on a VB version first then rewrite to JS, I just love the VB IDE (don't like at all the ESTK). It turns out THERE IS a name property in the VBS documentation, I'm not sure what's going on, there's a bunch on differences between the two.
About the naming thing, what you see in the layers palette is not a name, but more like a "preview" of the text, if not explicitly named, the name property returns "null" (I guess, or an empty string, but not a name).
Copy link to clipboard
Copied
Yes, those "names" in the layers pallette aren't really names, and scripts return null (or something). Until you edit the name. Then it becomes an actual item name that the script can see. And it's not just text, it's all the items (paths, groups, etc.) in the layers pallette. Text items just happen to show the textFrame contents. Somewhat odd. But now that it's understood, it's not a problem. Good to see that it wasn't just happening to me though. Thanks.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now