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);
}
}
... View more