Search code examples
javascriptadobe-indesign

Open multiple separately packaged indesign files at once with script


I feel like I am already really close to making this happen I just need to add another level to it.

So far the code below will open every indesign file in the folder selected when you run the script.

var myFolder = Folder.selectDialog("Select Indesign Folder");  
var myIndsnFiles = myFolder.getFiles("*.indd");  

for(k=0; k<myIndsnFiles.length; k++)  
{  
    app.open(myIndsnFiles[k]);  

    }

So for example lets say the path to this folder that contains multiple indesign files is desktop/ads/client1 but in the ads folder there are multiple folders (client1, client2, client3 etc.) and each one contains an indesign file.

What I want to do is select the ads folder and run the script and have it automatically search each folder and open the indesign files that lie inside.

I hope I explained this well enough to make sense. Thanks in advance.


Solution

  • I did my own recursive function for getting files whatever subfolder they are in…

    var api = {
    	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 ) ) 
    				files[ files.length ]  = f;
    			}
    		
    			return files;
    		}
    	
    		else
    		{
    			return fo.getFiles ( filterFunction );
    		}
    	},
    }
    
    var fo =  Folder.selectDialog(), u, files;
    if ( fo ) {
    	files = api.getFiles ( fo, ["indd"], true, u, false );
    	alert( files.join("\r") );
    }