Search code examples
javascriptdatabaseadobe-indesignauto-generate

Why is my InDesign Javascript not fully working?


I am creating a small script to be used within InDesign to be able to read a .txt file and create multiple .indd files of differing WxH and bleed .etc. it's a Javascript .js file.

So far I managed to get a script fully working but when i try to pretty it up using the Scrip UI features it just seems to die. No errors or crashing, just nothing.

Here is the problematic code. The working version is basically the same but runs without the UI features. If anyone can tell me where i'm going wrong that would be appreciated. I've been up and down this code 100 times and can't get the files to generate.

Incase it helps: the idea is to be able to receive spreadsheets from the client and provide the studio with a set of "pre-setup" indesign files. Hence the need for bleed settings and a web version.

I'm a few weeks into learning any kind of coding as i'm coming from a prepress artworking background. So ANY advise related to the query or not is greatly appreciated.

var dialog = new Window("dialog"); 
    dialog.text = "Doc Builder"; 
    dialog.orientation = "column"; 
    dialog.alignChildren = ["left","top"]; 
    dialog.spacing = 10; 
    dialog.margins = 16; 

// TXTPANEL
// ========
var txtPanel = dialog.add("panel", undefined, undefined, {name: "txtPanel"}); 
    txtPanel.orientation = "row"; 
    txtPanel.alignChildren = ["left","top"]; 
    txtPanel.spacing = 0; 
    txtPanel.margins = 10; 

var txtCopy = txtPanel.add("statictext", undefined, undefined, {name: "txtCopy"}); 
    txtCopy.text = "Select your txt file:"; 
    txtCopy.preferredSize.width = 200; 

var txtButton = txtPanel.add("button", undefined, undefined, {name: "txtButton"}); 
    txtButton.text = "Select"; 

    txtButton.onClick = function () {var file = File.openDialog("Select Your Text File (Must be a .txt format)", undefined, false);}

// FOLDERPANEL
// ===========
var folderPanel = dialog.add("panel", undefined, undefined, {name: "folderPanel"}); 
    folderPanel.orientation = "row"; 
    folderPanel.alignChildren = ["left","top"]; 
    folderPanel.spacing = 0; 
    folderPanel.margins = 10; 

var folderCopy = folderPanel.add("statictext", undefined, undefined, {name: "folderCopy"}); 
    folderCopy.text = "Select a folder destination"; 
    folderCopy.preferredSize.width = 200; 

var folderButton = folderPanel.add("button", undefined, undefined, {name: "folderButton"}); 
    folderButton.text = "Select"; 

    folderButton.onClick = function () {var folder = Folder.selectDialog("Select the folder where the new documents should be saved");}

// COLOURSPACEPANEL
// ================
var colourSpacePanel = dialog.add("panel", undefined, undefined, {name: "colourSpacePanel"}); 
    colourSpacePanel.text = "Colour Space"; 
    colourSpacePanel.preferredSize.height = 80; 
    colourSpacePanel.orientation = "row"; 
    colourSpacePanel.alignChildren = ["left","center"]; 
    colourSpacePanel.spacing = 50; 
    colourSpacePanel.margins = 10; 

var printButton = colourSpacePanel.add("radiobutton", undefined, undefined, {name: "printButton"}); 
    printButton.text = "Print"; 
    printButton.value = true; 

var webButton = colourSpacePanel.add("radiobutton", undefined, undefined, {name: "webButton"}); 
    webButton.text = "Web"; 

// BLEEDPANEL
// ==========
var bleedPanel = dialog.add("panel", undefined, undefined, {name: "bleedPanel"}); 
    bleedPanel.text = "Document Bleed"; 
    bleedPanel.orientation = "column"; 
    bleedPanel.alignChildren = ["left","top"]; 
    bleedPanel.spacing = 0; 
    bleedPanel.margins = 10; 

var bleedCopy = bleedPanel.add('edittext {properties: {name: "bleedCopy"}}'); 
    bleedCopy.text = "3mm"; 

// ENDPANEL
// ========
var endPanel = dialog.add("group", undefined, {name: "endPanel"}); 
    endPanel.orientation = "row"; 
    endPanel.alignChildren = ["left","bottom"]; 
    endPanel.spacing = 20; 
    endPanel.margins = 0; 

var buildButton = endPanel.add("button", undefined, undefined, {name: "buildButton"}); 
    buildButton.text = "Build"; 

var cancelButton = endPanel.add("button", undefined, undefined, {name: "cancelButton"}); 
    cancelButton.text = "Cancel"; 

buildButton.onClick = function ()
{
file.open("r");
var content = file.read().split("\n");

for (var i = 0; i < content.length; i++)
{
    var curLine = content[i].split("\t");
    var width = curLine[0];
    var height = curLine[1];
    var filename = curLine[2];
    docName = filename + "_" + height + "x" + width;
   
    try {
        var newDoc = app.documents.add(false);
        newDoc.documentPreferences.pageHeight = height;
        newDoc.documentPreferences.pageWidth = width;
        newDoc.documentPreferences.facingPages = false;

         newDoc.documentPreferences.properties =
            {
                documentBleedUniformSize : true,
                documentBleedTopOffset : docBleed
            };

        newDoc.save(new File(folder + "/" + docName + "mm.indd"));
        newDoc.close(SaveOptions.no)
        } catch(myError){}
}

}

dialog.show();


Tried various fixes: Changing onClick to onmousedown adjusting the sequence of some actions

The script will be visible in the "scripts" panel within InDesign. Once double clicked I want the one window to pop-up and all the parameters to be assigned. then once the user clicks "build"

It'll generate all the documents into the selected destination folder.

if you want to try running live data then you need to create a 3 column .txt file (i export from Excel) : Column A: Width, Column B: Height, Column C: Filename.


Solution

  • You have to decide if it should be a 'palette' or a 'dialog' window? They work differently. Dialog fetches the information from the interface and does work after the dialog window is closed (with the button 'Build' in this case). Palette stays on the screen constantly and can fetch some info and do the work every time you click on its buttons until you close the palette with the 'Cancel' button or some other way.

    Here is the fixed variant of the code that works as a palette:

    #targetengine 'session'
    
    var file, folder;
    
    var palette = new Window("palette");
        palette.text = "Doc Builder";
        palette.orientation = "column";
        palette.alignChildren = ["left","top"];
        palette.spacing = 10;
        palette.margins = 16;
    
    // TXTPANEL
    // ========
    var txtPanel = palette.add("panel", undefined, undefined, {name: "txtPanel"});
        txtPanel.orientation = "row";
        txtPanel.alignChildren = ["left","top"];
        txtPanel.spacing = 0;
        txtPanel.margins = 10;
    
    var txtCopy = txtPanel.add("statictext", undefined, undefined, {name: "txtCopy"});
        txtCopy.text = "Select your txt file:";
        txtCopy.preferredSize.width = 200;
    
    var txtButton = txtPanel.add("button", undefined, undefined, {name: "txtButton"});
        txtButton.text = "Select";
    
        txtButton.onClick = function () {file = File.openDialog("Select Your Text File (Must be a .txt format)", undefined, false);}
    
    // FOLDERPANEL
    // ===========
    var folderPanel = palette.add("panel", undefined, undefined, {name: "folderPanel"});
        folderPanel.orientation = "row";
        folderPanel.alignChildren = ["left","top"];
        folderPanel.spacing = 0;
        folderPanel.margins = 10;
    
    var folderCopy = folderPanel.add("statictext", undefined, undefined, {name: "folderCopy"});
        folderCopy.text = "Select a folder destination";
        folderCopy.preferredSize.width = 200;
    
    var folderButton = folderPanel.add("button", undefined, undefined, {name: "folderButton"});
        folderButton.text = "Select";
    
        folderButton.onClick = function () {folder = Folder.selectDialog("Select the folder where the new documents should be saved");}
    
    // COLOURSPACEPANEL
    // ================
    var colourSpacePanel = palette.add("panel", undefined, undefined, {name: "colourSpacePanel"});
        colourSpacePanel.text = "Colour Space";
        colourSpacePanel.preferredSize.height = 80;
        colourSpacePanel.orientation = "row";
        colourSpacePanel.alignChildren = ["left","center"];
        colourSpacePanel.spacing = 50;
        colourSpacePanel.margins = 10;
    
    var printButton = colourSpacePanel.add("radiobutton", undefined, undefined, {name: "printButton"});
        printButton.text = "Print";
        printButton.value = true;
    
    var webButton = colourSpacePanel.add("radiobutton", undefined, undefined, {name: "webButton"});
        webButton.text = "Web";
    
    // BLEEDPANEL
    // ==========
    var bleedPanel = palette.add("panel", undefined, undefined, {name: "bleedPanel"});
        bleedPanel.text = "Document Bleed";
        bleedPanel.orientation = "column";
        bleedPanel.alignChildren = ["left","top"];
        bleedPanel.spacing = 0;
        bleedPanel.margins = 10;
    
    var bleedCopy = bleedPanel.add('edittext {properties: {name: "bleedCopy"}}');
        bleedCopy.text = "3mm";
    
    // ENDPANEL
    // ========
    var endPanel = palette.add("group", undefined, {name: "endPanel"});
        endPanel.orientation = "row";
        endPanel.alignChildren = ["left","bottom"];
        endPanel.spacing = 20;
        endPanel.margins = 0;
    
    var buildButton = endPanel.add("button", undefined, undefined, {name: "buildButton"});
        buildButton.text = "Build";
    
    var cancelButton = endPanel.add("button", undefined, undefined, {name: "cancelButton"});
        cancelButton.text = "Cancel";
    
    cancelButton.onClick = function() { palette.close() }
    
    buildButton.onClick = function()
    {
        file.open("r");
        var content = file.read().split("\n");
    
        for (var i = 0; i < content.length; i++)
        {
            var curLine = content[i].split("\t");
            var width = curLine[0];
            var height = curLine[1];
            var filename = curLine[2];
            var docName = filename + "_" + height + "x" + width;
    
            try {
                var newDoc = app.documents.add(false);
                newDoc.documentPreferences.pageHeight = height;
                newDoc.documentPreferences.pageWidth = width;
                newDoc.documentPreferences.facingPages = false;
    
                newDoc.documentPreferences.properties =
                {
                    documentBleedUniformSize : true,
                    documentBleedTopOffset : parseFloat(bleedCopy.text)
                };
    
                newDoc.save(new File(folder + "/" + docName + "mm.indd"));
                newDoc.close(SaveOptions.no);
            } catch(myError){ alert(myError) }
        }
    
    }
    
    palette.show();