Skip to main content
Participant
August 7, 2024
Question

Trying to script replacing images based off partials

  • August 7, 2024
  • 2 replies
  • 851 views

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. 

This topic has been closed for replies.

2 replies

Community Expert
August 8, 2024

@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 )

m1b
Community Expert
Community Expert
August 7, 2024

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

Participant
August 7, 2024

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.

Kasyan Servetsky
Legend
August 7, 2024

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.


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