Copy link to clipboard
Copied
Copy link to clipboard
Copied
function find_file (dir, mask)
{
var f = Folder (dir).getFiles ('*.*');
for (var i = 0; i < f.length; i++)
if ( // compare f.name and mask here
return f;
}
Copy link to clipboard
Copied
I found this very helpful, thanks!
I scan the crossword every day and use a script to place it in InDesign, create a matching grid and so on, to do the crossword on screen. I wanted to use the scanner's default choice of folder and filename, which is something like "15-05-2019_103515.jpg" (from today's date + time). Although I didn't use a wildcard in my mask, I used the slice method to remove all but the date.
Copy link to clipboard
Copied
Hi Jeremy,
method getFiles() supports a filter function as well.
var myFilteredFilesArray = myFolder.getFiles
(
function( file )
{
if
(
file instanceof File
&&
file.name.match( /* testForSuffixRegExp or whatever you like */ )
&&
!file.hidden
)
return true
}
);
Hope, you'll get the idea.
Regards,
Uwe
Copy link to clipboard
Copied
Hi Uwe,
That looks very useful too, thanks very much! I'm sure I'll use that approach in future. For what it's worth, here's the code I was using before I decided the slice method was easier:
var myRegex = new RegExp(myYear + "-" + myMonth + "-" + myToday + "_\\d{6}\\.jpg");
var myFile = findFile("Macintosh HD/Users/jeremy/Pictures", myRegex);
myPage.place(myFile, [36,36]);
function findFile(myFolder, myRegex) {
var myFiles = Folder(myFolder).getFiles("*.*");
for (var i = 0; i < myFiles.length; i++) {
if (myFiles.name.match(myRegex)) return (myFiles);
}
}
Jeremy
Copy link to clipboard
Copied
(Works beautifully too, by the way — thanks again!)