Monday, March 20, 2006

 

Filtering Files

On page 540 of the Scripting Reference it says [I've added emphasis to the two key words]:

----------------------------------------

File.openDialog ([prompt][,select])

Opens the built-in platform-specific file-browsing dialog in which a user can select an existing file to open. If the user clicks OK, returns a File object for the selected file. If the user cancels, returns null.

[snip]

select
Optional. A file or files to be preselected when the dialog opens:

[snip]

In Mac OS, a string containing the name of a method defined in the current JavaScript scope that takes a File object argument. The method is called for each file about to be displayed in the dialog, and the file is displayed only when the method returns true.

----------------------------------------

It took me a while to realize that the argument select is not a string but a reference to the function (not method) by name, like this:
function indtFilter(myFile) {
  if (myFile.name.slice(-5) == ".indt") { return true }
  return false;
}

var myFile = File.openDialog("Choose a template.", indtFilter);
if (myFile == null) { exit() }
That works, but put quotes around the name of the function and it won't work -- it shows you all the files.

Change the function definition to a method definition, like this:
File.prototype.indtFilter = function(myFile) {
  if (myFile.name.slice(-5) == ".indt") { return true }
  return false;
}
and you'll get an error that indtFilter is not defined.

OK, so this only affects Mac users, but that's what I am most of the time.

While my observations about the reference are accurate in the above, my assertion that the function indtFilter(myFile) works is valid only in the narrow sense that it is constructed correctly. It has a bug you can drive a bus through (as my brother likes to say).

The problem is that the way I have constructed it denies the user the ability to navigate folders. What's needed is this:
function indtFilter(myFile) {
  if (myFile.constructor.name == "Folder") { return true }
  if (myFile.name.slice(-5) == ".indt") { return true }
  return false;
}
Now, in addition to (in this case) any InDesign templates, the folder items in the file dialog will also be active, allowing the user to navigate the file system.

Comments:
Hello Dave,

Thanks for all your posts on indesign scripting. It just saved my life in a way.

Anyways, I've been looking for a way to determine image dimensions before before actually placing it in an indesign document. I have no idea if this is possible or not.

I've tried to find more info on this on adobe's indesign script forum as well as in the supplied pdf's without any result. So i'm still a bit in the dark but i'm still quite curious about this. Maybe you know more.

Anyways, again, your work is totally appreciated.

Cheers!
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?