I can't get my script to create a new document
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();