Skip to main content
Participant
May 15, 2024
Answered

Help with adjusting InDesign script (imagesToCSV104.jsx)

  • May 15, 2024
  • 1 reply
  • 639 views

Hi

 

I've been using this images to csv script (imagesToCSV104.jsx) for years. Unfortunatelly it does not work anymore as it should. It doesn't sort the image list in an alphanumeric order into csv file. I'm using OS Sonoma, and the image list is in irregular order.

 

Please, could somebody help me to update the code to add name sorting in it?

 

 

Array.prototype.inArray = function(obj){
    var arrMax = this.length-1;
    for(var i=arrMax; i>=0; i--){
        if(this[i]===obj){
            return true;
        }
    }   
    return false;
}

var csvParser = (function(){
    var csvFile;
    return{
        create:function(fo){
          csvFile=File(fo+"/"+fo.name+".csv");  
        },
        write:function(csvContent){
            csvFile.open('w');
            csvFile.encoding="UTF-8";
            csvFile.write(csvContent);
            csvFile.close();
        },
        execute:function(){
           csvFile.execute(); 
        },
        getCSV:function(){
            return csvFile;
        }
    }
})();

function imagesToCSVthenChoose(){
    var doc,
        fo,
        fis,
        fiMax,
        fi,
        fiName,
        fiPath,
        imgFormats=["eps","jpg","tif","psd","pdf","png","ai","bmp","jpeg"],
        imgFormatMax = imgFormats.length-1,
        imgOk = [],
        csvContent = [], 
        ext,
        csvLine=[],
        csvSep=",";
        
    if(app.documents.length==0){
        alert("No documents open !");
        return
    }

    doc=app.activeDocument;
    fo = Folder.selectDialog("Please choose a folder with images");
    if(!fo) return
    
    fis = fo.getFiles();
    fiMax=fis.length;
    
    for(var i=0; i<fiMax; i++){
        
        fi=fis[i];
        ext = fi.name.match(/\.([a-z]+)$/i);
        if(ext==null) continue;
        ext = ext[1].toLowerCase();
        if(!imgFormats.inArray(ext)) continue;
        fiName = decodeURI(fi.name);
        fiPath=decodeURI(fi.fsName);
        csvContent.push(fiName+csvSep+fiPath);
    }
    
   csvContent = "Name"+csvSep+"@images\r"+csvContent.join("\r");
   csvParser.create(fo);
   csvParser.write(csvContent);
   
   /*
   doc.dataMergeProperties.selectDataSource(csvParser.getCSV());
    var myMenuAction = app.menuActions.item("$ID/DataMergePanelName");
    myMenuAction.invoke();
   */
}


imagesToCSVthenChoose();

 

 

This topic has been closed for replies.
Correct answer koskkim84

Thank you @Marc Autret for your reply.

I didn't have enough time or temper for this, so I used Codeium AI to modify the existing script.

Now it works like charm 🤩

Here's the modified code for all the intrested. Please share if you'd like.

Array.prototype.inArray = function(obj) {
    var arrMax = this.length - 1;
    for (var i = arrMax; i >= 0; i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
}

var csvParser = (function() {
    var csvFile;

    return {
        create: function (fo) {
            csvFile = File(fo + "/" + fo.name + ".csv");
        },
        write: function (csvContent) {
            csvFile.open('w');
            csvFile.encoding = "UTF-8";
            csvFile.write(csvContent);
            csvFile.close();
        },
        execute: function () {
            csvFile.execute();
        },
        getCSV: function () {
            return csvFile;
        }
    }
})();

function convertImagesToCSVThenChoose() {
    var doc,
        fo,
        fis,
        fiMax,
        fi,
        fiName,
        fiPath,
        imgFormats = ["eps", "jpg", "tif", "psd", "pdf", "png", "ai", "bmp", "jpeg"],
        imgOk = [],
        csvContent = [],
        ext,
        csvLine = [],
        csvSep = ",";

    if (app.documents.length == 0) {
        alert("No documents open!");
        return;
    }

    doc = app.activeDocument;
    fo = Folder.selectDialog("Please choose a folder with images");
    if (!fo) return;

    fis = fo.getFiles();
    fiMax = fis.length;

    for (var i = 0; i < fiMax; i++) {
        fi = fis[i];
        ext = fi.name.match(/\.([a-z]+)$/i);
        if (ext == null) continue;
        ext = ext[1].toLowerCase();
        if (!imgFormats.inArray(ext)) continue;
        fiName = decodeURI(fi.name);
        fiPath = decodeURI(fi.fsName);
        csvContent.push(fiName + csvSep + fiPath);
    }

    // Sort csvContent array based on image names
    csvContent.sort(function(a, b) {
        var nameA = a.split(csvSep)[0].toLowerCase();
        var nameB = b.split(csvSep)[0].toLowerCase();
        if (nameA < nameB) {
            return -1;
        }
        if (nameA > nameB) {
            return 1;
        }
        return 0;
    });

    csvContent = "Name" + csvSep + "@images\r" + csvContent.join("\r");
    csvParser.create(fo);
    csvParser.write(csvContent);

    /*
    doc.dataMergeProperties.selectDataSource(csvParser.getCSV());
    var myMenuAction = app.menuActions.item("$ID/DataMergePanelName");
    myMenuAction.invoke();
    */
}

convertImagesToCSVThenChoose();

 

1 reply

Marc Autret
Legend
May 15, 2024

Hi @koskkim84 

 

You obviously need something like csvContent.sort(); after the for loop. The script doesn't perform any sort in its current state.

 

[The reason why it previously worked is probably that Folder.getFiles() was already returning sorted elements, which likely depends on system-level settings.]

 

Best,

Marc

koskkim84AuthorCorrect answer
Participant
May 15, 2024

Thank you @Marc Autret for your reply.

I didn't have enough time or temper for this, so I used Codeium AI to modify the existing script.

Now it works like charm 🤩

Here's the modified code for all the intrested. Please share if you'd like.

Array.prototype.inArray = function(obj) {
    var arrMax = this.length - 1;
    for (var i = arrMax; i >= 0; i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
}

var csvParser = (function() {
    var csvFile;

    return {
        create: function (fo) {
            csvFile = File(fo + "/" + fo.name + ".csv");
        },
        write: function (csvContent) {
            csvFile.open('w');
            csvFile.encoding = "UTF-8";
            csvFile.write(csvContent);
            csvFile.close();
        },
        execute: function () {
            csvFile.execute();
        },
        getCSV: function () {
            return csvFile;
        }
    }
})();

function convertImagesToCSVThenChoose() {
    var doc,
        fo,
        fis,
        fiMax,
        fi,
        fiName,
        fiPath,
        imgFormats = ["eps", "jpg", "tif", "psd", "pdf", "png", "ai", "bmp", "jpeg"],
        imgOk = [],
        csvContent = [],
        ext,
        csvLine = [],
        csvSep = ",";

    if (app.documents.length == 0) {
        alert("No documents open!");
        return;
    }

    doc = app.activeDocument;
    fo = Folder.selectDialog("Please choose a folder with images");
    if (!fo) return;

    fis = fo.getFiles();
    fiMax = fis.length;

    for (var i = 0; i < fiMax; i++) {
        fi = fis[i];
        ext = fi.name.match(/\.([a-z]+)$/i);
        if (ext == null) continue;
        ext = ext[1].toLowerCase();
        if (!imgFormats.inArray(ext)) continue;
        fiName = decodeURI(fi.name);
        fiPath = decodeURI(fi.fsName);
        csvContent.push(fiName + csvSep + fiPath);
    }

    // Sort csvContent array based on image names
    csvContent.sort(function(a, b) {
        var nameA = a.split(csvSep)[0].toLowerCase();
        var nameB = b.split(csvSep)[0].toLowerCase();
        if (nameA < nameB) {
            return -1;
        }
        if (nameA > nameB) {
            return 1;
        }
        return 0;
    });

    csvContent = "Name" + csvSep + "@images\r" + csvContent.join("\r");
    csvParser.create(fo);
    csvParser.write(csvContent);

    /*
    doc.dataMergeProperties.selectDataSource(csvParser.getCSV());
    var myMenuAction = app.menuActions.item("$ID/DataMergePanelName");
    myMenuAction.invoke();
    */
}

convertImagesToCSVThenChoose();