6 Replies Latest reply on Nov 14, 2009 3:37 PM by RatsRCool

    New to scripting...


      Help! A client has just sprung it on me that I need to "script" a single picture box on an InDesign 4 page to place and proportionately crop a picture from a specified folder when run. I need learn more about scripting in general, but for now any "shortcut" would be greatly appreciated. The turorial materials are starting to look a little complicated for the short term. It must be Javascript, because, I'm told, it must run on a PC as well as a mac...


      Thanks in advance

        • 1. Re: New to scripting...
          [Jongware] Most Valuable Participant

          Can you work with this one I wrote last month, adjusting where necessary? The original placed over 200 images, 4 on each page, into predetermined positions. I just put the entire list of file names in the script, but it is possible to read them "live" out of a folder (see the help on "Folder.getFiles").

          The positions are in millimeters -- you should adjust them for anoother place and/or units. Alternatively, you could draw placeholder frames and label these.


          The script also adjusts the ruler origin -- my document had facing pages, and while the script can be adjusted to take that into account, this was the easier option


          var allPictures = [
          var positions = [
           [17.75, 18.5, 105.4, 75.5],
           [17.75, 76.25, 105.4, 133.745],
           [106.517, 18.5, 203, 75.5],
           [106.517, 76.25, 203, 133.745]
          app.activeDocument.viewPreferences = RulerOrigin.PAGE_ORIGIN;
          for (a = 0; a<allPictures.length; a++)
           rect = app.activeWindow.page[Math.floor(a/4)].place (File("/some folder/"+allPictures[a]));
           rect[0].parent.geometricBounds = positions[a & 3];
          • 2. Re: New to scripting...
            thestudioguy Level 1

            Thank you for your reply. I will try to make sense of and alterations to what you have sent.


            Already makes more sense than before I did the tutorial stuff...

            • 3. Re: New to scripting...
              [Jongware] Most Valuable Participant

              The brunt of the work is done in the for .. loop. Let me expand a bit on the salient points.


              The Place method is called with a "Page" as its 'source', so InDesign will automatically place the requested file onto that page. You can also call 'place' from a number of other sources, such as a frame itself, or your document. In this case, it was easiest to have the image placed immediately onto the right page.


              The Place method does not return a single image (although you specify one file only). It appears to return an array of objects instead. Well, from within the interface you can place multiple files ... (and I don't know how that would work out using a script). Initially, I stored this new object into a variable 'rect' but, as it turned out, I need to use item number 1 from the returned array -- rect[0].


              By default, a newly imported image is dropped on position (0,0) of the reference point -- in this case, the top left of the page -- and with its original size. So, next step is to move it into position by changing its geometricBounds. This is a simple positioning array, unnecessary made difficult by the fact that it holds the coordinates in the order [topy leftx bottomy rightx]. You don't have control over its width and height.

              Since the positions were predetermined for my document, I could just create four placeholder frames and copy their upper left and bottom right coordinates. Because I needed four, I stored them in the 'positions' array (which is an array of arrays).


              What gets moved is the frame of the image only. Imagine moving an image in the interface, where the image itself stays into place -- that's what happens here. So the next two commands are to first fit the image to the frame (just like in the interface, using the Fit Content Proportionally to Frame command), which leaves a small part at the bottom of the frame 'empty', then a Fit Frame to Content, to remove the extraneous frame space.

              • 4. Re: New to scripting...a new question here about adding photos, please!

                Hi! I posted my question in the InDesign forum, and I was looking through this forum and may have found the answer or something close in your response here.


                Here's what I asked:


                "I need to automate inserting photos into an InDesign document. I'm creating a simple document, multiple pages, and would like to insert about 400 photos. One the left side of the pages I'd like to put a column of three to five photos (already cropped in Photoshop) and leave the rest of the page blank (our un-techy uncle is going to handwrite something next to each photo) .


                How can I do this without individually inserting each photo and shrinking it to fit? (If this was Word, I'd write/record a macro ...)"


                If your script will work, then I will happily use it!! I'd just like a little guidance on how to do that. I believe I paste it into the Applescript window, name it, and then how do I actually use it?


                THANK YOU!!


                (This forum is very cool!)

                • 5. Re: New to scripting...a new question here about adding photos, please!
                  [Jongware] Most Valuable Participant

                  You are wrong there ... It's not Applescript but Javascript.


                  (Standard paragraph about how to use Javascript follows. Please stand by.)

                  Copy & paste into a plain text editor (the ESTK Editor that comes with InDesign is good; Mac's TextEdit is not, as it saves by default as RTF). Save into your Scripting folder as, ehm (make up a file name and insert here. Be sure to have it end with ".jsx" so ID will also recognize it as the Javascript it is). When saved correctly, it'll automatically pop up in your Scripts Panel. Double-click to run.


                  That said, do not double-click this script to run. This particular script reads filenames from the list at the top of the script and inserts each of them on one of four predefined positions in turn, on consecutive (pre-existing) pages. To have it work for your needs, it'll need some going-over.

                  • 6. Re: New to scripting...a new question here about adding photos, please!
                    RatsRCool Level 1

                    Thank you!


                    I am SO new to this but am a quick learner with a little direction. (I've also been reading a little more while I waited for a reply)


                    Is there a place to look for SIMPLE step by step to use your code? I have also been reading about RECORDING a script and can't seem to make that stick either.




                    For what I want to do is it better to use Automater?


                    (If these questions are too basic for this forum, please just tell me where to look.


                    Thanks again!