1 Reply Latest reply on Mar 12, 2012 11:44 AM by [Jongware]

    How to loop a script... (novice in over head?!)

    KuddRoww Level 1

      At the core of it I want to be able to change the filelink from a tif extension to an ai extension if the appropriate file exists. I can do this through a contrived search and replace on the IDML files but it scares the people who work on the files... so I'm left coming up with an InDesign script which mimics this sort of workflow. One of the InDesign scripting references I purcahsed 'InDesign CS5 Automation Using XML and Javascript' by Grant Gamble has a great script for accomplishing this. However the script is written with a single file input in mind and I'd like to extend it to handle a list of files defined by the user.


      I've tried with my sub-novice skills to loop it and I end up with a bunch of null object garble.  I've managed to modify to the point where it will open up the next document in the 'files' list but InDesign doesn't seem to reckognize its existence for lack of a better term.  At the end of the day I might be in over my head, I've struggled with this for too long so at this point I'm hoping someone can help me along and I can backtrack how their solution worked! I feel that this type of solution would help to evolve my understanding of Functions and variable scope.


      I'm likely in need of a paradigm shift! In a perfect world I would like to be able to:


      myFolder = selectDialog();

      files = myFolder.length;

      for(i=0; i<files.length; i++){



      Run("other script")





      Here's the "other script":

      // Example

      var g = {};


      g = null;


      function main(){

          if(app.documents.length == 0){

              alert("Please open the g.document containing the images to be relinked.");



              intResult = buildDialog();

              if(intResult == 1){






      function buildDialog(){

          g.doc = app.activeDocument;

          g.win = new Window("dialog", "Re-link Images");

          g.win.add("statictext", undefined, "Choose file extension or enter text:");

          var arrExtensions = [".ai", ".bmp", ".eps", ".gif", ".jpg", ".png", ".psd", ".tif"];


          // From panel

          g.win.pnlFrom = g.win.add("panel", undefined, "Find:");

          g.win.pnlFrom.ddl = g.win.pnlFrom.add("dropdownlist", undefined, arrExtensions);

          g.win.pnlFrom.txt = g.win.pnlFrom.add("edittext");

          g.win.pnlFrom.txt.minimumSize = [200, 20];

          g.win.pnlFrom.ddl.onChange = function(){

              if(this.selection != null){g.win.pnlFrom.txt.text = this.selection.text;}



          // To panel

          g.win.pnlTo = g.win.add("panel", undefined, "Change to:");

          g.win.pnlTo.ddl = g.win.pnlTo.add("dropdownlist", undefined, arrExtensions);

          g.win.pnlTo.txt = g.win.pnlTo.add("edittext");

          g.win.pnlTo.txt.minimumSize = [200, 20];

          g.win.pnlTo.ddl.onChange = function(){

              if(this.selection != null){g.win.pnlTo.txt.text = this.selection.text;}



          // Add button

          g.win.btnAdd = g.win.add("button", undefined, "Add");

          g.win.btnAdd.onClick = updateListBox;


          // Criteria list box

          g.win.lstChanges = g.win.add("listbox", undefined, undefined,

          {numberOfColumns: 2,

           showHeaders: true,

           columnTitles: ['Find:', 'Change to:'],

           columnWidths: [100, 100]


          g.win.lstChanges.minimumSize = [200, 100];


          // Delete button

          g.win.btnDelete = g.win.add("button", undefined, "Delete Selected");

          g.win.btnDelete.onClick = deleteListItem;


          // Manual/Auto radio buttons

          g.win.radManual = g.win.add("radiobutton", undefined, "Manual");

          g.win.radAuto = g.win.add("radiobutton", undefined, "Automatic");

          g.win.radAuto.value = true;


          // Relink and Cancel buttons

          g.win.btnRelink = g.win.add("button", undefined, "Relink");

          g.win.btnRelink.onClick = function(){

              if(g.win.lstChanges.items.length == 0){

                  alert("Please specify the relink criteria.");






          g.win.btnCancel = g.win.add("button", undefined, "Cancel");


          return g.win.show();


      } // End function buildDialog


      function updateListBox(){

          if(g.win.pnlFrom.txt.text == "" || g.win.pnlTo.txt.text == "" ){

              alert("Please enter text in both boxes.");



              var itm = g.win.lstChanges.add("item", g.win.pnlFrom.txt.text);

              itm .subItems[0].text = g.win.pnlTo.txt.text;

              g.win.pnlFrom.txt.text = "";

              g.win.pnlTo.txt.text = "";

              g.win.pnlFrom.ddl.selection = null;

              g.win.pnlTo.ddl.selection = null;




      function deleteListItem(){

          if(g.win.lstChanges.selection == null){

              alert("Please select the item to be deleted");







      function  relinkGraphics(){

          var strFeedback = "";

          var blnRelink;

          for(i = 0; i < g.doc.allGraphics.length; i ++){


              // Exclude embedded items

              if(g.doc.allGraphics[i].itemLink == null){




              // Apply all changes specified by user to filepath of graphic

              strBefore =  g.doc.allGraphics[i].itemLink.filePath.toLowerCase();

              currentLink = g.doc.allGraphics[i].itemLink.filePath.toLowerCase();

              for(j = 0; j < g.win.lstChanges.items.length; j++){

                  currentFind = g.win.lstChanges.items[j].text.toLowerCase();

                  currentChange = g.win.lstChanges.items[j].subItems[0].text.toLowerCase();

                  currentLink = currentLink.replace(currentFind, currentChange);



              // Relink image if applicable

              blnRelink = false;

              if(strBefore != currentLink){

                  blnRelink = true;

                  if(g.win.radManual.value == true){


                      blnRelink = confirm("Relink this image?");



              if(blnRelink == true){



                      strFeedback += "Relinked: " + strBefore + " ->\nto: " + currentLink + "\n\n";



                      strFeedback += "Could not relink: " + strBefore + "\n-> to: " + currentLink + "\n\n";





          // Display user feedback

          if(strFeedback == ""){

              alert("No images were relinked.");



              var fleFeedback = File(app.activeScript.parent + "/relink-log.txt");








                  alert("Sorry. Due to an error, the log file could not be created.");





      Any and all help is greatly appreciated,

        • 1. Re: How to loop a script... (novice in over head?!)
          [Jongware] Most Valuable Participant

          The full script runs on "the active document", and it's possible that is the point where ID gets confused. Can you re-write it as a function with *one* parameter -- the document to work on?


          Then you can do your loopy (sorry) looping like this:


          someDoc = app.open(files[i]);

          Run("other script", someDoc);



          "app.open" returns a handle to the ID document once it's opened, and if you save it into a variable you can use this wherever you are using "app.activeDocument" now.

          1 person found this helpful