• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
7

Help with adjusting InDesign script (imagesToCSV104.jsx)

New Here ,
May 15, 2024 May 15, 2024

Copy link to clipboard

Copied

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();

 

 

TOPICS
Scripting

Views

230

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

New Here , May 15, 2024 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;

    ret
...

Votes

Translate

Translate
Guide ,
May 15, 2024 May 15, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 15, 2024 May 15, 2024

Copy link to clipboard

Copied

LATEST

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();

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines