6 Replies Latest reply on Jan 20, 2014 4:18 AM by egikas

    How to place an asset from the library to all pages via JavaScript?

    egikas

      Hello, I'm newbie in scripting that's why I need a help from one who knows. I need a javascript to place an asset from the library to all pages of my document.

      I've tried to do it but all i have :

       

      var myLibPath = (File("C:/Library.indl"));

      var myLib = app.open(myLibPath);

      var libItem = app.libraries[0].assets[0].placeAsset(app.documents[0]);

       

      this places asset to the selected page only, may be someone can tell me how can it works for all pages.

       

      Thanks!

        • 1. Re: How to place an asset from the library to all pages via JavaScript?
          Mi_D Level 3

          HI egikas,

           

          try below code

           

          var myDoc = app.activeDocument;

          var myPages = myDoc.pages;

          var libraryFilePath=new File("C:\\Library.indl");

          app.open(File(libraryFilePath));

          myAsset = app.libraries[0].assets.item("test");// change name ase per ur requirement

          for (var pg =0; myPages.length>pg; pg++)

          {

           

                      var myObj = myAsset.placeAsset(app.documents.item(0))[0]; //first place library in first page

                      myObj.move (myPages[pg]);

                      myObj.move ( [15, 15] );//change ur x.y coodinate as per ur requirement

          }

           

          Mi_D

          • 2. Re: How to place an asset from the library to all pages via JavaScript?
            egikas Level 1

            Thank you, Mi_D. that's what I need! But there's one problem, my document has facing pages, so your script place asset only at right or left pages. And I want it to place on the both sides of facing pages.

             

            May be you have a good idea how to solve it in a simple way:) ? I'd be very appreciated.

            • 3. Re: How to place an asset from the library to all pages via JavaScript?
              Mi_D Level 3

              I post u two mathod

               

              in first check right hand page or left head page

              //////////////////////////////////////////////////////////////////////

              var myDoc = app.activeDocument;

              var myPages = myDoc.pages;

              var libraryFilePath=new File("C:\\Library.indl");

              app.open(File(libraryFilePath));

              myAsset = app.libraries[0].assets.item("test");// change name ase per ur requirement

              for (var pg =0; myPages.length>pg; pg++)

              {

              if (myPages[pg].side==PageSideOptions.RIGHT_HAND){//check right firstpage

                       if (pg ==0){//check right firstpage

                          var myObj = myAsset.placeAsset(app.documents.item(0))[0]; //first place library in first page

                          myObj.move (myPages[pg]);

                          myObj.move ( [12, 15] );//change ur x.y coodinate as per ur requirement

                          }

                      else{

                          var myObj = myAsset.placeAsset(app.documents.item(0))[0]; //first place library in first page

                          myObj.move (myPages[pg]);

                          myObj.move ( [222, 15] )

                          }

                  }

              if(myPages[pg].side==PageSideOptions.LEFT_HAND){

                              var myObj = myAsset.placeAsset(app.documents.item(0))[0]; //first place library in first page

                          myObj.move (myPages[pg]);

                          myObj.move ( [12, 15] );//change ur x.y coodinate as per ur requirement

                  }

              }

               

              ///////////////////////////////////////////////////////////////////////////

               

               

              2nd mathod change page preferences spread to page

               

              ///////////////////////////////////////

               

              var myDoc = app.activeDocument;

              var myPages = myDoc.pages;

              myDoc.viewPreferences.rulerOrigin=RulerOrigin.pageOrigin;  

              myDoc.viewPreferences.horizontalMeasurementUnits=MeasurementUnits.MILLIMETERS ;

              myDoc.viewPreferences.verticalMeasurementUnits=MeasurementUnits.MILLIMETERS;

              myDoc.zeroPoint=[0,0];

              var libraryFilePath=new File("C:\\Library.indl");

              app.open(File(libraryFilePath));

              myAsset = app.libraries[0].assets.item("test");// change name ase per ur requirement

              for (var pg =0; myPages.length>pg; pg++)

              {

               

                          var myObj = myAsset.placeAsset(app.documents.item(0))[0]; //first place library in first page

                          myObj.move (myPages[pg]);

                          myObj.move ( [15, 15] );//change ur x.y coodinate as per ur requirement

              }

               

              //////////////////////////////////////////

               

               

              Mi_D

              • 4. Re: How to place an asset from the library to all pages via JavaScript?
                egikas Level 1

                Hi, Mi_D. That's rock. Thank you a lot! Works great. You have solved my problems:)

                • 5. Re: How to place an asset from the library to all pages via JavaScript?
                  Mi_D Level 3

                  if pervious my post help u click yes in bottom

                   

                  Was this helpful? Yes   No

                  • 6. Re: How to place an asset from the library to all pages via JavaScript?
                    egikas Level 1

                    hmm I clicked "Yes" evry time u answer me, so i'll do it once again