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

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

Participant ,
Mar 22, 2023 Mar 22, 2023

Copy link to clipboard

Copied

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!!!!

TOPICS
Scripting

Views

429

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

correct answers 1 Correct answer

Community Expert , Mar 24, 2023 Mar 24, 2023

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 
...

Votes

Translate

Translate
Explorer ,
Mar 23, 2023 Mar 23, 2023

Copy link to clipboard

Copied

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

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 ,
Mar 23, 2023 Mar 23, 2023

Copy link to clipboard

Copied

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;

 

 

 

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
Participant ,
Mar 23, 2023 Mar 23, 2023

Copy link to clipboard

Copied

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?

1.jpg

 

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 } }
    
});

 

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 ,
Mar 23, 2023 Mar 23, 2023

Copy link to clipboard

Copied

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;
}

 

 

Screen Shot 28.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 ,
Mar 23, 2023 Mar 23, 2023

Copy link to clipboard

Copied

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;

}

 

Screen Shot 14.png

Screen Shot 15.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
Participant ,
Mar 24, 2023 Mar 24, 2023

Copy link to clipboard

Copied

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!! 🙂

1.jpg

 

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

3.jpg

 

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

2.jpg

 

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);

 

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 ,
Mar 24, 2023 Mar 24, 2023

Copy link to clipboard

Copied

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.

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
Participant ,
Mar 24, 2023 Mar 24, 2023

Copy link to clipboard

Copied

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

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 ,
Mar 24, 2023 Mar 24, 2023

Copy link to clipboard

Copied

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?

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
Participant ,
Mar 24, 2023 Mar 24, 2023

Copy link to clipboard

Copied

Ah ok THANK YOU for this. Works perfectly and way more simplified, thanks again!!

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
Participant ,
Mar 29, 2023 Mar 29, 2023

Copy link to clipboard

Copied

LATEST

Hi @rob day , I just noticed that if the width I set is very narrow, text that is supposed to all be treated as the same line with the same color gets pushed to the next line, like the generated page on the right in the screen shot that's 20 x 60.  The text "(includes bleeds)" which should have been part of the line 2 CYAN gets bumped down treated as line 3 (default black), and then the subsequent text all gets shifted to the next color. Any thoughts on how I can fix this? Thanks for your help 🙂

 

The left page shows the color matching the line as intended, which happens as long as the text does not get bumped down to the next line:

 

1.jpg

  
Text and color section snippet

// Set the text content
graphicSpecs.contents = productName + " - Graphic Template\r";
graphicSpecs.contents += "-Document Size: " + pageWidthIN + "\" x " + pageHeightIN + "\" (Includes bleeds)\r";
graphicSpecs.contents += "-Finished Size: " + finishedWidthIN + "\" x " + finishedHeightIN + "\"\r";
graphicSpecs.contents += "-Safe Size: " + safeWidthIN + "\" x " + safeHeightIN + "\"\r";
graphicSpecs.contents += "-Bleed: " + bleedIN + "\" on each of 4 sides\r";
graphicSpecs.contents += "-Extend background to full bleed\r";

if (polePocket ===0){}
else {
graphicSpecs.contents += "-Pole Pocket: " + polePocketIN + "\" at top and bottom (gray area is hidden side of pole pocket)\r";
     }

graphicSpecs.contents += "-CMYK\r-100 DPI @ Final Size\r-All text should be converted to outlines\r-Provide all linked files";


//line 2 DOCUMENT SIZE
graphicSpecs.lines[1].texts[0].fillColor = "C=100 M=0 Y=0 K=0";//CYAN
//line 4 SAFE SIZE
graphicSpecs.lines[3].texts[0].fillColor = "C=75 M=5 Y=100 K=0",//GREEN
//line 5 BLEED
graphicSpecs.lines[4].texts[0].fillColor = "C=15 M=100 Y=100 K=0"//RED
//line 6 EXTEND BACKGROUND
graphicSpecs.lines[5].texts[0].fillColor = "C=15 M=100 Y=100 K=0"//RED

 

Full code

// 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 productNameMenu = dropdowns.add({stringList:["*Custom", "A Frame SignHolder - AF", "Alle Banner Stand - AP", "American Eagle Banner Stand - FB", "Charisma 3D - XS", "Charisma Frame", "Charisma Inside Out - XSIO", "Classic Banner Stand - BN", "Classic Banner Stand - BV", "Classic Fixed Width Tower", "Clear Room Partition - CLRPART", "Convex SignHolder - VX", "Cool C Wall - CC", "CounterTop Banner Stand - CT", "Demo Table - CRDMWR", "EasyOpen SnapFrame - ME", "Flat Panel - 072x036 - FPL", "FrameGraphix SnapFrame - XE", "Halo - HALO", "Halo Rail - HR", "Harmony Banner Stand - HMN", "Harmony Banner Stand - HMQ", "Hercules Roll Around SignHolder - R", "Horizon Pillow Case - HP", "Large Format Banner Stand - BN", "Large Format Banner Stand - JN", "Lollipop Stand - LOLPOP", "Magnetic CounterTop SignBack - KB/KW", "Magnetic Signware Frame - TLC", "Mega Wave Wall - MG", "Mercury Banner Stand - RY", "Monster Frame - NS", "Nexus Banner Stand - NX", "Perfex A Frame - AFP", "Perfex Frame - PX", "Podium Case", "Pop-Up, Collage - LG", "Pop-Up, Presto - PFK", "Poster SignHolder", "Scenic Fabric Wall - PW", "SignPost Double Upright - YDU", "Silhouette Frame - OU", "Simple Standee - DEE", "Straight Flat Wall - FW", "TableTop Tension Fabric Display - TB", "Titan Poster SignHolder - LF", "Tornado Tower - COL", "Tornado Tower - TOR", "Ultra Banner Stand - UB", "Visual Graphic Stand - VG", "WireForm CounterTop Stand - WF"], selectedIndex:0});
            }
        }
        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"});
            }
        }
        with(dialogRows.add()){
	    with(dialogColumns.add()){
                staticTexts.add({staticLabel:"Pole Pocket (inches):"});
            }
            with(dialogColumns.add()){
                var polePocketField = dropdowns.add({stringList:["0", "1", "1.25", "1.75", "2", "2.75"], selectedIndex:0});
       }
        
	with(dialogRows.add()){
	    with(dialogColumns.add()){
                staticTexts.add({staticLabel:"Radius of Corners (inches):"});
            }
            with(dialogColumns.add()){
                var roundedCornerField = dropdowns.add({stringList:["0", "3.5", "6.5"], selectedIndex:0});
     
              }       
            }
        }
    }
}
var myResult = myDialog.show();

// Get the values entered in the dialog box
if(myResult == true){
    var productName = productNameMenu.selectedIndex == -1 ? "" : productNameMenu.stringList[productNameMenu.selectedIndex];
    var finishedWidthIN = finishedWidthField.editContents;
    var finishedHeightIN = finishedHeightField.editContents;
    var bleedIN = bleedField.editContents;
    var polePocketIN = polePocketField.selectedIndex == -1 ? "" : polePocketField.stringList[polePocketField.selectedIndex];
    var roundedCornerIN = roundedCornerField.selectedIndex == -1 ? "" : roundedCornerField.stringList[roundedCornerField.selectedIndex];
    var safetyOffsetIN = 2;
    var pageWidthIN = Number(finishedWidthIN) + (2 * Number(bleedIN));
    var pageHeightIN = Number(finishedHeightIN) + (2 * Number(bleedIN));
    var safeWidthIN = Number(finishedWidthIN) - (1 * Number(safetyOffsetIN));
    var safeHeightIN = Number(finishedHeightIN) - (1 * Number(safetyOffsetIN));
}

   
// Convert the entered values to points (1 inch = 72 points)
//coerce numbers
    finishedWidth = Number(finishedWidthIN)*72;
    finishedHeight = Number(finishedHeightIN)*72;
    bleed = Number(bleedIN)*72;
    polePocket = Number(polePocketIN)*72;
    roundedCorner = Number(roundedCornerIN)*72;
    safetyOffset = Number(safetyOffsetIN)*72;
    finishedSizeStroke = 0.20*finishedHeightIN;
    safeSizeStroke = 0.14*finishedHeightIN;

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

    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: 0,
    strokeColor: "None",
    fillColor: "C=15 M=100 Y=100 K=0",//RED
    transparencySettings: { blendingSettings: { opacity: 50 } },
    strokeAlignment: StrokeAlignment.INSIDE_ALIGNMENT
});

// Create the Finished Size CUT rectangle for bleed
var finishedRectCut = graphicTemplate.pages.item(0).rectangles.add({
    geometricBounds: [(graphicTemplate.documentPreferences.pageHeight - finishedHeight) / 2, (graphicTemplate.documentPreferences.pageWidth - finishedWidth) / 2, (graphicTemplate.documentPreferences.pageHeight + finishedHeight) / 2, (graphicTemplate.documentPreferences.pageWidth + finishedWidth) / 2],
    strokeWeight: 0,
    strokeColor: "None",
    fillColor: "C=15 M=100 Y=100 K=0",//RED
    transparencySettings: { blendingSettings: { opacity: 50 } },
    topLeftCornerOption: CornerOptions.ROUNDED_CORNER,
    topLeftCornerRadius: roundedCorner,
    topRightCornerOption: CornerOptions.ROUNDED_CORNER,
    topRightCornerRadius: roundedCorner,
    strokeAlignment: StrokeAlignment.INSIDE_ALIGNMENT
});

finishedRectCut.subtractPath (bleedRect);




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

if (polePocket ===0){}
else {

// Create the Pole Pocket rectangle - TOP
var topPolePocketRect = graphicTemplate.pages.item(0).rectangles.add({
    geometricBounds: [bleed, bleed, bleed + polePocket, graphicTemplate.documentPreferences.pageWidth - bleed],
    strokeWeight: 0,
    strokeColor: "None",
    fillColor: "Black",
    transparencySettings: { blendingSettings: { opacity: 25 } },
    strokeAlignment: StrokeAlignment.INSIDE_ALIGNMENT
});


// Create the Pole Pocket rectangle - BOTTOM
var bottomPolePocketRect = graphicTemplate.pages.item(0).rectangles.add({
    geometricBounds: [graphicTemplate.documentPreferences.pageHeight - (bleed + polePocket), bleed, graphicTemplate.documentPreferences.pageHeight - bleed,graphicTemplate.documentPreferences.pageWidth - bleed],
    strokeWeight: 0,
    strokeColor: "None",
    fillColor: "Black",
    transparencySettings: { blendingSettings: { opacity: 25 } },
    strokeAlignment: StrokeAlignment.INSIDE_ALIGNMENT
});


// Create the Pole Pocket rectangle - TOP CUT
var topPolePocketRectCut = graphicTemplate.pages.item(0).rectangles.add({
    geometricBounds: [bleed, bleed, bleed + polePocket, graphicTemplate.documentPreferences.pageWidth - bleed],
    strokeWeight: 0,
    strokeColor: "None",
    fillColor: "Black",
    transparencySettings: { blendingSettings: { opacity: 25 } },
    strokeAlignment: StrokeAlignment.INSIDE_ALIGNMENT
});


// Create the Pole Pocket rectangle - BOTTOM CUT
var bottomPolePocketRectCut = graphicTemplate.pages.item(0).rectangles.add({
    geometricBounds: [graphicTemplate.documentPreferences.pageHeight - (bleed + polePocket), bleed, graphicTemplate.documentPreferences.pageHeight - bleed,graphicTemplate.documentPreferences.pageWidth - bleed],
    strokeWeight: 0,
    strokeColor: "None",
    fillColor: "Black",
    transparencySettings: { blendingSettings: { opacity: 25 } },
    strokeAlignment: StrokeAlignment.INSIDE_ALIGNMENT
});

topPolePocketRectCut.subtractPath (bleedRect);
bottomPolePocketRectCut.subtractPath (bleedRect);

     }


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

//Add Graphic Specs Text
var graphicSpecs = graphicTemplate.textFrames.add();
graphicSpecs.parentStory.appliedFont = "Arial";
graphicSpecs.parentStory.hyphenation=false;
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";
graphicSpecs.contents += "-Document Size: " + pageWidthIN + "\" x " + pageHeightIN + "\" (Includes bleeds)\r";
graphicSpecs.contents += "-Finished Size: " + finishedWidthIN + "\" x " + finishedHeightIN + "\"\r";
graphicSpecs.contents += "-Safe Size: " + safeWidthIN + "\" x " + safeHeightIN + "\"\r";
graphicSpecs.contents += "-Bleed: " + bleedIN + "\" on each of 4 sides\r";
graphicSpecs.contents += "-Extend background to full bleed\r";

if (polePocket ===0){}
else {
graphicSpecs.contents += "-Pole Pocket: " + polePocketIN + "\" at top and bottom (gray area is hidden side of pole pocket)\r";
     }

graphicSpecs.contents += "-CMYK\r-100 DPI @ Final Size\r-All text should be converted to outlines\r-Provide all linked files";


//line 2 DOCUMENT SIZE
graphicSpecs.lines[1].texts[0].fillColor = "C=100 M=0 Y=0 K=0";//CYAN
//line 4 SAFE SIZE
graphicSpecs.lines[3].texts[0].fillColor = "C=75 M=5 Y=100 K=0",//GREEN
//line 5 BLEED
graphicSpecs.lines[4].texts[0].fillColor = "C=15 M=100 Y=100 K=0"//RED
//line 6 EXTEND BACKGROUND
graphicSpecs.lines[5].texts[0].fillColor = "C=15 M=100 Y=100 K=0"//RED



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


fileNameTextFrame.parentStory.appliedFont = "Arial";
fileNameTextFrame.parentStory.pointSize = fileNameSize;

 

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 ,
Mar 24, 2023 Mar 24, 2023

Copy link to clipboard

Copied

You might consider creating the document swatches at the top of the script—"C=15 M=100 Y=100 K=0",//RED would break on my InDesign because I’ve customized my default swatches. I don’t think you can be sure the defaults exist.

 

I use this function for swatch creation, which checks to see if the named swatch exists and if it doesn’t creates one. Note that some color names are reserved and can’t be used for swtches, (e.g. Cyan, Yellow, Magenta)

 

var graphicTemplate = app.documents.add()

var docCyan = makeSwatch(graphicTemplate, "ProcessCyan")
docCyan.properties = {model:ColorModel.PROCESS, space:ColorSpace.CMYK, colorValue:[100,0,0,0]}

var docRed = makeSwatch(graphicTemplate, "Red")
docRed.properties = {model:ColorModel.PROCESS, space:ColorSpace.CMYK, colorValue:[15,100,100,0]}

var docGreen = makeSwatch(graphicTemplate, "Green")
docRed.properties = {model:ColorModel.PROCESS, space:ColorSpace.CMYK, colorValue:[100,0,100,0]}

// use the color object variable for the fillColor:
//graphicSpecs.lines[1].texts[0].fillColor = docCyan;



/**
* Makes a new named Swatch 
* @ param the document to add the color to 
* @ param color name 
* @ return the new swatch 
*/

function makeSwatch(d, n){
    if (d.colors.itemByName(n).isValid) {
        return d.colors.itemByName(n);
    } else {
        return d.colors.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
Participant ,
Mar 24, 2023 Mar 24, 2023

Copy link to clipboard

Copied

This sounds like something good for me to try out in the future, but do you have any info on changing the color of each text line using the itemByRange I have shown.? Thanks.

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 ,
Mar 24, 2023 Mar 24, 2023

Copy link to clipboard

Copied

the itemByRange method has 2 parameters—the indexes of the first and last characters in the range. Looks like your code for the last character is breaking. I don’t know what your 2nd parameter is supposed to do:

 

graphicSpecs.contents.length-1 + ("Bleed: " + bleedIN + "\" on each of 4 sides\r").length)

 

Setting the text’s lines is easier—you don’t really need itemByRange.

 

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 ,
Mar 24, 2023 Mar 24, 2023

Copy link to clipboard

Copied

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