Skip to main content
AntoinEOz
Participant
April 10, 2026
Question

InDesign script closed immediately.

  • April 10, 2026
  • 1 reply
  • 34 views

Hello everyone!

 

I’m trying to create a small plugin to manage my product listings.

It’s a user interface that contains the information needed to automatically fill out my document.

The plugin uses named objects such as “Model Name”

 

and automatically fills in the fields with data from a CSV file.

 

Previously, I had written a windowed version to test and debug it. But I needed a palette/dialog box to scroll through the entire document while I enter the information (which doesn’t work with a windowed version because the document remains static).

 

And I wanted an OK/Apply button to confirm changes and continue working without the page closing, while still being able to navigate the document at the same time.

 

For now, the problem is at the end of the script with dlg.show();

The dialog box closes instantly when I click on it.

 

Here is the code.

 

Can you help me?:)

 

 

 

// ======================================================
// PRODUCT SHEET
// ======================================================

if (app.documents.length === 0) {
alert("Aucun document ouvert.");
exit();
}

var doc = app.activeDocument;

// ---------------- CSV ----------------

function trim(s){ return (s||"").replace(/^\s+|\s+$/g,""); }

function fileToString(f){
f.encoding="UTF-8";
f.open("r");
var s=f.read();
f.close();
if(s.charCodeAt(0)===0xFEFF) s=s.substring(1);
return s;
}

function detectDelimiter(line){
var c1=(line.match(/;/g)||[]).length;
var c2=(line.match(/,/g)||[]).length;
return (c1>c2)?";":",";
}

function splitCSVLine(line,del){
var out=[],f="",q=null,inQ=false,c;
for(var i=0;i<line.length;i++){
c=line[i];
if(inQ){
if(c===q){
if(i+1<line.length && line[i+1]===q){ f+=q; i++; }
else{ inQ=false; q=null; }
} else f+=c;
} else {
if(c===del){ out.push(trim(f)); f=""; }
else if(c==="\""||c==="'"){ inQ=true; q=c; }
else f+=c;
}
}
out.push(trim(f));
return out;
}

function parseCSV(txt){
var lines=txt.replace(/\r\n/g,"\n").replace(/\r/g,"\n").split("\n");
var probe="";
for(var i=0;i<lines.length;i++){
if(trim(lines[i]).length){ probe=lines[i]; break; }
}
var del=detectDelimiter(probe||",");
var rows=[];
for(var r=0;r<lines.length;r++){
if(trim(lines[r]).length) rows.push(splitCSVLine(lines[r],del));
}
return rows;
}

function headerIndex(h,n){
n=n.toLowerCase();
for(var i=0;i<h.length;i++){
if(trim(h[i]).toLowerCase()===n) return i;
}
return -1;
}

function resolveImagePath(p,doc,csv){
p=trim(p);
var f=new File(p);
if(f.exists) return f;

if(doc.fullName){
f=new File(doc.fullName.parent.fsName+"/"+p);
if(f.exists) return f;
}

if(csv.parent){
f=new File(csv.parent.fsName+"/"+p);
if(f.exists) return f;
}

return new File(p);
}

// ---------------- CSV ----------------

var key="finitions_csv_path_group";
var last=app.extractLabel(key);
var csvFile=(last && (new File(last)).exists)?new File(last):null;

if(!csvFile){
csvFile=File.openDialog("Sélectionne le CSV","CSV:*.csv");
if(!csvFile){ alert("Aucun CSV."); exit(); }
app.insertLabel(key,csvFile.fsName);
}

var rows=parseCSV(fileToString(csvFile));
var headers=rows[0];

var idxNom = headerIndex(headers,"Nom");
var idxCode= headerIndex(headers,"Code");
var idxImg = headerIndex(headers,"Image");

var itemsData=[];
for(var r=1;r<rows.length;r++){
var row=rows[r];
if(!row||!row.length) continue;
var nom = trim(row[idxNom]||"");
var code= trim(row[idxCode]||"");
var img = trim(row[idxImg]||"");
if(nom) itemsData.push({nom:nom,code:code,image:img});
}

if(itemsData.length===0){
alert("Aucune finition valide dans le CSV.");
exit();
}
// ---------------- UI ----------------

var dlg = new Window("palette", "Fiche produit");
dlg.orientation = "column";
dlg.alignChildren = "fill";
dlg.margins = 15;

// GROUPE PRINCIPAL
var mainGroup = dlg.add("group");
mainGroup.orientation = "column";
mainGroup.alignChildren = "fill";
mainGroup.spacing = 12;
mainGroup.margins = 0;

var grpPage = mainGroup.add("panel", undefined, "Page & Options");
grpPage.orientation = "column";
grpPage.alignChildren = "left";
grpPage.margins = 10;
grpPage.spacing = 6;

grpPage.add("statictext", undefined, "PAGE À MODIFIER");

var pageList = [];
for (var i = 0; i < doc.pages.length; i++){
pageList.push("Page " + doc.pages[i].name);
}

var ddPage = grpPage.add("dropdownlist", undefined, pageList);
ddPage.selection = 0;

var chkAllPages = grpPage.add("checkbox", undefined, "Appliquer à toutes les pages");
chkAllPages.value = false;

grpPage.add("statictext", undefined, "Dupliquer la page (nombre) :");
var inputDup = grpPage.add("edittext", undefined, "0");
inputDup.characters = 5;

var grpText = mainGroup.add("panel", undefined, "Champs texte");
grpText.orientation = "column";
grpText.alignChildren = "left";
grpText.margins = 10;
grpText.spacing = 6;

function addField(parent, label, multi){
parent.add("statictext", undefined, label);
var f;
if(multi){
f = parent.add("edittext", undefined, "", {multiline:true});
f.preferredSize = [400, 70];
} else {
f = parent.add("edittext", undefined, "");
f.characters = 50;
}
return f;
}

var inputDesign = addField(grpText, "DESIGN NAME");
var inputFamily = addField(grpText, "FAMILLY");
var inputSKU = addField(grpText, "SKU");

grpText.add("statictext", undefined, "ELECTRIFICATION");
var ddElec = grpText.add("dropdownlist", undefined, ["Sans","U.S","EU"]);
ddElec.selection = 0;

var inputFR = addField(grpText, "FR DESCRIPTION", true);
var inputUS = addField(grpText, "US DESCRIPTION", true);

grpText.add("statictext", undefined, "CUSTOM");
var ddCustom = grpText.add("dropdownlist", undefined, ["Sans","Custom"]);
ddCustom.selection = 0;

var grpFinish = mainGroup.add("panel", undefined, "Finitions");
grpFinish.orientation = "column";
grpFinish.alignChildren = "left";
grpFinish.margins = 10;
grpFinish.spacing = 10;

function buildList(){
var arr=["Sans"];
for(var i=0;i<itemsData.length;i++){
arr.push(itemsData[i].code + " - " + itemsData[i].nom);
}
return arr;
}

function addFinishUI(parent, label){
parent.add("statictext", undefined, label);

var row = parent.add("group");
row.orientation = "row";
row.alignChildren = "top";
row.spacing = 10;

var dd = row.add("dropdownlist", undefined, buildList());
dd.selection = 0;

var preview = row.add("image", undefined);
preview.preferredSize = [120,120];
preview.visible = false;

return {dd:dd, preview:preview};
}

var F1 = addFinishUI(grpFinish, "FINISH 1");
var F2 = addFinishUI(grpFinish, "FINISH 2");
var F3 = addFinishUI(grpFinish, "FINISH 3");
var F4 = addFinishUI(grpFinish, "FINISH 4");

function updatePreview(dropdown, preview){
if(!dropdown.selection || dropdown.selection.index === 0){
preview.visible = false;
return;
}

var item = itemsData[dropdown.selection.index - 1];
var f = resolveImagePath(item.image, doc, csvFile);

if(f.exists){
preview.image = ScriptUI.newImage(f);
preview.visible = true;
} else {
preview.visible = false;
}
}

F1.dd.onChange = function(){ updatePreview(F1.dd, F1.preview); };
F2.dd.onChange = function(){ updatePreview(F2.dd, F2.preview); };
F3.dd.onChange = function(){ updatePreview(F3.dd, F3.preview); };
F4.dd.onChange = function(){ updatePreview(F4.dd, F4.preview); };

function loadPageData(page){

function resetFinish(F){
F.dd.selection = 0;
F.preview.visible = false;
}

resetFinish(F1);
resetFinish(F2);
resetFinish(F3);
resetFinish(F4);

inputDesign.text = "";
inputFamily.text = "";
inputSKU.text = "";
inputFR.text = "";
inputUS.text = "";

ddElec.selection = 0;
ddCustom.selection = 0;

function getFrame(name){
var tf = page.textFrames.itemByName(name);
return (tf.isValid) ? tf.contents : "";
}

inputDesign.text = getFrame("DESIGN_NAME");
inputFamily.text = getFrame("FAMILLY");
inputSKU.text = getFrame("SKU");
inputFR.text = getFrame("FR_DESCRIPTION");
inputUS.text = getFrame("US_DESCRIPTION");

var elec = getFrame("ELECTRIFICATION");
if(elec.indexOf("U.S") !== -1) ddElec.selection = 1;
else if(elec.indexOf("E.U") !== -1) ddElec.selection = 2;

var custom = getFrame("CUSTOM");
ddCustom.selection = (custom === "CUSTOM") ? 1 : 0;

function detectFinish(groupName){
var grp = page.pageItems.itemByName(groupName);
if(!grp.isValid || !grp.visible) return 0;

var txt = "";
var it = grp.allPageItems;

for(var i=0;i<it.length;i++){
if(it[i].name === "texte" && it[i] instanceof TextFrame){
txt = it[i].contents;
break;
}
}

if(!txt) return 0;

for(var i=0;i<itemsData.length;i++){
if(txt.indexOf(itemsData[i].code) !== -1) return i+1;
}

return 0;
}

F1.dd.selection = detectFinish("FINISH_1");
F2.dd.selection = detectFinish("FINISH_2");
F3.dd.selection = detectFinish("FINISH_3");
F4.dd.selection = detectFinish("FINISH_4");

updatePreview(F1.dd, F1.preview);
updatePreview(F2.dd, F2.preview);
updatePreview(F3.dd, F3.preview);
updatePreview(F4.dd, F4.preview);
}

ddPage.onChange = function(){
loadPageData(doc.pages[ddPage.selection.index]);
};

loadPageData(doc.pages[ddPage.selection.index]);

function fillFrame(page, name, val){
var tf = page.textFrames.itemByName(name);
if(tf.isValid) tf.contents = val;
}

function applyFinish(page, groupName, selectionIndex){
var grp = page.pageItems.itemByName(groupName);
if(!grp.isValid) return;

var textFrame = null;
var imageFrame = null;

var it = grp.allPageItems;
for(var i=0; i<it.length; i++){
if(it[i].name === "texte" && it[i] instanceof TextFrame){
textFrame = it[i];
}
if(it[i].name === "image" && it[i] instanceof Rectangle){
imageFrame = it[i];
}
}

if(selectionIndex === 0){
grp.visible = false;
if(textFrame) textFrame.contents = "";
if(imageFrame){
try{
while(imageFrame.allGraphics.length){
imageFrame.allGraphics[0].remove();
}
}catch(e){}
}
return;
}

grp.visible = true;

var item = itemsData[selectionIndex - 1];
if(!item) return;

if(textFrame){
textFrame.contents = item.code + " - " + item.nom;
}

if(imageFrame){
var f = resolveImagePath(item.image, doc, csvFile);
if(f.exists){
try{
while(imageFrame.allGraphics.length){
imageFrame.allGraphics[0].remove();
}
}catch(e){}

imageFrame.place(f);
imageFrame.fit(FitOptions.FILL_PROPORTIONALLY);
imageFrame.fit(FitOptions.CENTER_CONTENT);
}
}
}

function fillCustom(page){
var tf = page.textFrames.itemByName("CUSTOM");
if(!tf.isValid) return;

tf.contents = (ddCustom.selection.index === 1) ? "CUSTOM" : "";
}

function applyToPage(page) {

fillFrame(page, "DESIGN_NAME", inputDesign.text);
fillFrame(page, "FAMILLY", inputFamily.text);
fillFrame(page, "SKU", inputSKU.text);

var elecText = "";
if (ddElec.selection.index === 1) elecText = "U.S electrification";
if (ddElec.selection.index === 2) elecText = "E.U electrification";
fillFrame(page, "ELECTRIFICATION", elecText);

fillFrame(page, "FR_DESCRIPTION", inputFR.text);
fillFrame(page, "US_DESCRIPTION", inputUS.text);

applyFinish(page, "FINISH_1", F1.dd.selection.index);
applyFinish(page, "FINISH_2", F2.dd.selection.index);
applyFinish(page, "FINISH_3", F3.dd.selection.index);
applyFinish(page, "FINISH_4", F4.dd.selection.index);

fillCustom(page);
}

var btnGroup = dlg.add("group");
btnGroup.orientation = "row";
btnGroup.alignment = "right";
btnGroup.spacing = 10;

var btnApply = btnGroup.add("button", undefined, "Appliquer");
var btnCancel = btnGroup.add("button", undefined, "Annuler");
var btnOK = btnGroup.add("button", undefined, "OK");

btnApply.onClick = function () {
var page = doc.pages[ddPage.selection.index];
applyToPage(page);
alert("Modifications appliquées à la page " + page.name);
};

btnCancel.onClick = function () {
dlg.close(0);
};

//btnOK.onClick = function () {
// var page = doc.pages[ddPage.selection.index];
// applyToPage(page);
// dlg.close(1);
//};

btnOK.onClick = function () {
var page = doc.pages[ddPage.selection.index];
applyToPage(page);

// --- DUPLICATION ICI ---
var dupCount = parseInt(inputDup.text, 10);
if(isNaN(dupCount) || dupCount < 0) dupCount = 0;

var basePage = page;
var pagesToProcess = [];

if(chkAllPages.value){
for(var i = 0; i < doc.pages.length; i++){
pagesToProcess.push(doc.pages[i]);
}
} else {
pagesToProcess.push(basePage);
}

if(dupCount > 0){
for(var d = 0; d < dupCount; d++){
var newPage = basePage.duplicate(LocationOptions.AFTER, basePage);
pagesToProcess.push(newPage);
basePage = newPage;
}
}

if(chkAllPages.value){
pagesToProcess = [];
for(var i = 0; i < doc.pages.length; i++){
pagesToProcess.push(doc.pages[i]);
}
}

alert("Fiche produit mise à jour !");
// NE PAS fermer la palette
};
dlg.show();


 

1 reply

Community Expert
April 12, 2026

In my experience this usually isn’t a dialog issue.

Because you’re using a palette (modeless window), the script continues after dlg.show() and then exits, which can kill the UI unless the engine is persisted.

Adding this at the very top has for me fixed the “dialog closes instantly” behaviour:

#targetengine "product_sheet_session"

A couple of small improvements you might also find useful:

  • If you rerun the script, the palette can duplicate you might be able to store and reuse it globally
  • Use app.activeDocument inside handlers to avoid old document references
  • You can also force page navigation with:
    • app.activeWindow.activePage = targetPage;

       

For example:

// Reuse / close existing palette
var oldWin = $.global["product_sheet_dlg"];
if (oldWin && oldWin instanceof Window) {
    oldWin.close();
}

var dlg = new Window("palette", "Fiche produit");
$.global["product_sheet_dlg"] = dlg;

 

btnApply.onClick = function () {
    var doc = app.activeDocument;
    var page = doc.pages[ddPage.selection.index];
    applyToPage(page);
};
ddPage.onChange = function(){
    var targetPage = app.activeDocument.pages[ddPage.selection.index];
    loadPageData(targetPage);
    app.activeWindow.activePage = targetPage;
};