4 Replies Latest reply on Apr 8, 2014 8:32 AM by T. Baze

    Need help placing images on separate layers

    T. Baze

      I'm a novice to Javascript but I've been trying to learn. I have a project where i need to place muliple images from a folder onto separate layers (same page) in a template. I tried to modify "ImageCatalogue" script within InDesign to disastrous results. On another thread someone was kind enough to give me a short script "Objects to Layers" written by Kasyan Servetsky. This was very helpful but not exact to my needs so now I'm starting from scratch. I've been playing with the following code trying to get it to work. It doesn't and I'm getting frustrated. I keep changing this line but it keeps coming back as "undefined":

       

      myImage.place(newLayer);

       

      Can someone please point me in the right direction? Is my coding too simple? The end comments are my wish list of add-ons should I ever get these layers working.

       

      Thanks for your help!

       

       

      // script to place images on separate layers

       

       

      Main();

       

       

      function Main(){

                // get path to folder, ask to select which folder

                var myFolder = Folder.selectDialog("Select folder containing images");

                // use open document

                doc = app.activeDocument;

                // if folder is empty, tell user to select another folder

                if(myFolder.length == 0){

                          selectDiaglog("Folder empty, select new folder");

                }

                else{

       

                // select each file in myFolder and place on new layer in active document

                          var myImage = myFolder.item;

                          newLayer = doc.layers.add(+1);

                          myImage.place(newLayer);

                }

                alert("Done")

      }

      // rotate image 90 CCW

      // place image at x-6.41, y9.9955 on page

      // rename layer as filename

        • 1. Re: Need help placing images on separate layers
          Dave Saunders Level 4

          Have you looked at the ESTK Object Model Viewer for help on the various properties and methods you're using?

           

          I'm surprised that your script gets past:

           

          myImage = myFolder.item

           

          because folders don't have an item property. But indeed, I've reproduced your result.

           

          Your problem is not that newLayer doesn't exist -- it does, although the "+1" parameter to the add method makes no sense. Your problem is that myImage is not defined because myFolder.item doesn't work.

           

          To walk through the files in a folder you need to use getFiles() to get an array of files and then cycle through that.

           

          It's also worth noting that a Folder object doesn't have a length property, so your test for an empty folder isn't working. myFolder.length is undefined, which isn't equal to 0, so that's how you're getting past there.

           

          The Object Model Viewer can be found on ESTK's Help menu.

           

          Dave

          • 2. Re: Need help placing images on separate layers
            T. Baze Level 1

            I did look at ESTK's Object Model but I apparently don't know how to use it because it was no help at all. I just used the debugger. Your tips did make sense and I'm going to go back and wrangle with my syntax some more. I'll post my progress.

             

            I was a programmer 25 years ago so I get the logic but I've been away from coding so long the language is all new to me. 

             

            Thanks for your time and advice!

            • 3. Re: Need help placing images on separate layers
              Jump_Over Level 5

              Hi,

               

              Main();
              
              function Main(){
                   // get path to folder, ask to select which folder
                   var myFolder = Folder.selectDialog("Select folder containing images");
                   // use open document
                   doc = app.activeDocument;
                   // if folder is empty, exit
                   if(!myFolder){
                        alert("no Folder selected, I am quitting"); exit();
                   }
              
                   // place each file in myFolder on new layer in active document
                   // files are filtered by their extension
                   var 
                        myImages = myFolder.getFiles(/\.jpg$|\.tiff$|\.pict$|\.bmp$|\.eps$|\.pdf$/i),
                        len = myImages.length, currImg,
                        mAngle = 270,
                        mX = -6.41,
                        mY = 9.9955;
              
                        while (len-->0) {
                             currImg = doc.pages[0].place(
                                  myImages[len],     // current file
                                  [0,0],     // initial place point
                                  doc.layers.add({name: myImages[len].displayName}),          // rename layer as filename
                                  false,
                                  false,
                                  {absoluteRotationAngle: mAngle}          // rotate image 90 CCW
                                  )[0].parent;
                             currImg.fit(FitOptions.FRAME_TO_CONTENT);     //  fix frame to content
                             currImg.move([mX,mY]);          // move image at x-6.41, y9.9955 on page
                             }
              
                        alert("Done")
                   }
              

               

              Each image in the same point placed?

               

              Good help is served here:

              http://www.jongware.com/idjshelp.html

               

              Each object has properties and methods.

              Used above: folder.getFiles(), page.place(), rectangle.fit() and rectangle.move() methods.

               

              Jarek

              • 4. Re: Need help placing images on separate layers
                T. Baze Level 1

                Jarek,

                 

                You are a lifesaver! After countless days of text books and trials this works perfectly. Each image was placed in the same point on its own layer. I had to tweak the placement a little as I was off with my coordinates but I get it now. The code is simple yet powerful and easy to understand. A great learning tool to keep me motivated to improve my coding. The link was helpful as well and much appreciated.

                 

                Thank you!!