Skip to main content
TestriteVisual
Inspiring
March 22, 2023
Answered

Can anyone let me know what's wrong with my script for generating 3 rectangle objects?

  • March 22, 2023
  • 3 replies
  • 1456 views

Greetings,

I'm trying to acheive the following algorythm

Write a javascript for Adobe InDesign that will do the following:

Step 1: Make dialogue box pop up for the user to enter a value for "finished width" in inches, "finished height" in inches and "bleed" in inches

Step 2: The user press an OK button on the dialogue box

Step 3. A new document is created with "document width" equal to "finished width" plus two times "bleed" and "document height" equal to "finished height" plus two times "bleed"

Step 4: Create a rectangle object named "Bleed" with width equal to "document width", and height equal to "document height", and stroke equal to 0, and fill equal to RED, and transparency equal to 50 percent, and place it centered on the page, and align stroke to inside

Step 5: Create a rectangle object named "Finished Size" with width equal to "finished width", and height equal to "finished height", and linetype equal to DASHED, and stroke equal to 10pt, and stroke color equal to BLACK, and fill color equal to NONE, and transparency equal to 100 percent, and place it centered on the page

Step 6: Create a rectangle object named "Safe Area" with width equal to "finished width" minus 2, and height equal to "finished height" minus 2, and linetype equal to SOLID, and stroke equal to 5pt, and stroke color equal to GREEN, and fill color equal to NONE, and transparency equal to 100 percent, and place it centered on the page

 

and I have this script, but it just crashes InDesign after entering the values into the dialogue box.

// Prompt the user to enter the finished width, finished height, and bleed values
var finishedWidthIN = prompt("Enter the finished width (inches):");
var finishedHeightIN = prompt("Enter the finished height (inches):");
var bleedIN = prompt("Enter the bleed (inches):");

// Convert the entered values to points (1 inch = 72 points)
finishedWidth = finishedWidthIN * 72;
finishedHeight = finishedHeightIN * 72;
bleed = bleedIN * 72;

// Create a new document with the calculated dimensions
var doc = app.documents.add({
    documentPreferences: {
        pageWidth: finishedWidth + (2 * bleed),
        pageHeight: finishedHeight + (2 * bleed)
    }
});

// Create the Bleed rectangle
var bleedRect = doc.pages.item(0).rectangles.add({
    geometricBounds: [0, 0, doc.documentPreferences.pageHeight, doc.documentPreferences.pageWidth],
    strokeWeight: bleed,
    fillColor: "Red",
    transparencySettings: { blendingSettings: { opacity: 50 } },
    strokeAlignment: StrokeAlignment.INSIDE_ALIGNMENT
});

// Create the Finished Size rectangle
var finishedRect = doc.pages.item(0).rectangles.add({
    geometricBounds: [(doc.documentPreferences.pageHeight - finishedHeight) / 2, (doc.documentPreferences.pageWidth - finishedWidth) / 2, (doc.documentPreferences.pageHeight + finishedHeight) / 2, (doc.documentPreferences.pageWidth + finishedWidth) / 2],
    strokeWeight: 10,
    strokeColor: "Black",
    strokeType: [DashedStroke()],
    fillColor: "None",
    transparencySettings: { blendingSettings: { opacity: 100 } }
});

// Create the Safe Area rectangle
var safeAreaRect = doc.pages.item(0).rectangles.add({
    geometricBounds: [(doc.documentPreferences.pageHeight - (finishedHeight - 2)) / 2, (doc.documentPreferences.pageWidth - (finishedWidth - 2)) / 2, (doc.documentPreferences.pageHeight + (finishedHeight - 2)) / 2, (doc.documentPreferences.pageWidth + (finishedWidth - 2)) / 2],
    strokeWeight: 5,
    strokeColor: "Green",
    strokeType: [SolidStroke()],
    fillColor: "None",
    transparencySettings: { blendingSettings: { opacity: 100 } }
});

 

Please let me know if you can find what's causing it to crash. Thank you much!!!!

This topic has been closed for replies.
Correct answer rob day

Thanks, I had updated all the "red" callouts. In the last full code I posted "red" has been replaced with,

"C=15 M=100 Y=100 K=0",//RED

 

but I'm still having the problem regarding setting the color of each line in the text box. I think I'm not clear on how to properley WRITE the range values for using the 

itemByRange function. Any thoughts on that? Thank you


I think itemByRange might be over complicating things. You could set the contents then just set the color of line 2 and 4:

 

 

// Set the text content
graphicSpecs.contents = productName + " Graphic Template\r";
graphicSpecs.contents += "Document Size: " + pageWidthIN + "\" x " + pageHeightIN + "\"\r";
graphicSpecs.contents += "Finished Size: " + finishedWidthIN + "\" x " + finishedHeightIN + "\"\r";
graphicSpecs.contents += "Bleed: " + bleedIN + "\" on each of 4 sides\r";
graphicSpecs.contents += "100 DPI\rAll text should be converted to outlines";

//line 2
graphicSpecs.lines[1].texts[0].fillColor = "C=100 M=0 Y=0 K=0";
//line 4
graphicSpecs.lines[3].texts[0].fillColor = "C=15 M=100 Y=100 K=0"

 

 

Also, is the script going to be used by others? If so, how can you be sure all users will have the colors you are setting in their swatches panel?

3 replies

rob day
Community Expert
Community Expert
March 23, 2023

Also you might look at InDesign’s built in dialog class, which has the very useful .measurementEditboxes. A measurementEditbox can be set up as any default unit and returns the unit as points. Here’s a sample:

 

makeDialog();

//the returned dialog values
var pw, ph, bld;
function makeDialog(){
    var d = app.dialogs.add({name:"Document Dimensions", canCancel:true});
    with(d.dialogColumns.add()){
        //labels
        staticTexts.add({staticLabel:"Page Width:"});
        staticTexts.add({staticLabel:"Page Height:"});
        staticTexts.add({staticLabel:"Bleed:"});
    }
    with(d.dialogColumns.add()){
        //Inch measurement units
        pw = measurementEditboxes.add({editUnits:MeasurementUnits.INCHES, editValue:0, minWidth:90});
        ph = measurementEditboxes.add({editUnits:MeasurementUnits.INCHES, editValue:0, minWidth:90});
        bld = measurementEditboxes.add({editUnits:MeasurementUnits.INCHES, editValue:0, minWidth:90});
    }

    if(d.show() == true){
        //the results returned as Points
        pw = pw.editValue;
        ph = ph.editValue;
        bld = bld.editValue;
        makeDocument()
        d.destroy();
	}
}

function makeDocument(){
    
    alert("Dialog Results: \rPage Height: " + ph + "\rPage Width: " +pw+ "\rBleed: " +bld)
    
    app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
    
    //Make Document....
    
    app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;

}

 

TestriteVisual
Inspiring
March 24, 2023

My latest version of the script is working well, except I was trying to change the font color of each line in a text box box, using itemByRange, but I'm getting the error below after it gets to the second line of the paragraph. Thank you again for any help on getting this to work!! 🙂

 

This code below is the original code a had for the text box

// Set the text content
graphicSpecs.contents = productName + " Graphic Template\r" + "Document Size: " + pageWidthIN + "\" x " + pageHeightIN + "\"\r" + "Finished Size: " + finishedWidthIN + "\" x " + finishedHeightIN + "\"\r" + "Bleed: " + bleedIN + "\" on each of 4 sides\r"+ "100 DPI\rAll text should be converted to outlines";

 and screenshot below is the result it produces, just as all black text

 

This code below is what I updated it to, in attempt to change the color on each line of the text box. The error comes after outputting the second text line, but DOES NOT change the second line color to cyan C=100 M=0 Y=0 K=0

// Set the text content
graphicSpecs.contents = productName + " Graphic Template\r";
// Set the text color to black
graphicSpecs.characters.itemByRange(0, graphicSpecs.contents.length-1).fillColor = "Black";//BLACK

graphicSpecs.contents += "Document Size: " + pageWidthIN + "\" x " + pageHeightIN + "\"\r";
// Set the text color to CYAN
graphicSpecs.characters.itemByRange(graphicSpecs.contents.length-1, graphicSpecs.contents.length-1 + ("Document Size: " + pageWidthIN + "\" x " + pageHeightIN + "\"\r").length).fillColor = "C=100 M=0 Y=0 K=0";//CYAN

graphicSpecs.contents += "Finished Size: " + finishedWidthIN + "\" x " + finishedHeightIN + "\"\r";
// Set the text color to BLACK
graphicSpecs.characters.itemByRange(graphicSpecs.contents.length-1, graphicSpecs.contents.length-1 + ("Finished Size: " + finishedWidthIN + "\" x " + finishedHeightIN + "\"\r").length).fillColor = "Black";//BLACK

graphicSpecs.contents += "Bleed: " + bleedIN + "\" on each of 4 sides\r";
// Set the text color to RED
graphicSpecs.characters.itemByRange(graphicSpecs.contents.length-1, graphicSpecs.contents.length-1 + ("Bleed: " + bleedIN + "\" on each of 4 sides\r").length).fillColor = "C=15 M=100 Y=100 K=0";//RED

graphicSpecs.contents += "100 DPI\rAll text should be converted to outlines";
// Set the text color to BLACK
graphicSpecs.characters.itemByRange(graphicSpecs.contents.length-1, graphicSpecs.contents.length-1 + ("100 DPI\rAll text should be converted to outlines").length).fillColor = "Black";//BLACK

the code above outputs the resulting screen shot below

 

The code below is the full script with the itemByRange error

//set units
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

// Create a dialog box to prompt user for input
var myDialog = app.dialogs.add({name:"Product Information"});
with(myDialog.dialogColumns.add()){
    with(borderPanels.add()){
        with(dialogRows.add()){
            with(dialogColumns.add()){
                staticTexts.add({staticLabel:"Product Name:"});
            }
            with(dialogColumns.add()){
                var productNameField = textEditboxes.add({editContents:""});
            }
        }
        with(dialogRows.add()){
            with(dialogColumns.add()){
                staticTexts.add({staticLabel:"Finished Width (inches):"});
            }
            with(dialogColumns.add()){
                var finishedWidthField = textEditboxes.add({editContents:""});
            }
        }
        with(dialogRows.add()){
            with(dialogColumns.add()){
                staticTexts.add({staticLabel:"Finished Height (inches):"});
            }
            with(dialogColumns.add()){
                var finishedHeightField = textEditboxes.add({editContents:""});
            }
        }
        with(dialogRows.add()){
            with(dialogColumns.add()){
                staticTexts.add({staticLabel:"Bleed (inches):"});
            }
            with(dialogColumns.add()){
                var bleedField = textEditboxes.add({editContents:"1"});
            }
        }
    }
}
var myResult = myDialog.show();

// Get the values entered in the dialog box
if(myResult == true){
    var productName = productNameField.editContents;
    var finishedWidthIN = finishedWidthField.editContents;
    var finishedHeightIN = finishedHeightField.editContents;
    var bleedIN = bleedField.editContents;
    var safetyOffsetIN = 2;
    var pageWidthIN = Number(finishedWidthIN) + (2 * Number(bleedIN));
    var pageHeightIN = Number(finishedHeightIN) + (2 * Number(bleedIN));
}
   
// Convert the entered values to points (1 inch = 72 points)
//coerce numbers
    finishedWidth = Number(finishedWidthIN)*72;
    finishedHeight = Number(finishedHeightIN)*72;
    bleed = Number(bleedIN)*72;
    safetyOffset = Number(safetyOffsetIN)*72;
    finishedHeightStroke = 5;

    // Create a new document with the calculated dimensions
    var graphicTemplate = app.documents.add({
        documentPreferences: {
            pageWidth: finishedWidth + (2 * bleed),
            pageHeight: finishedHeight + (2 * bleed)
        }
    });

    graphicTemplate.marginPreferences.properties = {
        top : 0,
        left: 0,
        right: 0,
        bottom:0
    };


// Create the Bleed rectangle
var bleedRect = graphicTemplate.pages.item(0).rectangles.add({
    geometricBounds: [0, 0, graphicTemplate.documentPreferences.pageHeight, graphicTemplate.documentPreferences.pageWidth],
    strokeWeight: bleed,
    strokeColor: "C=15 M=100 Y=100 K=0",//RED
    fillColor: "None",
    transparencySettings: { blendingSettings: { opacity: 50 } },
    strokeAlignment: StrokeAlignment.INSIDE_ALIGNMENT
});

// Create the Finished Size rectangle
var finishedRect = graphicTemplate.pages.item(0).rectangles.add({
    geometricBounds: [(graphicTemplate.documentPreferences.pageHeight - finishedHeight+finishedHeightStroke) / 2, (graphicTemplate.documentPreferences.pageWidth - finishedWidth+finishedHeightStroke) / 2, (graphicTemplate.documentPreferences.pageHeight + finishedHeight-finishedHeightStroke) / 2, (graphicTemplate.documentPreferences.pageWidth + finishedWidth-finishedHeightStroke) / 2],
    strokeWeight: 5,
    strokeColor: "Black",
    strokeType: "Dashed (4 and 4)",
    fillColor: "None",
    transparencySettings: { blendingSettings: { opacity: 100 } },
    strokeAlignment: StrokeAlignment.CENTER_ALIGNMENT
});



// Create the Safe Area rectangle
var safeAreaRect = graphicTemplate.pages.item(0).rectangles.add({
    geometricBounds: [(graphicTemplate.documentPreferences.pageHeight - (finishedHeight - safetyOffset)) / 2, (graphicTemplate.documentPreferences.pageWidth - (finishedWidth - safetyOffset)) / 2, (graphicTemplate.documentPreferences.pageHeight + (finishedHeight - safetyOffset)) / 2, (graphicTemplate.documentPreferences.pageWidth + (finishedWidth - safetyOffset)) / 2],
    strokeWeight: 5,
    strokeColor: "C=75 M=5 Y=100 K=0",//GREEN
    strokeType: "Solid",
    fillColor: "None",
    transparencySettings: { blendingSettings: { opacity: 100 } },
    strokeAlignment: StrokeAlignment.CENTER_ALIGNMENT
});

//Add Graphic Specs Text
var graphicSpecs = graphicTemplate.textFrames.add();
graphicSpecs.parentStory.appliedFont = "Arial";
graphicSpecs.textFramePreferences.verticalJustification = VerticalJustification.CENTER_ALIGN;
graphicSpecs.properties =

{

    geometricBounds: [(graphicTemplate.documentPreferences.pageHeight) * 0.125, (graphicTemplate.documentPreferences.pageWidth) * 0.125, (graphicTemplate.documentPreferences.pageHeight) * 0.875, (graphicTemplate.documentPreferences.pageWidth) * 0.875],
    strokeWidth : 0,
    parentStory : { pointSize : 1.5*Number(pageHeightIN) },
    fillColor : "None",
    //contents : "hello this is test text"
};



// Set the text content
graphicSpecs.contents = productName + " Graphic Template\r";
// Set the text color to black
graphicSpecs.characters.itemByRange(0, graphicSpecs.contents.length-1).fillColor = "Black";//BLACK

graphicSpecs.contents += "Document Size: " + pageWidthIN + "\" x " + pageHeightIN + "\"\r";
// Set the text color to CYAN
graphicSpecs.characters.itemByRange(graphicSpecs.contents.length-1, graphicSpecs.contents.length-1 + ("Document Size: " + pageWidthIN + "\" x " + pageHeightIN + "\"\r").length).fillColor = "C=100 M=0 Y=0 K=0";//CYAN

graphicSpecs.contents += "Finished Size: " + finishedWidthIN + "\" x " + finishedHeightIN + "\"\r";
// Set the text color to BLACK
graphicSpecs.characters.itemByRange(graphicSpecs.contents.length-1, graphicSpecs.contents.length-1 + ("Finished Size: " + finishedWidthIN + "\" x " + finishedHeightIN + "\"\r").length).fillColor = "Black";//BLACK

graphicSpecs.contents += "Bleed: " + bleedIN + "\" on each of 4 sides\r";
// Set the text color to RED
graphicSpecs.characters.itemByRange(graphicSpecs.contents.length-1, graphicSpecs.contents.length-1 + ("Bleed: " + bleedIN + "\" on each of 4 sides\r").length).fillColor = "C=15 M=100 Y=100 K=0";//RED

graphicSpecs.contents += "100 DPI\rAll text should be converted to outlines";
// Set the text color to BLACK
graphicSpecs.characters.itemByRange(graphicSpecs.contents.length-1, graphicSpecs.contents.length-1 + ("100 DPI\rAll text should be converted to outlines").length).fillColor = "Black";//BLACK



// Add file name text frame
var fileNameTextFrame = graphicTemplate.textFrames.add({
    geometricBounds: [graphicTemplate.documentPreferences.pageHeight - (bleed*0.75), 36, graphicTemplate.documentPreferences.pageHeight, graphicTemplate.documentPreferences.pageWidth],
    contents: app.activeDocument.name,
    fillColor: "None"
});


fileNameTextFrame.parentStory.appliedFont = "Arial";
fileNameTextFrame.parentStory.pointSize = 1.00*Number(pageHeightIN);

 

rob day
Community Expert
Community Expert
March 24, 2023

I get a parameter error on the bleedRect creation on line 80. Do the Swatches you are referencing with strings exist in your swatches panel? If they don’t you’ll need to create them.

rob day
Community Expert
Community Expert
March 23, 2023

Hi @TestriteVisual , Your dialogs are returning strings, so I think you need to coerce the strings to numbers. Also you are asking for inches, and converting inches to points (*72), but if the application ruler units are set to inches you would get a dimension that exceeds InDesign’s limits, which might be the source of the crash. You can either set the application ruler units to points, or set the scriptPreferences.measurementUnit as POINTS:

 

 

//set units
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

// Prompt the user to enter the finished width, finished height, and bleed values
var finishedWidthIN = prompt("Enter the finished width (inches):");
var finishedHeightIN = prompt("Enter the finished height (inches):");
var bleedIN = prompt("Enter the bleed (inches):");

// Convert the entered values to points (1 inch = 72 points)
//coerce numbers
finishedWidth = Number(finishedWidthIN)*72;
finishedHeight = Number(finishedHeightIN)*72;
bleed = Number(bleedIN)*72;

// Create a new document with the calculated dimensions
var doc = app.documents.add({
    documentPreferences: {
        pageWidth: finishedWidth + (2 * bleed),
        pageHeight: finishedHeight + (2 * bleed)
    }
});
//reset units
app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;

 

 

 

TestriteVisual
Inspiring
March 23, 2023

Thanks for the reply. I'm able to get the following updated script to run, but how can have the all-in-one dialogue box pop up to incorporate the script below, and set a prefilled default value of 1 inch for Bleed, change the strokeColor to be a CMYK value of [15,100,100,0] and set the  Stroke Alignment = INSIDE_ALIGNMENT ?

 

Also regarding the dialogue box, how can I have the "in" (for inches) not show in the box, but rather just have the word "(INCHES)" show up in the box header like this?

 

Thanks for any additional help on this!!! 🙂 

 

//set units
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

// Prompt the user to enter the finished width, finished height, and bleed values
var finishedWidthIN = prompt("Enter the finished width (inches):");
var finishedHeightIN = prompt("Enter the finished height (inches):");
var bleedIN = prompt("Enter the bleed (inches):");

// Convert the entered values to points (1 inch = 72 points)
//coerce numbers
finishedWidth = Number(finishedWidthIN)*72;
finishedHeight = Number(finishedHeightIN)*72;
bleed = Number(bleedIN)*72;

// Create a new document with the calculated dimensions
var doc = app.documents.add({
    documentPreferences: {
        pageWidth: finishedWidth + (2 * bleed),
        pageHeight: finishedHeight + (2 * bleed)
    }

});

doc.marginPreferences.properties = {
			top : 0,
			left: 0,
			right: 0,
			bottom:0
};


//reset units
//app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;


// Create the Finished Size rectangle
var bleeRect = doc.pages.item(0).rectangles.add({
    geometricBounds: [(doc.documentPreferences.pageHeight - finishedHeight) / 2, (doc.documentPreferences.pageWidth - finishedWidth) / 2, (doc.documentPreferences.pageHeight + finishedHeight) / 2, (doc.documentPreferences.pageWidth + finishedWidth) / 2],
    strokeWeight: bleed,
    strokeColor: "Black",
    strokeType: "Solid",
    fillColor: "None",
    transparencySettings: { blendingSettings: { opacity: 50 } }
    
});

 

rob day
Community Expert
Community Expert
March 23, 2023

Also regarding the dialogue box, how can I have the "in" (for inches) not show in the box

 

The dialog class also includes realEditboxes, which forces a real number—in that case you would want to set the script preference to INCHES and not multiply by 72. The advantage of a measurementEdit box is the user could type in any unit, if I enter 300mm, the dialog would return 300 millimeters converted to points

 

Try this

 

 

makeDialog();

//the returned dialog values
var pw, ph, bld;
function makeDialog(){
    //name is the dialog’s displayed name
    var d = app.dialogs.add({name:"Document Dimensions in Inches", canCancel:true});
    with(d.dialogColumns.add()){
        //labels
        staticTexts.add({staticLabel:"Page Width:"});
        staticTexts.add({staticLabel:"Page Height:"});
        staticTexts.add({staticLabel:"Bleed:"});
    }
    with(d.dialogColumns.add()){
        //Inch measurement units. editValues are in points
        //There’s also realEditboxes
        //pw = realEditboxes.add({editValue:8, minWidth:100});
        pw = measurementEditboxes.add({editUnits:MeasurementUnits.INCHES, editValue:576, minWidth:90});
        ph = measurementEditboxes.add({editUnits:MeasurementUnits.INCHES, editValue:720, minWidth:90});
        bld = measurementEditboxes.add({editUnits:MeasurementUnits.INCHES, editValue:72, minWidth:90});
    }

    if(d.show() == true){
        //the results returned as Points
        pw = pw.editValue;
        ph = ph.editValue;
        bld = bld.editValue;
        makeDocument()
        d.destroy();
	}
}

function makeDocument(){
    app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
    var finishedWidth = pw + (2 * bld);
    var finishedHeight = ph + (2 * bld);
    // Create a new document with the calculated dimensions
    var doc = app.documents.add({
        documentPreferences: {
            pageWidth: finishedWidth,
            pageHeight: finishedHeight
        }
    });
    doc.marginPreferences.properties = {
		top : 0,
		left: 0,
		right: 0,
		bottom:0
    };
    // Create the Finished Size rectangle
    var bleeRect = doc.pages.item(0).rectangles.add({
        geometricBounds: [0, 0, finishedHeight, finishedWidth],
        strokeWeight: bld,
        strokeColor: "Black",
        strokeType: "Solid",
        strokeAlignment: StrokeAlignment.INSIDE_ALIGNMENT,
        fillColor: "None",
        transparencySettings: { blendingSettings: { opacity: 50 } }
    });
    
    app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;
}

 

 

 

saschak38136531
Known Participant
March 23, 2023

It seems that your fill- and stroke-Colors are not defined in the new document.

Maybe you have to create new swatches before creating the rectangles. 

 

Hope, this helps 😉

Sascha