5 Replies Latest reply on Jan 3, 2013 6:15 AM by shilpa25

    change text frame contents

    shilpa25 Level 1

      hi forum,

       

       

      this is my small script.. to find and change contents...

       

      var myDoc = application.activeDocument;

      var TF = myDoc.pages.everyItem().textFrames.everyItem().getElements();

          if (TF.contents == "©NM") {

          TF.contents = "©LW";

         }

       

      but nothing happens;;  any help please.

       

      thanks shil...

        • 1. Re: change text frame contents
          Jump_Over Level 5

          Hi,

           

          Watch your small script structure:

           

          TF is an array of every textFrames in myDoc.

          You can not assign .content to this. It could work if you go into the loop.

          But simpler way is to use find...change, I guess.

           

          rgds

          • 2. Re: change text frame contents
            KuddRoww Level 1

            I haven't tested but:

             

            var myDoc = app.activeDocument;

            tf = app.activeDocument.textFrames;

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

            textFrame = tf[i];

            if(textFrame.contents=="©NM"){textFrames.contents = "©LW"};

            }

            Should work fast enough, but as Jump_Over mentioned you could also try a find/replace method, the built in one is much faster than looping through textframe contents.

             

            Looks a little something like this:

             

            var doc = app.activeDocument;

            app.findTextPreferences.findWhat = "I don't know how to change text.";

            app.changeTextPreferences.changeWhat = "I know how to change text";

            doc.changeText();

            • 3. Re: change text frame contents
              shilpa25 Level 1

              hi Jump_over,

               

              thanks,

               

              actually, i tried yours & modified like this...

               

              var myDoc = app.activeDocument;

              tfs= app.activeDocument.textFrames.everyItem().getElements();

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

              tf = tfs[i];

              if(tf.contents=="©NM")

                  tf.remove();

                  }


              then, creating textframe on the same place and inputting new text...with the below script....

               

              var myDocument = app.activeDocument;

                        ViewPreference.rulerOrigin = RulerOrigin.pageOrigin;

                        myDocument.zeroPoint = [0,0];

                        with(myDocument){

                        var myPage = myDocument.pages.item(0);

                        with (myDocument.documentPreferences){

                        var myPageHeight = pageHeight;

                        var myPageWidth =  pageWidth;}

                        var myTextFrame = textFrames.add();

                 myTextFrame.geometricBounds = [ myPageHeight-2.5, myPageWidth-6.5, myPageHeight-1, myPageWidth-1];

                myTextFrame.contents = "XXX"

               

               

              but my request is , without removing the ©NM (textframe) how can i use if & else if statement.....

              ie. if the textframe content is ©NM change to XXX and if there is no text frame with content ©NM,, then create new text frame and put XXX. as above script...

               

              many thanks for your reply...

              thanks & rgds.

              shil....

              • 4. Re: change text frame contents
                Jump_Over Level 5

                Yeap,

                 

                Notice that often the problem is not how to do it but how to describe this...

                Script will try to run in every possible situations, not only those you are actually met.

                 

                So how many pages is, or could be, in your doc?

                Are there any others textFrames in your doc then described one?

                ...

                cause yours "tfs = app.activeDocument.textFrames.everyItem().getElements();" says that there is at least few of them...

                ...

                What it has to be done if there is a textFrame with another content then you are looking for? Leave it? Remove it? Change the content?

                 

                If "there is no textFrame" is going to be a starter of "to do something" - it leads to never ending story, cause you (script) always can detect: "there is no text Frame". You have to determine more circumstances then only "no exist" one

                 

                If your doc is a "1 frame per page structure", your script could be like this:

                 

                For every page:

                If  (page.textFrames.length > 0)

                     {

                     If (page.textFrames[0].contents == "some")

                          page.textFrames[0].contents == "another";

                     }

                else

                     page.textFrames.add ( {geometricBounds: myGeo, content: "another"} )

                 

                 

                rgds

                1 person found this helpful
                • 5. Re: change text frame contents
                  shilpa25 Level 1

                  thanks so much Jump_over,

                   

                  I understood. thats fine!  

                   

                  thanks very much.