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

Change Text in a Text Box with Script

Explorer ,
Sep 06, 2011 Sep 06, 2011

Hi,

I have a text box that lists the word "English" which identifies the English Manual on the title page. I copy and translate this manual. On the translated manuals, I want to change this "English" into the word "Spanish" or "Japanese" using a script. What would be the right way to do this? I could do a find change that targeted only the first page or is there some way to give a text box a unique identifier to call out in the code? Is this what "script labels" do?

TOPICS
Scripting
8.9K
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

correct answers 1 Correct answer

Explorer , Sep 06, 2011 Sep 06, 2011

Hi

The label property depends if you use CS5 or up.

CS4 and previous versions it was sufficient to use the script label.

Select the frame with the selection tool and write the name on the Script Label palette.

CS5 and up you need to select the text frame, open the Layers palette, find the text frame you wish to name, you will see the color indication, then you should click the <text frame> label on that palette and give it the label you want.

anyway, the code to use it is, if you call your frame as "

...
Translate
Explorer ,
Sep 06, 2011 Sep 06, 2011

Hi

The label property depends if you use CS5 or up.

CS4 and previous versions it was sufficient to use the script label.

Select the frame with the selection tool and write the name on the Script Label palette.

CS5 and up you need to select the text frame, open the Layers palette, find the text frame you wish to name, you will see the color indication, then you should click the <text frame> label on that palette and give it the label you want.

anyway, the code to use it is, if you call your frame as "a":

myFrame = app.activeDocument.textFrames.itemByName("a");

hope this help

Cheers,

Eli

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 ,
Sep 06, 2011 Sep 06, 2011

Worked like a charm. I filled it using the myFrame.contents = "Spanish" and it works great.

You wouldn't happen to know how to reference the layers group would you? The layers group that is named "Rev line". I tried and was unsuccessful:

myFrame = app.activeDocument.textFramesGroup.itemByName("Rev Line").textFrames.itemByName("a");


I tried looking it up in the Scripting Object Library and there was a Group.textFrame but not sure how to use it here.

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 ,
Sep 06, 2011 Sep 06, 2011

Did you mean the whole layer? or a group of objects?

I will answer both:

Reference the whole layer:

app.activeDocument.layers.itemByName("Rev Line")

Reference a group of objects in a specified layer:

app.activeDocument.layers.itemByName("Rev Line").groups.itemByName("myGroup").name

or reference the group from the page items collection:

app.activeDocument.pageItems.itemByName("myGroup").name

or reference the group from the document's groups collection:

app.activeDocument.groups.itemByName("myGroup").name

I hope this helps

Regards,

Eli

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 ,
Sep 07, 2011 Sep 07, 2011

Thank you Eli, this is very helpful and educational as well. I really appreciate your help.

Here is a screenshot of the layer:

Region Capture.png

As you can see, "Rev Line" is a group and holds my Language text box. Its basicly a text box inside a text box. So would this be correct?

var myFrame = app.activeDocument.layers.itemByName("Rev Line").groups.itemByName("Language").name;

myFrame.contents = "Spanish"

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 ,
Sep 07, 2011 Sep 07, 2011

I cannot figutre exactly from your screenshot for each object what type it is.

"Its basicly a text box inside a text box" - Inside means that the frame text is anchored inside the parent text frame? if so the 'child' text frame will be considered as a character inside the 'parent' text frame, so you should reference it from the characters collection.

Anyway, in order to reference an object, simply go through the collection->object->collection->object, figure out which type it is and try to debug it on the javaScript console.

good luck

Eli

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
LEGEND ,
Sep 07, 2011 Sep 07, 2011

No.

You probably need something like this:

var myFrame = app.activeDocument.groups.itemByName("Rev Line").textFrames.itemByName("Language").name;

Alternatively, you can do something like this to find your frame:

var myFrame = getObjectByName(app.documents[0],"Language");

function getObjectByName(container,name){

  var objects = container.allPageItems.slice();

  for(var i=0;i<objects.length;i++){

    if(objects.name == name){

      return objects;

    }

  }

  return null;

}

You can feed into the function any item which has an allPageItems property. That includes Document or Layer among others...

Harbs

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 ,
Sep 09, 2011 Sep 09, 2011

Eli and Harbs,

Thank you so much for your help. I was unable to call it using the:

var myFrame = app.activeDocument.groups.itemByName("Rev Line").textFrames.itemByName("Language").name;

But your other code worked like a charm!!!

var myFrame = getObjectByName(app.documents[0],"Language");
 
function getObjectByName(container,name){
  var objects = container.allPageItems.slice();
  for(var i=0;i<objects.length;i++){
    if(objects.name == name){
      return objects;
    }
  }
  return null;
}

I will add an if statement to this so if the text box is not in the document, the user will get an message box.

Thanks again for your help.

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 Beginner ,
Oct 02, 2017 Oct 02, 2017

Hi,

I'm also struggling with getting and changing the text of layers, I tried all the solution provided over here but didn't got success.

I've this type of layer structure in AI

Screen Shot 2017-10-02 at 12.45.59 PM.png

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 ,
Oct 02, 2017 Oct 02, 2017
LATEST

You're in the InDesign forum, and Illustrator are different from InDesign scripts. You should ask in the/a Illustrator forum.

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