• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
6

I can't get my script to create a new document

Community Beginner ,
Aug 26, 2023 Aug 26, 2023

Copy link to clipboard

Copied

I'm trying to make a script that creates a new document whose page size, bleed amount, and slug amout are determined by parameteres entered by the user. The reason I need a script for this rather than the normal "New Document" dialogue is because the page size has to be adjusted by a certain factor.

 

I have very little scripting knowledge, and the vast majority of my code was written by ChatGPT. As far as ChatGPT is concerned the script should execute without issue. In the end, I think ChatGPT even got fed up and told me to look for the answer elsewhere.

 

My dialogue box comes up as it should, I can enter and store the values, but when I click "OK", the box closes and nothing happens. My script is as bellow. Any help will be greatly appreciated.

// Function to add aligned static text labels and input fields
function addAlignedInputRow(labelText, defaultInputValue, units, parent) {
    var group = parent.add("group");
    group.alignChildren = "left";

    var label = group.add("statictext", undefined, labelText);
    label.characters = 10;

    var input = group.add("edittext", undefined, defaultInputValue, {multiline: false, number: true});
    input.characters = 4;

    var unitsLabel = group.add("statictext", undefined, units);

    return input;
}

stretchFactor = 0.995;

// Simple SEG calculator
var dialog = new Window("dialog", "New tension fabric");
dialog.orientation = "column";
dialog.alignChildren = "left";

var widthInput = addAlignedInputRow("Width:", "", "mm", dialog);
var heightInput = addAlignedInputRow("Height:", "", "mm", dialog);
var scaleInput = addAlignedInputRow("Scale", "100", "%", dialog);

// Dropdown control for Beading
var beadingGroup = dialog.add("group");
beadingGroup.alignChildren = "left"; // Align children to the left within the group
var beadingLabel = beadingGroup.add("statictext", undefined, "Beading:");
beadingLabel.characters = 10; // Adjust the width of the label as needed
var beading = beadingGroup.add("dropdownlist");
beading.add("item", "14");
beading.add("item", "12");
beading.add("item", "10");
beading.add("item", "9");
beading.selection = 0;
var mmLabel = beadingGroup.add("statictext", undefined, "mm");

// Create a horizontal group for buttons
var buttonGroup = dialog.add("group");
buttonGroup.alignment = "center"; // Center align the buttons
var cancelButton = buttonGroup.add("button", undefined, "Cancel");
var okButton = buttonGroup.add("button", undefined, "OK");

// Event listener for the "Cancel" button
cancelButton.onClick = function() {
    dialog.close();
};

// Event listener for the "OK" button
okButton.onClick = function() {
    var width = parseFloat(widthInput.text);
    var height = parseFloat(heightInput.text);
    var scale = parseFloat(scaleInput.text) / 100;
    var selectedBeading = parseInt(beading.selection.text);

    var adjustedHeight = Math.round((height * stretchFactor) * scale);
    var adjustedHeight = Math.round((width * stretchFactor) * scale);

    // Create a new InDesign document with custom page size, bleed, and slug
    var newDocument = app.documents.add();
    var firstPage = newDocument.pages[0];
    firstPage.bounds = [0, 0, adjustedHeight, adjustedWidth]; // Set the page bounds

    app.documents.add();

    // Set bleed and slug values based on selectedBeading
    var bleedAmount = (selectedBeading * scale) + (10 * scale); // Bleed is 10 mm plus selectedBeading
    firstPage.documentBleedUniformSize = true;
    firstPage.documentBleedTopOffset = bleedAmount;
    firstPage.documentBleedBottomOffset = bleedAmount;
    firstPage.documentBleedInsideOrLeftOffset = bleedAmount;
    firstPage.documentBleedOutsideOrRightOffset = bleedAmount;

    // Slug is equal to selectedBeading
    var slugAmount = selectedBeading * scale;
    firstPage.slugTopOffset = slugAmount;
    firstPage.slugBottomOffset = slugAmount;
    firstPage.slugInsideOrLeftOffset = slugAmount;
    firstPage.slugOutsideOrRightOffset = slugAmount;

    dialog.close();
};

dialog.show();

 

TOPICS
Scripting

Views

147

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 26, 2023 Aug 26, 2023

Copy link to clipboard

Copied

While your dialog is displayed InDesign can't do anything. Change the dialog to a palette-type window (like an InDesign panel) and the script can continue to create a document. In this line:

var dialog = new Window("dialog", "New tension fabric");

Change 'dialog' to 'palette'.

 

That's the easy solution. It would have been more appropriate to collect all the values you need, close the dialog, then create the new document. As it is now, creating the document is part of the interface, which will cause all kinds of logistical problems. (Still amazing what ChatGPT comes up with.)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 26, 2023 Aug 26, 2023

Copy link to clipboard

Copied

Hi @kylep77360271 , Also, the part of the ChatGPT script that constructs the document is going to throw a bunch of errors—page.bounds are read only so you can’t change them, you would have to transform the page. Pages also don’t have bleed or slug properties, so all of those lines are going to throw an error (bleed and slug are document properties).

 

Here’s an example of capturing the dialog values, where I’ve declared the variables in the first line and the given them the values in the onClick function, then display them in an alert after the dialog closes:

 

 

 


//the global dialog variables
var width, height, scale, selectedBeading, adjustedHeight, adjustedWidth;

// Function to add aligned static text labels and input fields
function addAlignedInputRow(labelText, defaultInputValue, units, parent) {
    var group = parent.add("group");
    group.alignChildren = "left";

    var label = group.add("statictext", undefined, labelText);
    label.characters = 10;

    var input = group.add("edittext", undefined, defaultInputValue, {multiline: false, number: true});
    input.characters = 4;

    var unitsLabel = group.add("statictext", undefined, units);

    return input;
}

stretchFactor = 0.995;

// Simple SEG calculator
var dialog = new Window("dialog", "New tension fabric");
dialog.orientation = "column";
dialog.alignChildren = "left";

var widthInput = addAlignedInputRow("Width:", "", "mm", dialog);
var heightInput = addAlignedInputRow("Height:", "", "mm", dialog);
var scaleInput = addAlignedInputRow("Scale", "100", "%", dialog);

// Dropdown control for Beading
var beadingGroup = dialog.add("group");
beadingGroup.alignChildren = "left"; // Align children to the left within the group
var beadingLabel = beadingGroup.add("statictext", undefined, "Beading:");
beadingLabel.characters = 10; // Adjust the width of the label as needed
var beading = beadingGroup.add("dropdownlist");
beading.add("item", "14");
beading.add("item", "12");
beading.add("item", "10");
beading.add("item", "9");
beading.selection = 0;
var mmLabel = beadingGroup.add("statictext", undefined, "mm");

// Create a horizontal group for buttons
var buttonGroup = dialog.add("group");
buttonGroup.alignment = "center"; // Center align the buttons
var cancelButton = buttonGroup.add("button", undefined, "Cancel");
var okButton = buttonGroup.add("button", undefined, "OK");

// Event listener for the "Cancel" button
cancelButton.onClick = function() {
    dialog.close();
};

// Event listener for the "OK" button
okButton.onClick = function() {
    //set the returned dialog values
    width = parseFloat(widthInput.text);
    height = parseFloat(heightInput.text);
    scale = parseFloat(scaleInput.text) / 100;
    selectedBeading = parseInt(beading.selection.text);

    adjustedHeight = Math.round((height * stretchFactor) * scale);
    adjustedWidth = Math.round((width * stretchFactor) * scale);
    dialog.close();
};

dialog.show();

main()
function main(){

    alert("Returned Dialog Values: \r" + width + "\r" + height +"\r"+scale + "\r"+selectedBeading)


/* remove comments to  throw errors

    var newDocument = app.documents.add();
    var firstPage = newDocument.pages[0];
    //error bounds is read only
    firstPage.bounds = [0, 0, adjustedHeight, adjustedWidth]; // Set the page bounds

    app.documents.add();

    // Set bleed and slug values based on selectedBeading
    var bleedAmount = (selectedBeading * scale) + (10 * scale); // Bleed is 10 mm plus selectedBeading
    
    //error pages don’t have bleed propertie
    firstPage.documentBleedUniformSize = true;
    firstPage.documentBleedTopOffset = bleedAmount;
    firstPage.documentBleedBottomOffset = bleedAmount;
    firstPage.documentBleedInsideOrLeftOffset = bleedAmount;
    firstPage.documentBleedOutsideOrRightOffset = bleedAmount;

    // Slug is equal to selectedBeading
    var slugAmount = selectedBeading * scale;
    //pages don’t have slug prperties
    firstPage.slugTopOffset = slugAmount;
    firstPage.slugBottomOffset = slugAmount;
    firstPage.slugInsideOrLeftOffset = slugAmount;
    firstPage.slugOutsideOrRightOffset = slugAmount; 
    
end comments*/

}

 

 

 

Screen Shot 12.pngScreen Shot 13.png

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 28, 2023 Aug 28, 2023

Copy link to clipboard

Copied

Hi @kylep77360271 , Try this:

 

//the global dialog variables
var width, height, scale, selectedBeading, adjustedHeight, adjustedWidth;

// Function to add aligned static text labels and input fields
function addAlignedInputRow(labelText, defaultInputValue, units, parent) {
    var group = parent.add("group");
    group.alignChildren = "left";
    var label = group.add("statictext", undefined, labelText);
    label.characters = 10;
    var input = group.add("edittext", undefined, defaultInputValue, {multiline: false, number: true});
    input.characters = 4;
    //var unitsLabel = group.add("statictext", undefined, units);
    return input;
}

stretchFactor = 0.995;

// Simple SEG calculator
var dialog = new Window("dialog", "New tension fabric");
dialog.orientation = "column";
dialog.alignChildren = "left";

var widthInput = addAlignedInputRow("Width:", "", "mm", dialog);
var heightInput = addAlignedInputRow("Height:", "", "mm", dialog);
var scaleInput = addAlignedInputRow("Scale", "100", "%", dialog);

// Dropdown control for Beading
var beadingGroup = dialog.add("group");
beadingGroup.alignChildren = "left"; // Align children to the left within the group
var beadingLabel = beadingGroup.add("statictext", undefined, "Beading:");
beadingLabel.characters = 10; // Adjust the width of the label as needed
var beading = beadingGroup.add("dropdownlist");
beading.add("item", "14");
beading.add("item", "12");
beading.add("item", "10");
beading.add("item", "9");
beading.selection = 0;
var mmLabel = beadingGroup.add("statictext", undefined, "mm");

// Create a horizontal group for buttons
var buttonGroup = dialog.add("group");
buttonGroup.alignment = "center"; // Center align the buttons
var cancelButton = buttonGroup.add("button", undefined, "Cancel");
var okButton = buttonGroup.add("button", undefined, "OK");

// Event listener for the "Cancel" button
cancelButton.onClick = function() {
    dialog.close();
};

// Event listener for the "OK" button
okButton.onClick = function() {
    //set the returned dialog values
    width = parseFloat(widthInput.text);
    height = parseFloat(heightInput.text);
    scale = parseFloat(scaleInput.text) / 100;
    selectedBeading = parseInt(beading.selection.text);

    adjustedHeight = Math.round((height * stretchFactor) * scale);
    adjustedWidth = Math.round((width * stretchFactor) * scale);
    dialog.close();
};

dialog.show();

main()
function main(){
    alert("Returned Dialog Values: \r" + adjustedWidth + "\r" + adjustedHeight +"\r"+scale + "\r"+selectedBeading)

    app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
    //the bleed is larger than the slug???
    var bleedAmount = (selectedBeading * scale) + (10 * scale); // Bleed is 10 mm plus selectedBeading
    var slugAmount = selectedBeading * scale;
    var currdate = new Date
    var ct =  "Temp-" + currdate.getTime();
    var dp = makeDocPreset(ct);
    dp.properties = {facingPages:false, 
                    pageHeight:adjustedHeight, 
                    pageWidth:adjustedWidth, 
                    top:0, 
                    bottom:0, 
                    left:0, 
                    right:0, 
                    documentBleedUniformSize:true, 
                    documentBleedTopOffset:bleedAmount, 
                    documentSlugUniformSize:true, 
                    documentSlugTopOffset:slugAmount}

    //make the new doc
    var doc = app.documents.add(true, dp);
    doc.viewPreferences.properties = {horizontalMeasurementUnits:MeasurementUnits.MILLIMETERS, verticalMeasurementUnits:MeasurementUnits.MILLIMETERS}
    dp.remove();
    app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;
}


/**
* Makes a new document preset 
* @ param preset name name 
* @ return the new preset 
*/
function makeDocPreset(n){
    if (app.documentPresets.itemByName(n).isValid) {
        return app.documentPresets.itemByName(n);
    } else {
        return app.documentPresets.add({name:n});
    }
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 29, 2023 Aug 29, 2023

Copy link to clipboard

Copied

LATEST

Thanks for the help, all. The script now works as expected.


@Peter Kahrel, changing 'dialog' to 'palette' didn't work for me, but as you said, I should gather the variables, close the dialogue, then create the new document. I've rewritten my code to do just that (I'd rather do it right than bodge it).

 

@rob day, I did notice that and thought it was odd, but I just took ChatGPT's word for it. That's all sorted now.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines