6 Replies Latest reply on Jul 26, 2017 2:34 AM by Antony Mooe

    Find and place images from subfolders

    Antony Mooe Level 1

      Hello,

      I have an Indesign file with the name of the images I want to place:

      Screen Shot 2017-07-24 at 20.25.50.png

       

       

      Each images has it's folder and they're in a top folder named "fotos".

       

      The structure:

      fotos / 10 - description / image1

      fotos / 20 - description / image2

      fotos / 30 - description / image2

      fotos / etc………    

       

       

      Here is the code I'm using:

      It finds the subfolders and the images files, but don't place them into Indesign.

       

      if(app.documents.length != 0)
      
      
      { 
      
      
      var subf = Folder ("/Users/iMac/Desktop/fotos/");
      var sf = subf.getFiles(onlyFolders);
      var s = "";
      for (var n = 0; n < sf.length; n++) {
        s += sf[n].name + "\r";
      }
      alert(s);
      
      function onlyFolders(subf) {
        if (subf.name == "File") {
          return false;
        } else {
          return true;
        }
      } 
        
        
          if(subf != null){ 
              // reset the Find/Change dialog 
              app.findGrepPreferences = app.changeGrepPreferences = null; 
              // formulate a grep search string 
              app.findGrepPreferences.findWhat = '@.+?@'; 
              // find all occurrence, last one first 
              f = app.activeDocument.findGrep (true); 
              for (i = 0; i < f.length; i++){ 
                  // construct file name 
                  name = f[i].contents.replace (/@/g, ''); 
                  
                  // place the image 
                  var placedObjects = f[i].insertionPoints[0].place (File (sf +  '/'+ name+'/')); 
                             
              } 
          // delete all @??@ codes 
          app.activeDocument.changeGrep(); 
          } 
      } 
      else{ 
         alert('Please open a document and try again.'); 
      } 
      

       

       

      Here is the error from ExtendScript Toolkit:

       

      Screen Shot 2017-07-24 at 20.59.38.png

       

       

       

       

      Can I get help from you guys?!

       

      Thank you.

        • 1. Re: Find and place images from subfolders
          Loic.Aigon Adobe Community Professional

          Your biggest mistake is that you are using your reference to the array of files your retrieve from the folder user selection instead of the folder itself.

           

          var sf = subf.getFiles(onlyFolders);

          //This is an array of files objects

           

          var placedObjects = f[i].insertionPoints[0].place (File (sf +  '/'+ name+'/'));

          //sf isn't the folder object !

          //subf is

          • 2. Re: Find and place images from subfolders
            Antony Mooe Level 1

            Hello,

            Thank you for your reply.

             

            I changed the code to:

             

            if(app.documents.length != 0)
            
            
            { 
            
            
            var subf = Folder ("/Users/iMac/Desktop/fotos/");
            var sf = subf.getFiles(onlyFolders);
            var s = "";
            for (var n = 0; n < sf.length; n++) {
              s += sf[n].name + "\r";
            }
            alert(s);
            
            function onlyFolders(subf) {
              if (subf.name == "File") {
                return false;
              } else {
                return true;
              }
            } 
              
              
                if(subf != null){ 
                    // reset the Find/Change dialog 
                    app.findGrepPreferences = app.changeGrepPreferences = null; 
                    // formulate a grep search string 
                    app.findGrepPreferences.findWhat = '@.+?@'; 
                    // find all occurrence, last one first 
                    f = app.activeDocument.findGrep (true); 
                    for (i = 0; i < f.length; i++){ 
                        // construct file name 
                        name = f[i].contents.replace (/@/g, ''); 
                        
                        // place the image 
                        var placedObjects = f[i].insertionPoints[0].place (File (subf +  '/'+ name+'/')); 
                                   
                    } 
                // delete all @??@ codes 
                app.activeDocument.changeGrep(); 
                } 
            } 
            else{ 
               alert('Please open a document and try again.'); 
            } 
            

             

            But it returns to the top folder and don't search in the subfolders.

            Do you know how can fix the code?

             

            Thank you.

            • 3. Re: Find and place images from subfolders
              Loic.Aigon Adobe Community Professional

              The problem is that getFiles won't look at subfolders (unless I am wrong). You need a recursive function.

               

              A possible approach:

              var lib = {
              getFiles : function ( fo, aExtensions, bRecursive, aFiles, includeFolder )
              {
              var exts = aExtensions? aExtensions.join("|") : ".+" ;
              var pattern = new RegExp ( "\\."+exts+"$", "g" );
              var files = aFiles? aFiles : [];
              var filterFunction = function(file)
              {
              return pattern.test ( file.name );
              }
              
              if ( bRecursive )
              {
              var foFiles = fo.getFiles();
              while (  f = foFiles.shift() )
              {
              if ( f instanceof Folder )
              {
              if (includeFolder===true) files[ files.length ] = f;
              
              this.getFiles ( f, aExtensions, true, files );
              }
              if ( f  instanceof File && pattern.test ( f.name ) && f.name!=".DS_Store" ) 
              files[ files.length ]  = f;
              }
              
              return files;
              }
              
              else
              {
              return fo.getFiles ( filterFunction );
              }
              },
              getFilesDB:function(arrFiles){
              var db = {}, n = arrFiles.length, aFile;
              while ( n-- ) {
              aFile = arrFiles[n];
              db[decodeURI(aFile.name)] = aFile;
              }
              return db;
              },
              
              
              replaceFiles:function ( doc, db ){
              var fgp = app.findGrepPreferences.properties,
              found = [], 
              text,
              n = 0, 
              fileName = "",
              file;
              
              app.findGrepPreferences.properties = {
              findWhat:"@.+@",
              };
              found = doc.findGrep();
              
              n = found.length;
              
              while ( n-- ) {
              text = found[n];
              tf = text.parentTextFrames.length ? text.parentTextFrames[0] : text.parentStory.textContainers[0];
              fileName = text.contents.replace(/@/g, "" );
              file = db[fileName];
              if ( file ) {
              tf .contents = "";
              tf.contentType = ContentType.GRAPHIC_TYPE;
              tf.place ( file );
              tf.fit ( FitOptions.FILL_PROPORTIONALLY );
              tf.fit ( FitOptions.CENTER_CONTENT );
              }
              }
              
              }
              };
              
              
              var main = function(paragraph) {
              var doc = app.properties.activeDocument,
              fo, files, db;
              
              
              if ( !doc ) return;
              
              fo = Folder.selectDialog("Please choose the parent folder" );
              if ( !fo ) return;
              
              files = lib.getFiles ( fo, ["jpe?g"], true);
              
              if  (!files.length) {
              alert("no files returned!");
              return;
              }
              
              
              db = lib.getFilesDB ( files );
              
              lib.replaceFiles ( doc, db );
              
              }
              
              
              var u;
              
              
              app.doScript ( "main(app.activeDocument.stories[0].paragraphs[-1])",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
              
              • 4. Re: Find and place images from subfolders
                Antony Mooe Level 1

                Man,

                you made my day!!!!

                 

                Thank you so much!!

                 

                Cheers!

                • 5. Re: Find and place images from subfolders
                  Loic.Aigon Adobe Community Professional

                  Be aware it might not be bulletproof. What if several images are equally named ? Actually script "overrides" file references so the latest found file will prior when placed (I only one "1.jpg" file will be placed even if several found).

                  • 6. Re: Find and place images from subfolders
                    Antony Mooe Level 1

                    I will keep that in mind.

                    It works perfectly for what I need.

                     

                    Thank you again!