17 Replies Latest reply on Sep 16, 2014 11:54 AM by Laubender

    Placing library item into page

    Math Kay Level 1

      This script for placing library item into page and applying object style to the item. In line 16, script shows that "assets" property does not support. Please resolve this.

      if (app.documents.length==0){
          alert("Please open the Document");
          exit();
          }
      if (app.libraries.length==0){
          alert("Please open the Library");
          exit();
          }
      app.findTextPreferences=app.changeTextPreferences=null;
      app.findTextPreferences.appliedParagraphStyle="MN";
      var myfound=app.activeDocument.findText();
      alert(myfound.length);
      
      for(i=0; i<myfound.length; i++){
          
      var mylib=app.libraries.assets.itemByName("Box").placeAsset(myfound[i].insertionPoints[0]);
      var mymove=myfound[i].move(LocationOptions.atBeginning, mylib.insertionPoints[0])
      mylib.appliedObjectStyle="MN_Obj";
      }
      
      
        • 1. Re: Placing library item into page
          Laubender Adobe Community Professional & MVP

          To make that work, you have to address an open library (line 16).

          app.libraries describes the collection of all open libraries, not a distinct one.

          If only one library is open you could address the one with its index:

          app.libraries[0]


          If you already know the name of the library or better, the exact path of the library, you can:

          1. Make sure to open it

          2. Call it by its name


          Let's assume you know the name and you know that the asset you want to place is stored inside, you could do it this way:


          app.libraries.itemByName("TheNameOfTheLibrary").assets.itemByName("Box").placeAsset(
          /*Wherever you want*/
          );
          


          Now, what will this line return?

          A library object? Hardly. So be cautious with using a variable name like "mylib".

          According to documentation it is returning an Array, and we can assume, that in this array is the object that is placed out of the library. Your asset stored in the library which goes by the name of "Box". Whatever that is.


          If you want to find out, you can use the following, more descriptive variable: "myPlacedAsset". That sounds more neutral.

           

          var myPlacedAsset = app.libraries.itemByName("TheNameOfTheLibrary").assets.itemByName("Box").placeAsset(
          /*Wherever you want*/
          );
          


          Using the ESTK (ExtendScript Toolkit) together with $.writeln() can write the true nature of the placed asset to your JavaScript Console:


          $.writeln(myPlacedAsset.constructor.name);


          is informing you what kind of object "myPlacedAsset" really is. If indeed it is an Array, you could look for its length. If its length is "1", you get to the "heart" of your placed object by:

           

          $.writeln(myPlacedAsset[0].constructor.name);


          Or by that more inquisitory question:

          $.writeln(myPlacedAsset[0].getElements()[0].constructor.name);

           

          If your array is of a greater length, you have to loop through this array and question every single object!

           

          Before testing, exchange my comment "/*Wherever you want*/" with the code you like to use: Obviously the first insertion point of your found text. Hold on! This will backfire to you, because every time you place one instance of the asset you will add a character to your found text and shift the original insertionPoint. Better loop from back to forth or do the search from back to forth.

           

          Adobe InDesign CS6 (8.0) Object Model JS: Text


          Please, try that and report back…

           

          (Isn't noone reading the documentation these days?)

           

          Indesign JavaScript Help


          Adobe InDesign CS6 (8.0) Object Model JS: Table of Contents, Libraries Suite

          Adobe InDesign CS6 (8.0) Object Model JS: Library

          Adobe InDesign CS6 (8.0) Object Model JS: Asset

          Adobe InDesign CS6 (8.0) Object Model JS: Asset

           

          Uwe

          • 2. Re: Placing library item into page
            Jump_Over Level 5

            Hi,

             

            I would add two remarks to Uwe's :

            1. You cant apply objectStyle using a string (name)

                so use:

             

            mylib[0].appliedObjectStyle = app.activeDocument.objectStyles.item("MN_Obj");
            

             

            2. Notice that to move myfound[i] text means moving whole paragraphs (including its "enter signs") so you are deleting these paragraphs and joining the next paragraph of the story, in fact.

            It depends on your text structure but it can be an serious change.


            Jarek

            PS. Moreover, as a result of text.move() method ==> backward loop is absolutely necessary

            • 3. Re: Placing library item into page
              Math Kay Level 1

              Hi Laubender ,

               

              Line 16 resolved. it works fine. but "Box" is placing for first occurrence only but there are totally 6 occurrences.

               

              and in line 17, what is wrong with it?

              • 4. Re: Placing library item into page
                Math Kay Level 1

                Hi Jump,

                 

                Is this reverse loop correct?

                 

                 

                 

                 

                for(i=0; i<myfound.length-1; i--){

                   

                var myPlacedAsset=app.libraries.item(0).assets.itemByName("Box").placeAsset(myfound[i].insert ionPoints[0]);

                myPlacedAsset.appliedObjectStyle=app.activeDocument.objectStyles.item("MN_Obj"); 

                 

                var mymove=myfound[i].move(LocationOptions.atBeginning,myPlacedAsset.insertionPoints[0])

                }

                • 5. Re: Re: Placing library item into page
                  Laubender Adobe Community Professional & MVP

                  Did you loop in reverse?
                  Or (alternatively) did you do the search backwards?

                   

                  Doing the search backwards is done with changing line 11:

                   

                  var myfound=app.activeDocument.findText(true);

                   

                  findText() takes one argument, true for reverse order, false for not resverse order (that is default) which means, if you do not provide the argument, the search is not in reverse order.

                   

                  Since i do not know, what you like to accomplish with line 17, I cannot recommend anything.

                   

                  Could you explain the whole concept of your script in procedural steps using plain English?

                  And: What exactly is "Box" ?


                  Also: Provide some screenshots and post them inline here to visualize what you want as a result.

                  A before/after comparison…

                   

                  Uwe

                  • 6. Re: Re: Placing library item into page
                    Jump_Over Level 5

                    Is this reverse loop correct?

                    for(i=0; i<myfound.length-1; i--){

                        ....

                    }

                     

                    Nope. Reverse means frome the last to first:

                    for (i = myfound.length - 1; i >=0; i--) {
                        ...
                    }
                    

                     

                    Jarek

                    • 7. Re: Placing library item into page
                      Math Kay Level 1

                      Hi Lubander,

                       

                      Objectives of this Script:

                      1. Placing text frame "Box" from library into the text wherever MN style has been applied

                      2. Moving "MN" Style Text into Box"

                      3. Applying Object stye "MN_Obj" to "Box" in order to fix the box in Stub column of book

                       

                       

                       

                      Capture.JPG

                      • 8. Re: Placing library item into page
                        Math Kay Level 1

                        error in line 17,

                         

                        var mymove=myfound[i].move(LocationOptions.atBeginning,myPlacedAsset.insertionPoints[0])

                        • 9. Re: Placing library item into page
                          Laubender Adobe Community Professional & MVP

                          Thank you!

                           

                          Maybe I made myself not clear enough. I did not want to refer to the scripting side of the problem. Just the layout situation. Could you show us an InDesign page before and after (just like you would accomplish your task without scripting)? Together with the open library?

                           

                          Uwe

                          • 10. Re: Placing library item into page
                            Math Kay Level 1

                            Hi Lubander,

                             

                            Please find the screenshots of before and after running scripts

                            Before:

                            Before.JPG

                            AFTER:


                            After.JPG

                            • 11. Re: Placing library item into page
                              Laubender Adobe Community Professional & MVP

                              Now it's a bit more clear to me. But not much ;-)
                              Could you build a result without scripting and showing that?

                               

                              Right now I can see only an inserted frame in the text (your second screenshot). What is this frame supposed to hold as contents and how is the design of an orderly finished example?

                               

                              Uwe

                              • 12. Re: Placing library item into page
                                Jump_Over Level 5

                                Hi,

                                 

                                Notice that your original code needed 5-6 changes to run with success. Did you modify it?

                                Place your current code to compare as well, pls.

                                 

                                Jarek

                                • 13. Re: Placing library item into page
                                  Math Kay Level 1

                                  Hi,

                                  Please find the finished design that suppose to be. "MN" text should be moved into text.

                                   

                                  Sample Design.JPG

                                  • 14. Re: Re: Placing library item into page
                                    Math Kay Level 1

                                    This is modified current script.

                                     

                                     

                                    if (app.documents.length==0){
                                        File.openDialog("Open Indesign Document");
                                        exit();
                                        }
                                    if (app.libraries.length==0){
                                        File.openDialog(" Open the Library");
                                        exit();
                                        }
                                    app.findTextPreferences=app.changeTextPreferences=null;
                                    app.findTextPreferences.appliedParagraphStyle="MN";
                                    var myfound=app.activeDocument.findText(true);
                                    //~ alert(myfound.length);
                                    
                                    for (i = myfound.length - 1; i >=0; i--){
                                    var myPlacedAsset=app.libraries.item(0).assets.itemByName("Box").placeAsset(myfound[i].insertionPoints[0]);
                                    myPlacedAsset.appliedObjectStyle=app.activeDocument.objectStyles.item("MN_Obj");  
                                    var mymove=myfound[i].move(LocationOptions.atBeginning,myPlacedAsset.insertionPoints[0])
                                    }
                                    
                                    
                                    • 15. Re: Re: Re: Placing library item into page
                                      Jump_Over Level 5

                                      Hi,

                                      line 15:

                                       

                                      var myPlacedAsset=app.libraries.item(0).assets.itemByName("Box").placeAsset(myfound[i].insertionPoints[0])[0];
                                      
                                      

                                       

                                      placeAsset returns an array (watch Uwe's remarks)

                                       

                                      Jarek

                                      • 16. Re: Re: Re: Placing library item into page
                                        Math Kay Level 1

                                        Hi Thank you so much for your patience. Now script works fine. I have to see the object styles only for Box postion in book.

                                        • 17. Re: Placing library item into page
                                          Laubender Adobe Community Professional & MVP

                                          So your asset named "Box" is a single text frame?

                                           

                                          That means your variable myPlacedAsset is an array with a length of 1.

                                          myPlacedAsset[0] actually is the text frame then.

                                          (I explained that before)

                                           

                                          How would you define its anchoring position relative to the insertion point of the anchor?

                                          I think, the best way is to assign a predefined object style to the frame.

                                           

                                          Uwe