7 Replies Latest reply on Oct 11, 2011 3:53 AM by Maria964

    Help fine-tuning a file-placing script?

    ianmcarey Level 1

      Hello Smart Folks,

       

      About us: we are a small shop of (OS X Leopard, CS4) designers within an educational non-profit. We have been using a script which was helpfully uploaded by Olav Kvern in this forum, which we then made minor adjustments to, and which has been working very well for us--it searches a document and replaces tagged filenames with inline graphics placed from a selected folder.

       

      We've recently realized there's one situation which seems to trip up the script every time--when we have more than one file to be placed on a single line. For example, if the script comes across

       

      <<12345a.eps>> [tab] <<12345b.eps>> [para return]

       

      ... it will insert the first file on the line, but then stop the script and delete (yikes!) the remainder of the text on the line.

       

      Any suggestions on ways to address this would be appreciated. Script is below.

       

      Thanks,

       

      Ian Carey

       

       

      main();

      function main(){

      myFolder= Folder.selectDialog ("Choose a Folder");

      if(myFolder != null){

        myReplaceGraphics(myFolder);

      }

      }

      function myReplaceGraphics(myFolder){

      var myInlineGraphic, myX1, myY1, myX2, myY2, myFoundItem;

      var myDocument = app.documents.item(0);

      myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;

      myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;

      var myRegExp = "(?i)<<(.*)>>"

      var myDocument = app.documents.item(0);

      var myDoc=app.activeDocument;

      myDoc.selection = NothingEnum.nothing;            // deselect all objects to prevent replacing the color of an object that might be selected already

      myDoc.pageItemDefaults.strokeColor = "None";   // set document default stroke color to "None"

      myDoc.pageItemDefaults.fillColor = "None"   // set document default fill color to "None"

      //Clear the grep find/change preferences.

      app.findGrepPreferences = NothingEnum.nothing;

      app.changeGrepPreferences = NothingEnum.nothing;

      app.findGrepPreferences.findWhat = myRegExp;

      //Search the document.

      var myFoundItems = myDocument.findGrep(true);

      //Clear the find/change preferences after the search.

      app.findGrepPreferences = NothingEnum.nothing;

      app.changeGrepPreferences = NothingEnum.nothing;

      if(myFoundItems.length != 0){

        for(var myCounter = 0; myCounter < myFoundItems.length; myCounter ++){

         myFoundItem = myFoundItems[myCounter];

         myX1 = myFoundItem.characters.item(0).horizontalOffset;

         myY1 = myFoundItem.characters.item(0).baseline;

         //Change the following values to change the size of the frame.

         myX2 = myX1 + 72;

         myY2 = myY1 + 72;

         myFileName = myFoundItem.contents.replace(/<<(.*)>>/i, "$1");

         myFilePath = myFolder + "/" + myFileName;

         $.writeln(myFilePath);

         myFrame = myFoundItem.insertionPoints.item(0).rectangles.add();

         //Recompose the text after adding the inline frame.

         myFoundItem.parent.recompose();

         myFrame.geometricBounds = [myY1, myX1, myY2, myX2];

         try{

          myInlineGraphic = myFrame.place(File(myFilePath))[0];

          myFrame.fit(FitOptions.frameToContent);

         } catch(myError){}

        }

      }

      //Now replace the tags.

      app.findGrepPreferences.findWhat = myRegExp;

      app.changeGrepPreferences.changeTo = "";

      myDocument.changeGrep();

      //Clear the find/change preferences after the search.

      app.findGrepPreferences = NothingEnum.nothing;

      app.changeGrepPreferences = NothingEnum.nothing;

      }

        • 1. Re: Help fine-tuning a file-placing script?
          John Hawkinson Level 5

          Please post scripts using >> Java Syntax Highlighting in the advanced editor.

           

          Your problem appears to be that this regexp:

           

          var myRegExp = "(?i)<<(.*)>>"
          

           

          is greedy and matches everything from the first << to the last >>. (Also, the line is missing a semicolon).

           

          Try replacing it with:

           

          var myRegExp = "(?i)<<(.*?)>>"
          

           

          to turn off the greediness.

          • 2. Re: Help fine-tuning a file-placing script?
            ianmcarey Level 1

            Worked like a charm! Thank you very much.

            • 3. Re: Help fine-tuning a file-placing script?
              Maria964 Level 1

              I John,

               

              I have tried this script in Indesign CS5 and i does not work. I confess that I am not much of a scripter -- I always loose the batle -- but I really need something like this. Can yoy make it work in ID 5.5?

               

              Thank you

               

              Maria

              • 4. Re: Help fine-tuning a file-placing script?
                John Hawkinson Level 5

                Good morning, Maria:

                 

                CS5 or CS5.5?

                In any case, please detail the symptoms of the failure.

                 

                If you simply say, "does not work," we cannot help you. You will have to tell us more about how it does not work. In as much detail as possible.

                • 5. Re: Help fine-tuning a file-placing script?
                  Maria964 Level 1

                  Hi, John

                   

                  Thank you very much for your fast reply. First, let me apologize for making you loose time. I have tried this script yesterday and it did not work (always stoping at this line:  myX1 = myFoundItem.characters.item(0).horizontalOffset;

                   

                  But today it works!!!??? So, my problem is solved and you don't have to loose any more time again with this.

                   

                  One question: I know that there is a way to increase script compatibility in Indesig 5.5 (Windows) by placing scripts in certain folders named «Version X.XX». Can you point me to some more info about this (mainly the number for the version -- the X.XX -- and the place where to build the corresponding folder?

                   

                  Thank you again and have a very nice day.

                   

                  Maria

                  • 6. Re: Help fine-tuning a file-placing script?
                    John Hawkinson Level 5

                    Thank you very much for your fast reply. First, let me apologize for making you loose time. I have tried this script yesterday and it did not work (always stoping at this line:  myX1 = myFoundItem.characters.item(0).horizontalOffset;

                    Surely it also stopped with an error -- what is that error?

                     

                     

                    One question: I know that there is a way to increase script compatibility in Indesig 5.5 (Windows) by placing scripts in certain folders named «Version X.XX». Can you point me to some more info about this (mainly the number for the version -- the X.XX -- and the place where to build the corresponding folder?

                    I would not recommend this method, but I can't articulate a good reason for that.

                    Anyhow, see "Script Versioning" on pp.12-13 of the InDesign CS5 Scripting Guide: JavaScript, specificall both sections labelled Interpretation.

                    • 7. Re: Help fine-tuning a file-placing script?
                      Maria964 Level 1

                      Thank you John for the info. I am one one the many non scripter people, so I have to surveil places like this forum to collect this «perls» that help me carry on my work.

                      Have a nice day.

                       

                      Maria