Trying to script replacing images based off partials
I have a database hosted on google workspace. I was hoping to use teh data there to generate PDFs for me - basically catalogs. I looked around and was shocked at how little support there is for google drive from indesign and almost every plugin available. Only solution i found was easy catalog which I'm trying now. However, my database has thumbnail images and easy catalog can not interact with them. Since I have a pretty structured image filenaming, I figured I could use an indesign script to just populate the catalog with teh full size images.
I think I ahve it mostly sorted, however the script is hitting a roadblock at partial matching. I've tried a million different approaches and none seem to work.
function findImageFile(folderPath, titlePart) {
var folder = new Folder(folderPath);
if (!folder.exists) {
alert("Folder does not exist: " + folderPath);
return null;
}
var files = folder.getFiles();
alert("Number of files in folder: " + files.length);
// Decode and handle underscores in title part, replace leading/trailing spaces
var decodedTitlePart = decodeURIComponent(titlePart).toLowerCase().replace(/[_]+/g, ' ').replace(/^\s+|\s+$/g, '');
var titleWords = decodedTitlePart.split(/\s+/);
alert("Searching for decoded and underscore-handled title part: '" + decodedTitlePart + "'");
for (var i = 0; i < files.length; i++) {
var fileName = files[i].name;
// Decode and handle underscores in file name, replace leading/trailing spaces
var decodedFileName = decodeURIComponent(fileName).toLowerCase().replace(/[_]+/g, ' ').replace(/^\s+|\s+$/g, '');
var fileWords = decodedFileName.split(/\s+/);
alert("Comparing file name: '" + decodedFileName + "' with title part: '" + decodedTitlePart + "'");
// Manually check each word in fileWords for any match in titleWords
var matchFound = false;
for (var j = 0; j < fileWords.length; j++) {
for (var k = 0; k < titleWords.length; k++) {
if (fileWords[j].indexOf(titleWords[k]) !== -1) {
matchFound = true;
break;
}
}
if (matchFound) break;
}
if (matchFound) {
alert("Match found: File '" + files[i].name + "' matches the title part.");
return files[i];
}
}
alert("No matching files found in: " + folderPath + " for title part: '" + decodedTitlePart + "'");
return null;
}
function findImageFile(folderPath, titlePart) {
var folder = new Folder(folderPath);
if (!folder.exists) {
alert("Folder does not exist: " + folderPath);
return null;
}
var files = folder.getFiles();
alert("Number of files in folder: " + files.length);
// Decode and handle underscores in title part
var decodedTitlePart = decodeURIComponent(titlePart).toLowerCase().replace(/_/g, ' ');
alert("Searching for decoded and underscore-handled title part: '" + decodedTitlePart + "'");
for (var i = 0; i < files.length; i++) {
var fileName = files[i].name;
if (typeof fileName === 'string') {
// Decode and handle underscores in file name
var decodedFileName = decodeURIComponent(fileName).toLowerCase().replace(/_/g, ' ');
alert("Comparing file name: '" + decodedFileName + "' with title part: '" + decodedTitlePart + "'");
var index = decodedFileName.indexOf(decodedTitlePart);
alert("Index of title part in file name: " + index);
if (index !== -1) {
alert("Match found: File '" + files[i].name + "' matches the title part.");
return files[i];
}
} else {
alert("Error: File name is not a string, cannot compare. File index: " + i);
}
}
alert("No matching files found in: " + folderPath + " for title part: '" + decodedTitlePart + "'");
return null;
}
Both of these produce alerts that hae the desired Title and File Name appearing to be compared, and yet unable to see the match. Anybody have an idea? It feels like a dead end for me since I'm getting exactly the comparisons I want but it's never making the match.
