Copy link to clipboard
Copied
Hello.
When I try to query the name of a text frame from AI 25.3.1 under macOS 11 with the following Javascript, I only get an empty string back.
Am I doing something wrong or is it a bug?
Javascript:
// Save Name
var vName1 = app.activeDocument.textFrames[0];
// Show Name
alert (vName1);
Screenshot:
Maybe not you. But it may take more effort on your part to fix it.
By default, text frames are un-named. It doesn't look like that, because Illustrator places the first words/characters inside a text frame into the layer/sub-layer name as a proxy. The proxy appears to name the text frame/layer, but it does not. It serves as the proxy to illustrate there is no name.
I don't know how to name a text frame. But I do know how to name a layer/sub-layer, which may or may not work for your data need
...your text frames are not named, what your see in the layers panel is a preview of their contents.
your variable vName1 holds the actual Text Frame, to query it's name
var vName1 = app.activeDocument.textFrames[0].name; // this will return an empty string since your frame is not named
if you're trying to get the contents of the frame
var vName1 = app.activeDocument.textFrames[0].contents; // returns Abc
to name your frame, double click on it in the layers panel and type a name for example "
...Copy link to clipboard
Copied
Maybe not you. But it may take more effort on your part to fix it.
By default, text frames are un-named. It doesn't look like that, because Illustrator places the first words/characters inside a text frame into the layer/sub-layer name as a proxy. The proxy appears to name the text frame/layer, but it does not. It serves as the proxy to illustrate there is no name.
I don't know how to name a text frame. But I do know how to name a layer/sub-layer, which may or may not work for your data needs.
Open the Layers panel, as you've shown above, and double-click inside the proxy text for your targeted layer/sub-layer. This highlights all the proxy text, and will replace it with whatever you subsequently type. Let's name it Fred. Now click away, and you'll have a new layer/sub-layer named Fred.
Please note that I don't know if naming a sub-layer with an element on it is the same as naming an element for your data needs. But it will at least give you something to put between the "..." quote marks to find out.
Hope this helps,
Randy
Copy link to clipboard
Copied
Hi Randy,
thanks for your answer. I need the textframe, but I can get my information from .contents, which is better for my script to come.
Jens.
Copy link to clipboard
Copied
You can name a text frame like any other object or container, just double-click the current name (or sudo-name) and rename it to your liking. This does not affect the content of the text frame, but makes it more scriptable. I see Carlos already fully explained this. 🙂
Copy link to clipboard
Copied
your text frames are not named, what your see in the layers panel is a preview of their contents.
your variable vName1 holds the actual Text Frame, to query it's name
var vName1 = app.activeDocument.textFrames[0].name; // this will return an empty string since your frame is not named
if you're trying to get the contents of the frame
var vName1 = app.activeDocument.textFrames[0].contents; // returns Abc
to name your frame, double click on it in the layers panel and type a name for example "myFrame"
or you can also name it programmatically
app.activeDocument.textFrames[0].name = "myFrame";
Copy link to clipboard
Copied
Thanks Carlos,
I will use .contents for my solution.
Starting with Javascript in AI is very hard and the documentation I found is not very helpfull so far.
Jens.
Copy link to clipboard
Copied
you're right about that. not that you asked, but technically it's not even javascript, which definitely contributes to the difficulty in finding useful resources. If you look up any kind of javascript help.. you're not likely to find what you need since modern javascript is many versions beyond "ExtendScript" (which is the language that is masquerading as javascript in the illustrator API).
Here's a pretty decent reference manual that's easily searchable and has lots of great links making it easy to navigate from concept to concept so you can see how things are supposed to be strung together.
https://ai-scripting.docsforadobe.dev/jsobjref/javascript-object-reference/
Copy link to clipboard
Copied
Here are two more examples:
var aTF = app.activeDocument.textFrames[0];
var vName1 = (aTF.name != "") ? aTF.name : "Sorry, this is an unnamed text frame.";
alert (vName1);
or
var aTF = app.activeDocument.textFrames[0];
var vName1 = (aTF.name != "") ? aTF.name : app.activeDocument.textFrames[0].name = "New name: " + aTF.contents.slice(0,5) + "…";
alert (vName1);
Try them out and
have fun
😉
Copy link to clipboard
Copied
Hi,
thanks for the examples. that will help.
have a goot day, jens.