Skip to main content
February 15, 2011
Pregunta

find and open files in subfolders

  • February 15, 2011
  • 2 respuestas
  • 13055 visualizaciones

Hi

Simple request I know but I can't manage to find how to make fileArray = targetFolder.getFiles("*.indd"); to access *.indd files inside sub folders and perhaps in sub sub folders..

Can anyone share how to do this?

Thanks in advance, Ken

Este tema ha sido cerrado para respuestas.

2 respuestas

February 16, 2011

Thanks, that's got it.

I'm learning Javascript but coming from Applescript.. I admit there are some better ways to do things but this is not one!

-- tell finder to get ENTIRE contents of folder x whose name contains ".indd" -- is soo much simpler

Muppet_Mark-QAl63s
Inspiring
February 16, 2011

Whilst 'entire contents' is simpler to understand it has an overhead and is some distance slower with directories containing large quantities of files… The 'whose' filtering you will miss but there are posts here on how to add this functionality to JavaScript. At a glance only yesterday it looks like its in 'extendables' and 'marc' also posted one a while back too.

February 16, 2011

Understood Muppet Mark, it's a trade off between simplicity and speed.

I actually use OS X Spotlight to rapidly find .indd files on massive volumes as part of my Applescripts but want to be able to move to JS

Kasyan Servetsky
Legend
February 15, 2011

Here is an example -- this script gets all indesign documents from the selected folder including its subfolders and puts all them into a book.

var myFolder = Folder.selectDialog( "Select a folder with InDesign files" );
if ( myFolder != null ) {
     var myFiles = [];
     
     GetSubFolders(myFolder);
     
     if ( myFiles.length > 0 ) {
          var myBookFileName = myFolder + "/"+ myFolder.name + ".indb";
          myBookFile = new File( myBookFileName );
          if ( myBookFile.exists ) {
               if ( app.books.item(myFolder.displayName + ".indb") == null ) {
                    myBook = app.open( myBookFile );
               }
          }
          else {
                myBook = app.books.add( myBookFile );
                myBook.automaticPagination = false;
                for ( i=0; i < myFiles.length; i++ ) {
                    myBook.bookContents.add( myFiles );
                }
                myBook.save();
           }
     }
}

//=================================== FUNCTIONS =========================================
function GetSubFolders(theFolder) {
     var myFileList = theFolder.getFiles();
     for (var i = 0; i < myFileList.length; i++) {
          var myFile = myFileList;
          if (myFile instanceof Folder){
               GetSubFolders(myFile);
          }
          else if (myFile instanceof File && myFile.name.match(/\.indd$/i)) {
               myFiles.push(myFile);
          }
     }
}
//--------------------------------------------------------------------------------------------------------------

Hope this helps.

Kasyan

Inspiring
February 15, 2011

A slight clarification on Kasyan's script:

Contrary to what you are thinking, your own getFiles command will return Folders ... if its name ends with .indd, that is! It's just doing exactly to the letter what you asked for. And it's a mean trick to scripters to end a folder's name in INDD (I think a lot of my own scripts would fall over it, getting a folder when it really expected to be handed a file).

So Kasyan checks "everything", 'cause that's the default when you don't give an argument to getFiles. Each individual item is first checked whether it's a folder or not, and if it is, his routine calls itself (a process called "recursion"; and its name doesn't contain "curse" for nothing). So even if the folder name ends with .INDD, this part of the routine will do the proper thing.  Only if it's not a folder, he checks if the name ends with ".indd", and if so, It Is A Document.

(Note to Kasyan: uh, what about "MeanThingToDo.INDD"? A-ha -- your GREP match is case InSeNsItIvE. Almost got me there )

Community Expert
March 30, 2012

@Jongware – And then we have the "strange" case of InDesign files without suffix on Mac OSX. Some users might think it would be a good idea to ommit it so they could easily identify the InDesign version in the file system. For several reasons NOT a good idea at all, but unfortunately possible…

See the following screengrab:


Just an idea, did not test that yet:

to detect those files, it could be a good idea to "misuse" an altered version of your "IDentify.jsx" script (thanks to that a lot!).

@For all see Jongware's "IDentify.jsx" at:
http://forums.adobe.com/message/4071273#4071273

Uwe