8 Replies Latest reply on Feb 11, 2010 6:57 AM by Andreas Jansson

    [CS3 JS] Copying contents with styles

    Andreas Jansson Level 2

      What's the best way to copy contents and keeping styles (and other attributes)?

      In this case it's the contents of a table cell I would like to get into a text frame, with its style information untouched.

       

      (Not using the clipboard.)

       

      Best regards,

      Andreas Jansson

        • 1. Re: [CS3 JS] Copying contents with styles
          Harbs. Level 6

          text.duplicate()

           

          (CS3 and CS4)

           

          Harbs

          • 2. Re: [CS3 JS] Copying contents with styles
            Andreas Jansson Level 2

            Perfect, thanks!

            • 3. Re: [CS3 JS] Copying contents with styles
              Andreas Jansson Level 2

              Now I'm tearing my hair with this one (again)...

               

              Creating a new script for another purpose, I can't get it working. There is no duplicate method on the Text object according to the error message.

               

              Just create a blank page and run this to reproduce:

               

              var myText = app.activeDocument.pages[0].textFrames.add()
              
              var test = myText.texts[0].constructor.name == 'Text'
              test + '\n' + myText.texts[0].reflect.methods;
              
              

               

              (For me) it returns the following:

               

              Result: true
              createOutlines,markup,remove,convertToTable,applyStyle,changecase,move,clearOverrides,reco mpose,search,place,convertToNote,

              exportFile,convertBulletsAndNumberingToText,applyBullets,applyNumbers,removeBulletsAndNumb ers,select,toSource,getElements,

              toSpecifier,==,===

               

              Where is the duplicate() method? Is something more required for a Text to be "duplicatable"?

              My old scripts, from when I was helped out here before, still works as far as I know, so I know it's doable.

               

              Best regards,

              Andreas

              • 4. Re: [CS3 JS] Copying contents with styles
                [Jongware] Most Valuable Participant

                According to the ESTK Help, there is:

                 

                Text duplicate (to: LocationOptions[, reference: {Text | Story | Cell | Row | Column | Table | PageItem} ])
                Duplicates the text in the specified location.

                ParameterTypeDescription
                toLocationOptions:
                LocationOptions.BEFORE
                LocationOptions.AFTER
                LocationOptions.AT_END
                LocationOptions.AT_BEGINNING
                LocationOptions.UNKNOWN
                The location relative to the reference object or within the containing object.
                referenceText
                Story
                Cell
                Row
                Column
                Table
                PageItem
                The reference object. Note: Required when the to parameter specifies before or after. Can accept: Text, Story, Cell, Row, Column, Table or PageItem. (Optional)

                1 person found this helpful
                • 5. Re: [CS3 JS] Copying contents with styles
                  Andreas Jansson Level 2

                  Yes, but I get an error trying to use duplicate (no matter the parameters I use), since the duplicate method is reported not to be there. And checking with the test code, see above, there is really no duplicate method on the object, which is also reported to be a Text object.

                  I can't see how this can be...

                   

                  Trying to call duplicate on the text object of the textFrame results in this error:

                  myText.texts[0].duplicate is not a function

                  • 6. Re: [CS3 JS] Copying contents with styles
                    [Jongware] Most Valuable Participant

                    myText a bad type?

                    This totally works:

                     

                    doc = app.documents.add();
                    frame = doc.textFrames.add({geometricBounds:[0,0,"100mm","100mm"]});
                    frame.contents = TextFrameContents.PLACEHOLDER_TEXT;
                    frame.geometricBounds = [0,0,"200mm","100mm"];
                    frame.texts[0].duplicate (LocationOptions.AFTER, frame.texts[0]);
                    

                     

                    (Press Undo to see it in reverse.)

                    1 person found this helpful
                    • 7. Re: [CS3 JS] Copying contents with styles
                      Andreas Jansson Level 2

                      Ah... Testing your code I get the same error (see image) which let me know that there's something faulty in InDesign here...

                      Closing down InDesign and opening it... and now your code runs fine!

                      I'll close down for today now, and continue with my own code tomorrow. Hopefully InDesign is feeling sounder towmorrow.

                      Beating ones head into strange errors like this is a pain.

                       

                      Thank you so much for letting me know that the code was not the error by trying to reproduce it at your place!

                      Andreas

                      • 8. Re: [CS3 JS] Copying contents with styles
                        Andreas Jansson Level 2

                        Just for the record: Another script had set my app.scriptPreferences.version to CS3, that's why some things did not work until restart that day...