Skip navigation
Currently Being Moderated

find and open files in subfolders

Feb 15, 2011 7:26 AM

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

 
Replies
  • Currently Being Moderated
    Feb 15, 2011 10:52 AM   in reply to kenjones99

    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[i] );
                    }
                    myBook.save();
               }
         }
    }
     
    //=================================== FUNCTIONS =========================================
    function GetSubFolders(theFolder) {
         var myFileList = theFolder.getFiles();
         for (var i = 0; i < myFileList.length; i++) {
              var myFile = myFileList[i];
              if (myFile instanceof Folder){
                   GetSubFolders(myFile);
              }
              else if (myFile instanceof File && myFile.name.match(/\.indd$/i)) {
                   myFiles.push(myFile);
              }
         }
    }
    //--------------------------------------------------------------------------------------------------------------
    

    Hope this helps.

     

    Kasyan

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 15, 2011 1:11 PM   in reply to Kasyan Servetsky

    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 )

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 16, 2011 1:28 AM   in reply to kenjones99

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 16, 2011 2:31 AM   in reply to kenjones99

    I too use Spotlight a lot AppleScript… Kasyan's method is in the somewhere between these for performance but I would expect it to be some what closer to Spotlight…

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 29, 2012 9:47 AM   in reply to Kasyan Servetsky

    What a great example of when/how to use recursion! Thank-you to Jongware for spelling it out and Kasyan for scripting it. Previous to reading this post I had been using Command Prompt to write up tab separated dir lists and then I read those in for file queues. Haha things are looking a little less hacked on my end.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 30, 2012 12:58 AM   in reply to [Jongware]-9BC6tI

    @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:

     

    InDesignFilesWithoutSuffix.png


    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

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 30, 2012 1:52 AM   in reply to Laubender

    Uwe: I must admit I've somehow managed to save a file without the proper suffix on my Mac a couple of times -- much to my surprise, as I am so used to Windows' always adding one! I have no idea what the magic (or in this case, voodoo) sequence is that makes the Save dialog omit the suffix.

     

    Sure, it is possible to test all suffix-less files with my script -- but ask youself the question "what is suffix-less?" What if you save an InDesign file as "my.test.file" -- surely the Mac will think the extension is ".file"? So you can't rely on this, unless you want to test each and every not-InDesign file ...

    In addition, on Windows at least, adding ".indd" to a file name makes Windows think it is an InDesign file Then you should use my script on every file, even on .indd ones!

     

    Oh enough with the fun. I see you still have your proper icons and "Art" descriptions on the Mac for all files. Wouldn't it be easier to test the Mac OS file type? Is that available through JS?  (I'd gladly try that out myself immediately, but am at work & behind Windows...)

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 30, 2012 2:17 AM   in reply to [Jongware]

    @Jongware – the point is, on Mac OSX all users could deliberately ommit the suffix if they choose "Save As" and delete the automatic generated suffix in the dialog (I did for testing reasons).

     

    And you are right: if we like to catch those we have to test with an altered IDentify.jsx against ALL files. (Sigh.)

     

    Beeing on the Mac maybe it's better to test by AppleScript to get the file information. But I'm not sure at all if that will work in every case. Maybe an AppleScript savvy participant could comment on that?

     

    Uwe

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 30, 2012 8:20 AM   in reply to Laubender

    This stuff is extremely confusing on the Mac, to me anyway. Without the extension—which OS X uses to lookup the file's "Uniform Type Identifier," which will be associated with the highest installed version of InDesign—OS X will rely on the (now deprecated) file type and creator codes in an extended attribute to associate the file with an application. And that's if you save it from InDesign without an extension. If you remove the extension in the Finder, it will hide the extension but silently leave it there and continue to use the extension and UTI. (And if you then remove the extended attribute that holds the code, the Finder will unhide the extension!) Voodoo indeed. You could use an extension check along with a doScript() Applescript call inside a filter function for getFiles():

     

    var folder = Folder.desktop,
        files = folder.getFiles(function(file) {
            var fT;
            if (file instanceof Folder) return false;        
            if (file.fsName.split(".").pop() === "indd") return true;
            fT = app.doScript("tell application \"Finder\"\ntry\nreturn the file type of (POSIX file \"" + file.fsName + "\" as alias)\non error\nreturn \"\"\nend try\nend tell", ScriptLanguage.APPLESCRIPT_LANGUAGE);
            if (typeof fT === 'string' && fT.substring(0, 3) === "IDd") return true;
            return false;
        });
     
    alert(files);
    

     

    But I bet it would be faster and less error-prone to just read enough bytes from the beginning of every file to determine whether or not it's an InDesign file, as you suggested.

     

    Jeff

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 30, 2012 10:58 AM   in reply to absqua

    Jeff, as Jong pointed out earlier you don't need to call out to AppleScript to get those mac identifiers they are available properties of the File Object…

     

    var f = File.openDialog( 'Pick Me…', undefined, false );
     
    alert( 'Is Indesign = ' + /InDn/.test( f.creator ) );
     
    alert( 'Is CS' + Number( f.type.match( /\d$/ ) - 2 ) ); // My lazy doc only match
     
    $.writeln( f.fsName ); // Gives full name even with ext OS hidden…
     
    
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 30, 2012 11:27 AM   in reply to Muppet Mark

    D'oh! I could've used that a time or two. Thanks. A better (and reasonably quick) filter function, then:

     

    var folder = Folder.desktop,
        files = folder.getFiles(function(file) {    
            return !(file instanceof Folder) && (file.fsName.split(".").pop() === "indd" || file.type.indexOf("IDd") === 0);
        });
     
    alert(files);
    
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 30, 2012 11:58 AM   in reply to absqua

    I only have CS2 & CS5 so all betwixt & between is a best guess… CS5.5 is that still IDd7? I dunno… I don't have Lion either so this may be kitty litter in that?

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 30, 2012 12:09 PM   in reply to Muppet Mark

    CS5.5 is "IDd2"! (Of course!)

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 30, 2012 12:33 PM   in reply to absqua

    PM or link me one… I want to see…

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 30, 2012 12:46 PM   in reply to Muppet Mark
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 30, 2012 12:52 PM   in reply to absqua

    Ouaf!

     

    Screen Shot 2012-03-30 at 9.50.11 PM.png

     

    Now I'm a-shiver for the very first ID CS6 document to try this on ...

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 30, 2012 1:12 PM   in reply to Muppet Mark

    Mark—That was kind of silly of me; of course the extended attributes don't survive the Dropbox upload/download. I don't know how I could get it to you with the metadata intact...

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 30, 2012 1:17 PM   in reply to absqua

    I wasn't sure about that… Compress it…?

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 30, 2012 1:20 PM   in reply to Muppet Mark

    Yep, sure—something (more than usually) wrong with my brain today. Here it is: http://dl.dropbox.com/u/422307/CS5.5.indd.zip

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 30, 2012 1:33 PM   in reply to absqua

    Your not mistaken IDd2 whats that all about…

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points