9 Replies Latest reply on Nov 15, 2010 7:28 AM by lhanneman

    [JS] create textframe programatically?

    lhanneman Level 1

      hello,

       

      I'm trying to programatically build up a new textframe and populate it with text, paragraph/character styles. Is this possible? So for I've tried:

       

      var newTextFrame = new TextFrame; //This works

      newTextFrame.contents = 'hello'; //This also works

       

      but if I try to use:

      newTextFrame.insertionPoints[-1].contents = 'hello'; // "Error: Undefined is not an object"

       

      I get the same error if I try and set pointsize, font, or fill color using the InsertionPoints[-1] method. is there a better way to do this, or is it even possible?

       

      I should mention that these are snippets I've used before, only for existing text frames in a document, i.e. updating a current text frame. But now I need to be able to create a new one on the fly.

       

      thanks in advance!

        • 1. Re: [JS] create textframe programatically?
          [Jongware] Most Valuable Participant

          You cannot create any native InDesign object using "new" -- you have to 'add' them to an existing object.

          In the case of text frames, you can add them to the document (in which case they appear on the first page), or to any other page.

           

          As in:

           

          frame = doc.textFrames.add ({geometricBounds:[".5in",".5in","30in","7.5in"]});
          frame.contents = "hello there!";
          
          • 2. Re: [JS] create textframe programatically?
            lhanneman Level 1

            so it is not possible to build up a text frame object in memory then? I mean without adding it to the page?

             

            Is it possible to create a text object in memory with styles and content, and then when I'm finished, insert it into a text frame?

             

            The reason I want to build all the values up is because in the scenario of updating a group of text frames that are threaded together, it is difficult to handle carriage returns. I want to be able to build up all of the text, insert it into the first text frame, and then let it automatically flow into the threaded text frames. does that make sense? Is there an easy way to do that?

            • 3. Re: [JS] create textframe programatically?
              lhanneman Level 1

              Actually that worked!

               

              I've got a script that will update two grouped & threaded text frames. Instead of clearing out the text frames and adding the new text/styles to the "start" text frame, I created a "temp" textFrame with your example, add ALL of the text/styles to that, and then do something like:

               

              myStartTextFrame.contents = tempTextFrame.contents;

              tempTextFrame.remove(); //un-needed

               

              And the text flows right into the 2nd threaded text frame perfectly, styles and all!

               

              Thanks so much for the snippet!

              • 4. Re: [JS] create textframe programatically?
                lhanneman Level 1

                Actually its looking like the styles from one text frame aren't copied into another if you just assign the contents:

                 

                myTextFrame.contents = tempTextFrame.contents;

                 

                Is there a way to copy EVERYTHING from tempTextFrame into myTextFrame (including character/paragraph styles, etc)??

                 

                Thanks!

                • 5. Re: [JS] create textframe programatically?
                  [Jongware] Most Valuable Participant
                  function(){return A.apply(null,[this].concat($A(arguments)))}

                  Actually its looking like the styles from one text frame aren't copied into another if you just assign the contents.

                   

                  Yeah, I thought so much ... It's not a problem if you were using your default style anyway, because it'll be assigned automatically to new text frames. But anything else is discarded.

                   

                  To copy/move text with all the trimmings, use the 'move' command. From memory:

                   

                  tempTextFrame.texts[0].move (LocationOptions.AT_BEGINNING, myTextFrame.texts[0]);

                   

                  (note: it's the reverse of assigning, you must move what you have to the new location)

                  • 6. Re: [JS] create textframe programatically?
                    lhanneman Level 1

                    awesome, thanks for the quick reply. I'll give it a shot and let you know how it goes!

                     

                    Thanks!

                    • 7. Re: [JS] create textframe programatically?
                      [Jongware] Most Valuable Participant

                      By the way #1: if you are already creating a temp frame, why not simply create the correct one right away? Does it have to do with your threading the correct frames together?

                       

                      By the way #2: this also ought to work (untested and all):

                       

                      tempTextFrame.nextTextFrame = myTextFrame;

                      tempTextFrame.remove();

                       

                      -- can you figure out what it does?

                      • 8. Re: [JS] create textframe programatically?
                        lhanneman Level 1

                        #1 - yes its because of the threaded text frames. With single text frames, if i need to update text/styles I basically just clear out the contents, and rebuild the content directly into the text frame. This became difficult with threaded frames (it never seemed to work right), so I got the idea to just build up one text frame, and drop the contents into the "start" frame and let it flow automatically.

                         

                        #2 - I like that! very clever, I may give that a shot as well.

                        • 9. Re: [JS] create textframe programatically?
                          lhanneman Level 1

                          I've got one more question, whenever I am building up the contents of  a text frame (even in my temp frame) if I add more than one paragraph style, it seems like I always lose my font size value.

                           

                          I've tried setting myParagraphStyle.pointsize = 6 and also myTextFrame.insertionPoints[-1].pointSize = 6 right before adding the content, and it always seems to default to 12 pt anyway? any thoughts on this?

                           

                          Not sure this matters, but for what its worth, if I click on any random spot on the document it resets to "12 pt" for the size, so it seems like that is the default which is is resorting to no matter what. But if i only use one paragraph style it seems to work fine and keeps the 6 pt font. Is there something I need to do differently than this to retain font size?

                           

                               //CREATE THE NEW PARAGRAPH STYLE:

                              var myParagraphStyle1 = Doc2100125263.paragraphStyles.item('myParagraphStyle1');

                           

                              try{

                                  //If it already exists, remove and re-add it:

                                  var myParagraphName = myParagraphStyle1.name;

                                  myParagraphStyle1.remove();

                                  myParagraphStyle1 = Doc2100125263.paragraphStyles.add({name:'myParagraphStyle1'});

                              }catch(myError){

                                  myParagraphStyle1 = Doc2100125263.paragraphStyles.add({name:'myParagraphStyle1'});

                              }

                           

                              myParagraphStyle1.justification = Justification.LEFT_ALIGN;

                           

                              //APPLY PARAGRAPH STYLE:

                              tempFrame.insertionPoints[-1].appliedParagraphStyle = myParagraphStyle1;