Skip to main content
Bas Donders
Participant
February 21, 2023
Answered

Help with adjusting Indesign script

  • February 21, 2023
  • 2 replies
  • 534 views

Hi,

 

I need some help adjusting this code. It almost does what i need to have. It outputs a folder with images to a CSV file where it shows the path to every image and the filename and extention.

What i need is the same but then without the extention to the name.

Anyone have any knowledge of this kind of code and knows what i need to adjust to get it to work my way? I tried a few things but i can't find out what i need to change.

 

I would very much appreciate 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 = [], 
        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);

 

This topic has been closed for replies.
Correct answer Charu Rajput

Hi @Bas Donders , Replace one following line from your code

fiName = decodeURI(fi.name);

to

fiName = decodeURI(fi.name.replace('.' + ext, ''));

 

2 replies

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
February 21, 2023

Hi @Bas Donders , Replace one following line from your code

fiName = decodeURI(fi.name);

to

fiName = decodeURI(fi.name.replace('.' + ext, ''));

 

Best regards
Bas Donders
Participant
February 22, 2023

Thank you!

Bas Donders
Participant
February 21, 2023

Sorry I see I didn't copy the whole script. Here is the whole script.

 

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 = [], 
        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();