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
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
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
)
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.
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. ![]()
@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
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...)
@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
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
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…
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);
Yep, sure—something (more than usually) wrong with my brain today. Here it is: http://dl.dropbox.com/u/422307/CS5.5.indd.zip
North America
Europe, Middle East and Africa
Asia Pacific