8 Replies Latest reply: Mar 26, 2012 6:23 AM by Laubender RSS

    copy/paste testframe from a master page ?

    Boitaumail

      Hi,

       

      A layout has been made by a designer. He uses a lot of properties (rotation, odd corner, gradient ...) for each of his elements (textFrame).

      From this layout, I have to create an automatic layout from his work (I'm using InDesign CS5). That mean I should recreate the properties in Javascript for several elements.

       

      I was wondering if it would be better to use a Master page with all of those elements in it and then using of the "copy/paste" to call my elements.

      According to you, is it a good way to proceed ?

       

       

      By the way, I have some problems to select a textframe in my master page...

      Here is what I use to access to my textFrame element :

       

      myDocument.masterSpreads.item("A-MyName").pages.item(0).textFrames.item("MyBlocName")
      

       

      What's wrong ?

       

       

       

       

      Thank you very much.

        • 1. Re: copy/paste testframe from a master page ?
          Laubender MVP

          How did you get the name of the text frame? I mean how do you know that your text frame you want to duplicate has the name "MyBlocName"?

           

          What would you do if several text frames have an identical name?


          You could loop through all page items on your master page, single out the text frames and looking for a name:

           

          //In case you gave a name by using the layers palette in the UI (CS5 and CS5.5):
          for(var n=0;n<myMasterPage.allPageItems.length;n++){
              if(myMasterPage.allPageItems[n].constructor.name == "TextFrame" && myMasterPage.allPageItems[n].name == "MyBlocName"){
                  //Do something
                  };
          };
          

           

          //In case you gave a name by using the labels palette in the UI (CS4, CS5 and CS5.5):
          for(var n=0;n<myMasterPage.allPageItems.length;n++){
              if(myMasterPage.allPageItems[n].constructor.name == "TextFrame" && myMasterPage.allPageItems[n].label == "MyBlocName"){
                  //Do something
                  };
          };
          

           

          Uwe

          • 2. Re: copy/paste testframe from a master page ?
            Boitaumail Community Member

            I use the script label to name my textframes.

            I define the names myself in a MasterPage especially reserved to contain those textFrames ready to be copied and pasted, so I suppose there won't be two identical names !

             

            There's no other way to proceed than with an instruction FOR ?

            Something direct like : myMasterPage.item("MyBlocName").copy()  ?

            (I don't really know how copy/paste function works in Indesign Javascript Scripting...)

             

            By the way, I tried to use the exemple like this (to see if it works) :

             

            myMasterPage = myDocument.masterSpreads.item("C-test"); //My Master is called "test" and its letter is "C".
            for(var n=0;n<myMasterPage.allPageItems.length;n++){
                if(myMasterPage.allPageItems[n].constructor.name == "TextFrame" && myMasterPage.allPageItems[n].label == "MyBlocName"){
                    myMasterPage.allPageItems[n].contents = "TEST";        
                    };
            };
            

             

            But nothing happen... (no error).

            A problem with myMesterPage ?

             

             

             

            Thank you very much for your help !

            • 3. Re: copy/paste testframe from a master page ?
              [Jongware] MVP

              (Only as an aside:

               

              This

               

              if(myMasterPage.allPageItems[n].constructor.name == ..

               

              does not work. If you access your page items by asking for "pageItem", you will get the name "pageItem" -- not "TextFrame".)

              • 4. Re: copy/paste testframe from a master page ?
                Laubender MVP

                @Jongware –

                hm, strange, just tested and it does work. (InDesign CS5.5 v7.5.2 on Mac OSX 10.6.8)

                 

                Alternatively one could use "instanceof TextFrame" instead of "constructor.name == "TextFrame"".

                 


                @Boitaumail – could you provide a reduced sample of your InDesign file (IDML) we can download somewhere?
                I just tested my code with the script label and all went right…

                 

                Uwe

                • 5. Re: copy/paste testframe from a master page ?
                  Boitaumail Community Member

                  You will find a sample test here, where my code doesn't work.

                   

                  Thank you very much for your help !

                  • 6. Re: copy/paste testframe from a master page ?
                    Laubender MVP

                    @Boitaumail – downloaded your file and tested (InDesign CS5, CS5.5). No problem on my side with the following code:

                     

                    var myPage = app.documents[0].masterSpreads.itemByName("B-right").pages[0];
                    var allPageItemsMyPage = myPage.allPageItems;
                    
                    for(var n=0;n<allPageItemsMyPage.length;n++){
                    
                        if(allPageItemsMyPage[n].constructor.name == "TextFrame" && allPageItemsMyPage[n].label == "COLLECTION"){
                            $.writeln("HIT TARGET");
                            /*
                                Do something useful here
                            */
                            //After that we could break the for-loop; 
                            //there should be only ONE instance of text frames with label "COLLECTION" on the page. That will speed up operation a bit.
                            break;
                            };
                    
                        };
                    

                     

                    Uwe

                    • 7. Re: copy/paste testframe from a master page ?
                      Boitaumail Community Member

                      Thank you very much, your way to proceed works.

                       

                      For my part, I test this :

                       

                      var myPage = app.documents[0].masterSpreads.itemByName("B-right").pages[0];
                      var allPageItemsMyPage = myPage.allPageItems;
                      myPage.textFrames.item("test").contents = "New content inside !";
                      

                       

                      Which only works (for CS5) when the "label" is define like this :

                      http://www.wc-blog.com/data/test01.gif

                       

                      The probleme is... I expect my script works on several plateform (CS4, CS5 and including MAC OS...).

                       

                      @Laubender : Do you think your code is OK on different plateform ?

                       

                       

                      Thank you again.

                      • 8. Re: copy/paste testframe from a master page ?
                        Laubender MVP

                        @Boitaumail – My code will work for InDesign CS4, CS5 and CS5.5. I cannot test for  Windows OS, but it should work there as well.

                         

                        The difference to my approach is: if you change the object's name in the layers palette, you define the "name" property of your text frame (CS5) which initially is "undefined". What you see in the layers palette of the UI after adding a new text frame is something more generic and you should not mix that up with "name" or "label". The "label" property is a totally different beast.

                         

                        Apart from defining "label" and "name" properties you could use the insertLabel() and extractLabel() methods to "pin point" a text frame. Downside (more often a bonus if you mind user interaction): you cannot see the contents of an inserted label in the UI and only define it and read it out by scripting.


                        There is a (long) thread about the differences between CS4 and CS5. See:

                         

                        With CS5, some things have changed

                         

                        http://forums.adobe.com/message/2728529#2728529

                         

                        Uwe