Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Hi @Roland37372810sxr6, without seeing an example titlePart and an example folder structure, I can't know if I've understood what you want correctly, but if you just want to find a file in a known folder, you can just check if it exists, for example:
function findImageFile2(folderPath, titlePart) {
if (!/\/$/.test(folderPath))
// add trailing slash
folderPath += '/';
var pathToCheck = folderPath + titlePart.toLowerCase().replace(/_/g, ' ');
var f = File(pathToCheck);
if (f.exists)
return f;
alert('No file at "' + pathToCheck + '".');
};
This way, if you believe it is generating the correct path, you can check the path in another way, such as via a terminal.
But if you really do need to "search" for a file, then it is more complex because you need to recurse into sub-folders. Let me know if this is what you mean to do.
- Mark
Copy link to clipboard
Copied
I didn't include all the rest of the script because it seems to be working fine. But it uses file name information to find the right file, and it seems to do so because the alerts are showing the file names for every file I was looking for.
The alerts are showing a decoded file name and the text from the title ... as orignal file names would be too hard to match perfectly. So original title is formatted ItemName_Title_Extraneous_Extraneous_Extraneous.jpg. The alerts are showing that it is decoding it to: itemname title extraneous extraneous extraneous ; then says it's comapring that to title. Match not found.
I tried putting an image into the folder that was nothing but a title.jpg, and it oculd not match that either. I also tried breaking it up and searching for individual words - which was not useful for me since these words could be found in different combinations - but it did not identify them. If title was Blue Door 1, and there were files Blue Dash, Blue 10, and Blue Dog, it would not identify blue in any of those file names. I feel like there is something very wrong with how I'm trying to find the match but I don't understand what since I'm a little confused about the adobe scripting environment and what works and what doesn't.
Copy link to clipboard
Copied
Okay thanks for the clarification—you are searching in the filename, not *deriving* the filename.
Well, first thing to say is: you are not in a dead end—this is something we all do all the time with complete success, so there is just something wrong which we can get to the bottom of with a bit more effort.
When I run your function (I picked the 2nd one) it works as expected, matching a file that has the modified titlePart found in the filename.
So we need to set up a more realistic little test scenario. So you could either (a) create a folder structure, and zip it and share it via link here, along with a real example "titlePart", or (b) just share a bunch of real file names, along with a real "titlePart" so that we can set up a test ourselves and test your code on.
- Mark
Copy link to clipboard
Copied
Okay, so I looked a bit deeper and after some more debugging, it looks like there's something wrong with how it's parsing the "title" form. The title is being generated by easy catalog. So that seems to be the issue, even tho everythign looks proper, it's not a normal text value that indesign can use as a search or something. If I untag it from easy catalog it works fine. Maybe I'll just live this way for the time being unless anyone has a good idea.
Copy link to clipboard
Copied
I'm pretty sure someone here will have a good idea and be able to help you, but we need some more information. Would it help to share your title parsing code, for example?
Or maybe someone familiar with EasyCatalog (I'm not unfortunately) will have something to say.
- Mark
Copy link to clipboard
Copied
I guess that EasyCatalog inserts some special characters so the text (file name) looks visually identical but is different under the hood.
I have encountered such problems many times and wrote a couple of functions that helped me solve them on the fly.
— Kas
Copy link to clipboard
Copied
Just riffing on @Kasyan Servetsky's idea, you could do a pretty good comparison between just subset of the visible letters, for example:
var illegalChars = /[^a-z0-9]+/g;
var cleanTitle = titlePart.lowercase().replace(illegalChars,'');
var match = -1 !== filename
.lowercase()
.replace(illegalChars,'')
.search(cleanTitle);
Copy link to clipboard
Copied
Thank you everyone! I needed a good night's sleep and took a crack at it this morning, and you guys are right. There must be some kind of invisible characters inserted by EasyCatalog to tag insertion points. I guess I was too focused on cleaning the file names, since I the alerts showed it adding %20 to file names. I'm never sure what actually works inside the Adobe Scripting environment and what doesn't, and was also baffled as my database had essentially become a giant loop- generating title cell values from the image file names, loading them into the template, and then using those titles to find the pictures. How copying A -> B -> C and have C not match A was making me lose my mind. A reminder to everyone: when you've reached a dead end, a good night's sleep and some patient debugging is always a good thing to try!
I ended up using this to clean the text:
function cleanText(text) {
return text.replace(/[^\x20-\x7E]/g, '').replace(/^\s+|\s+$/g, '').toLowerCase();
}
Was able to place 50 pictures in a single click. Hoorah.
Copy link to clipboard
Copied
Excellent news!
Copy link to clipboard
Copied
Great. But it's always a good idea to post some screenshots with the initial question.
Copy link to clipboard
Copied
@Robert at ID-Tasker said: "But it's always a good idea to post some screenshots with the initial question."
Hi together,
yes, so true, especially if you have a layout where text is tagged with EasyCatalog field tags.
Those field tags are special characters that contain all relevant information for EasyCatalog.
Unicode value is FEFF.
Also note that FEFF special characters are used by InDesign for various purposes.
Fully apart from a plug-in like EasyCatalog. For example for InDesign Notes, Index Markers, XML tagging, PDF Comments.
Within InDesign you cannot find FEFF special characters with a GREP pattern. Instead you have to do it with Text Find and look after <FEFF>.
Regards,
Uwe Laubender
( Adobe Community Expert )