Skip to main content
New Participant
May 15, 2025
Question

This Script Works for Indesign 2025 on Windows but Does not Work for 2021 On Mac

  • May 15, 2025
  • 5 replies
  • 482 views

Hello Guys this Script I made for creating .csv files froms Indesign Works perfectly with
Indesign 2025 on Windows 
but Does Not Work for
InDesign 2021 on Mac


Here is the code

#target indesign

(function () {
var dlg = new Window("dialog", "Generate QR CSV for SmartStream");

dlg.orientation = "column";
dlg.alignChildren = "left";

dlg.add("statictext", undefined, "From:");
var fromInput = dlg.add("edittext", undefined, "1");
fromInput.characters = 10;

dlg.add("statictext", undefined, "To:");
var toInput = dlg.add("edittext", undefined, "1000");
toInput.characters = 10;

dlg.add("statictext", undefined, "Leading Zeros:");
var zeroInput = dlg.add("edittext", undefined, "4");
zeroInput.characters = 10;

dlg.add("statictext", undefined, "Prefix (e.g., https://www.xymo.co/p/078873-05|050725|):");
var prefixInput = dlg.add("edittext", undefined, "");
prefixInput.characters = 40;

dlg.add("statictext", undefined, "Prefix Font Size (pt):");
var fontSizeDropdown = dlg.add("dropdownlist", undefined, [
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13"
]);
fontSizeDropdown.selection = 6; // Default: 10pt

dlg.add("statictext", undefined, "CSV File Name (no extension):");
var fileNameInput = dlg.add("edittext", undefined, "qr_codes");
fileNameInput.characters = 30;

var generateBtn = dlg.add("button", undefined, "Create .CSV", { name: "ok" });

generateBtn.onClick = function () {
try {
var from = parseInt(fromInput.text, 10);
var to = parseInt(toInput.text, 10);
var zeroPad = parseInt(zeroInput.text, 10);
var rawPrefix = String(prefixInput.text);
var prefix = rawPrefix; // for CSV
var cleanedPrefix = rawPrefix.replace(/^https:\/\/www\.xymo\.co\/p\//, ""); // for document
var fontSize = parseInt(fontSizeDropdown.selection.text, 10);
var fileName = String(fileNameInput.text).replace(/^\s+|\s+$/g, '');

if (
isNaN(from) || isNaN(to) || isNaN(zeroPad) || isNaN(fontSize) ||
from > to || fileName === ""
) {
alert("Invalid input. Please verify all fields.");
return;
}

// Build CSV
var csvLines = [];
csvLines.push("QTY,QR Text Line,|,#QRcodes");

var qty = 1;
for (var i = from; i <= to; i++) {
var num = i.toString();
while (num.length < zeroPad) num = "0" + num;
var fullCode = prefix + num;
csvLines.push(qty + "," + fullCode + ",|," + fullCode + "|");
qty++;
}

// File setup
var safeName = fileName.toLowerCase();
var fullFileName = safeName.indexOf(".csv") === safeName.length - 4 ? fileName : fileName + ".csv";

var desktopFolder = Folder.desktop;
var filePath = desktopFolder.fsName + "/" + fullFileName;
var file = new File(filePath);

// Save CSV
if (file.open("w")) {
file.encoding = "UTF-8";
file.write(csvLines.join("\n"));
file.close();
} else {
alert("Failed to write file:\n" + file.fsName);
return;
}

// Add cleaned prefix to document
var doc = app.activeDocument;
var page = doc.pages[0];
var marginPrefs = doc.marginPreferences;
var bounds = page.bounds;

var top = bounds[0] + marginPrefs.top;
var left = bounds[1] + marginPrefs.left;

var prefixFrame = page.textFrames.add();
prefixFrame.geometricBounds = [top, left, top + 30, left + 300];
prefixFrame.contents = cleanedPrefix;

var text = prefixFrame.parentStory.texts[0];
text.appliedFont = "Arial";
text.fontStyle = "Regular";
text.pointSize = fontSize;

// Try to open CSV with SmartStream
try {
file.execute();
} catch (e) {
alert("CSV saved, but could not auto-open in SmartStream.\nFile path:\n" + file.fsName);
dlg.close();
return;
}

alert("CSV created, SmartStream launched, and cleaned prefix added to document.");
dlg.close();

} catch (e) {
alert("Script error: " + e.message);
}
};

dlg.center();
dlg.show();
})();

5 replies

rob day
Community Expert
May 16, 2025

Hi @hector_2413, Not sure if this helps, here’s your dialog using the dialogColumns class—the results get passed to the main() function:

var f, t, z, p, pre, ic, fn;
makeDialog();
function makeDialog(){
    var theDialog = app.dialogs.add({name:"Enter a Number", canCancel:true});
    with(theDialog.dialogColumns.add()){
        staticTexts.add({staticLabel:"From:"});
        staticTexts.add({staticLabel:"To:"});
        staticTexts.add({staticLabel:"Leading Zeros:"});
        staticTexts.add({staticLabel:"Prefix (e.g., https://www.xymo.co/p/078873-05|050725|):"});
        staticTexts.add({staticLabel:"Prefix Font Size (pt):"});
        staticTexts.add({staticLabel:"CSV File Name (no extension):"});
    }
    with(theDialog.dialogColumns.add()){
        f = realEditboxes.add({editValue:1, maximumValue:1000, minimumValue:1, minWidth:100});
        t = realEditboxes.add({editValue:1000, maximumValue:5000, minimumValue:0, minWidth:100});
        z = realEditboxes.add({editValue:4, maximumValue:5, minimumValue:1, minWidth:100});
        pre = textEditboxes.add({editContents:"", minWidth:150});
        ic = integerComboboxes.add({stringList:["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"], editValue:10, maximumValue:100, minimumValue:0, smallNudge:1, largeNudge:10, minWidth:100});
        fn = textEditboxes.add({editContents:"qr_codes", minWidth:150});
    }
    if(theDialog.show() == true){
        f = f.editValue;
        t = t.editValue;
        z = z.editValue;
        pre = pre.editContents;
        ic = ic.editValue;
        fn = fn.editContents;
        main()
	}else{
        theDialog.destroy();
    }
}

/**
* Script to run.
* @ return void 
*/

function main(){
    alert("Results\rFrom: " + f + "\rTo: "+ t+ "\rZeros: "+ z + "\rPrefix: "+ pre+ "\rFont Size: "+ ic+ "\rFile Name: "+ fn);
    //script code here
}

 

 

 

 

rob day
Community Expert
May 15, 2025

this Script I made for creating .csv files froms Indesign

 

Hi @hector_2413 , Generally I use InDesign’s built in dialog class for simple dialogs like yours, so I can’t help with your code. See the API Dialog, DialogColumn classes.

 

I also get the error using CC2021.

 

For building ScriptUI class dialogs I use this great online dialog builder:

https://scriptui.joonas.me/

 

Robert at ID-Tasker
Brainiac
May 15, 2025

@hector_2413

 

HFS / POSIX paths problem? 

 

Kasyan Servetsky
Brainiac
May 15, 2025

On my side, while running your original version, both on Mac (2024, 2025) & Windows (2024), I get the following error:

The problem is you can't modify a doc while the modal dialog is open.
Here's my version:

#target indesign

makeDialog();

function makeDialog() {
	var dlg = new Window("dialog", "Generate QR CSV for SmartStream");

	dlg.orientation = "column";
	dlg.alignChildren = "left";

	dlg.add("statictext", undefined, "From:");
	var fromInput = dlg.add("edittext", undefined, "1");
	fromInput.characters = 10;

	dlg.add("statictext", undefined, "To:");
	var toInput = dlg.add("edittext", undefined, "1000");
	toInput.characters = 10;

	dlg.add("statictext", undefined, "Leading Zeros:");
	var zeroInput = dlg.add("edittext", undefined, "4");
	zeroInput.characters = 10;

	dlg.add("statictext", undefined, "Prefix (e.g., https://www.xymo.co/p/078873-05|050725|):");
	var prefixInput = dlg.add("edittext", undefined, "");
	prefixInput.characters = 40;

	dlg.add("statictext", undefined, "Prefix Font Size (pt):");
	var fontSizeDropdown = dlg.add("dropdownlist", undefined, [
		"4", "5", "6", "7", "8", "9", "10", "11", "12", "13"
	]);
	fontSizeDropdown.selection = 6; // Default: 10pt

	dlg.add("statictext", undefined, "CSV File Name (no extension):");
	var fileNameInput = dlg.add("edittext", undefined, "qr_codes");
	fileNameInput.characters = 30;

	var generateBtn = dlg.add("button", undefined, "Create .CSV", { name: "ok" });

	generateBtn.onClick = function () {
		// the vars in this handler are global to make them available in the main function
		from = parseInt(fromInput.text, 10);
		to = parseInt(toInput.text, 10);
		zeroPad = parseInt(zeroInput.text, 10);
		rawPrefix = String(prefixInput.text);
		prefix = rawPrefix; // for CSV
		cleanedPrefix = rawPrefix.replace(/^https:\/\/www\.xymo\.co\/p\//, ""); // for document
		fontSize = parseInt(fontSizeDropdown.selection.text, 10);
		fileName = String(fileNameInput.text).replace(/^\s+|\s+$/g, '');
		
		dlg.close(1);
	};

	dlg.center();
	
	var showDialog = dlg.show();
	if (showDialog == 1) {
		main();
	}
}

function main() {
	try {
		if (
			isNaN(from) || isNaN(to) || isNaN(zeroPad) || isNaN(fontSize) ||
			from > to || fileName === ""
		) {
			alert("Invalid input. Please verify all fields.");
			return;
		}

		// Build CSV
		var csvLines = [];
		csvLines.push("QTY,QR Text Line,|,#QRcodes");

		var qty = 1;
		for (var i = from; i <= to; i++) {
			var num = i.toString();
			while (num.length < zeroPad) num = "0" + num;
			var fullCode = prefix + num;
			csvLines.push(qty + "," + fullCode + ",|," + fullCode + "|");
			qty++;
		}

		// File setup
		var safeName = fileName.toLowerCase();
		var fullFileName = safeName.indexOf(".csv") === safeName.length - 4 ? fileName : fileName + ".csv";

		var desktopFolder = Folder.desktop;
		var filePath = desktopFolder.fsName + "/" + fullFileName;
		var file = new File(filePath);

		// Save CSV
		if (file.open("w")) {
			file.encoding = "UTF-8";
			file.write(csvLines.join("\n"));
			file.close();
		} else {
			alert("Failed to write file:\n" + file.fsName);
			return;
		}

		// Add cleaned prefix to document
		var doc = app.activeDocument;
		var page = doc.pages[0];
		var marginPrefs = doc.marginPreferences;
		var bounds = page.bounds;

		var top = bounds[0] + marginPrefs.top;
		var left = bounds[1] + marginPrefs.left;

		var prefixFrame = page.textFrames.add();
		prefixFrame.geometricBounds = [top, left, top + 30, left + 300];
		prefixFrame.contents = cleanedPrefix;

		var text = prefixFrame.parentStory.texts[0];
		text.appliedFont = "Arial";
		text.fontStyle = "Regular";
		text.pointSize = fontSize;

		// Try to open CSV with SmartStream
		try {
			file.execute();
		} catch (e) {
			alert("CSV saved, but could not auto-open in SmartStream.\nFile path:\n" + file.fsName);
			dlg.close();
			return;
		}

		alert("CSV created, SmartStream launched, and cleaned prefix added to document.");

	} catch (e) {
		alert("Script error: " + e.message + " - line " + e.line);
	}
}

 

 

 

m1b
Community Expert
May 15, 2025

Hi @hector_2413 I'm running MacOS 15.4.1, Indesign 20.3.1 and your script worked okay as far as I could tell. It generated a csv file with my test input. What error do you get on Mac?

- Mark

Community Expert
May 15, 2025

Can't they put it into a 2021 folder or something? Or am I imagining this trick?

Probably the other way around... 

 

 

m1b
Community Expert
May 15, 2025

Yes! @Eugene Tyson but I can't remember how it works exactly. I *think* you make a "2021" folder—or is it something like "version 19" folder?—in the normal "Scripts Panel" folder and Indesign will use that version's scripting API. @rob day I think you showed me this once? Or @Laubender ?

- Mark