7 Replies Latest reply on Sep 9, 2011 7:14 AM by RichardM0701

    Change Text in a Text Box with Script

    RichardM0701 Level 1

      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?

        • 1. Re: Change Text in a Text Box with Script
          Eli.Shklovsky Level 1

          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

          • 2. Re: Change Text in a Text Box with Script
            RichardM0701 Level 1

            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.

            • 3. Re: Change Text in a Text Box with Script
              Eli.Shklovsky Level 1

              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

              • 4. Re: Change Text in a Text Box with Script
                RichardM0701 Level 1

                 

                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"
                
                • 5. Re: Change Text in a Text Box with Script
                  Eli.Shklovsky Level 1

                  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

                  1 person found this helpful
                  • 6. Re: Change Text in a Text Box with Script
                    Harbs. Level 6

                    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[i].name == name){
                          return objects[i];
                        }
                      }
                      return null;
                    }
                    

                     

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

                     

                    Harbs

                    1 person found this helpful
                    • 7. Re: Change Text in a Text Box with Script
                      RichardM0701 Level 1

                      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[i].name == name){
                            return objects[i];
                          }
                        }
                        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.