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

Having a problem with the itemByRange function, please help

Contributor ,
Mar 24, 2023 Mar 24, 2023

Copy link to clipboard

Copied

Can anyone please help with this error. Thank you kindly!!! 🙂

1.jpg

 

Here is the line where it gives the error shown in context of the code snipped below

// 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, ("Finished Size: " + finishedWidthIN + "\" x " + finishedHeightIN + "\"\r").length).fillColor = "Black";//BLACK

graphicSpecs.contents += "Safe Size: " + safeWidthIN + "\" x " + safeHeightIN + "\"\r";
// Set the text color to GREEN
graphicSpecs.characters.itemByRange(graphicSpecs.contents.length-1, ("Safe Size: " + safeWidthIN + "\" x " + safeHeightIN + "\"\r").length).fillColor = "C=75 M=5 Y=100 K=0";//GREEN

graphicSpecs.contents += "Bleed: " + bleedIN + "\" on each of 4 sides\r";
// Set the text color to RED
graphicSpecs.characters.itemByRange(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, ("100 DPI\rAll text should be converted to outlines").length).fillColor = "Black";//BLACK

 

Here is the full script

//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));
    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;
    safetyOffset = Number(safetyOffsetIN)*72;
    finishedSizeStroke = 7;
    safeSizeStroke = 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+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 } },
    strokeAlignment: StrokeAlignment.CENTER_ALIGNMENT
});



// 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 } },
    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, ("Finished Size: " + finishedWidthIN + "\" x " + finishedHeightIN + "\"\r").length).fillColor = "Black";//BLACK

graphicSpecs.contents += "Safe Size: " + safeWidthIN + "\" x " + safeHeightIN + "\"\r";
// Set the text color to GREEN
graphicSpecs.characters.itemByRange(graphicSpecs.contents.length-1, ("Safe Size: " + safeWidthIN + "\" x " + safeHeightIN + "\"\r").length).fillColor = "C=75 M=5 Y=100 K=0";//GREEN

graphicSpecs.contents += "Bleed: " + bleedIN + "\" on each of 4 sides\r";
// Set the text color to RED
graphicSpecs.characters.itemByRange(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, ("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);

 

 

TOPICS
Scripting

Views

207

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

Enthusiast , Mar 25, 2023 Mar 25, 2023

I replaced the text formatting section with 2 arrays and a loop, and I think it's more along the lines of what you're trying to do? If not, I haven't really worked with ranges in InDesign so this was good practice!
Screen Grab of result below:

iamwickedtall_0-1679776544971.png

Script below replaces original lines 132-155:

sw = app.activeDocument.swatches;
// Set the text content
graphicSpecs.contents = productName + " Graphic Template\r";
// Set the text color to black

col = ["Black","C=100 M=0 Y=0 K=0","Black","C=75 M=5 Y=100 K=0
...

Votes

Translate

Translate
Enthusiast ,
Mar 25, 2023 Mar 25, 2023

Copy link to clipboard

Copied

It seems that the error is occuring with "C=100 M=0 Y=0 K=0". Change that to "app.activeDocument.swatches.item("C=100 M=0 Y=0 K=0");" and it should work. The highlighting seems to be a bit off, but I did test that in place of the green and it highlighted blue. I'd create a variable for swatches and change all of them to .item("<<NAME>>");

Full Line:

graphicSpecs.characters.itemByRange(graphicSpecs.contents.length-1, (graphicSpecs.contents.length-1 + ("Document Size: " + pageWidthIN + "\" x " + pageHeightIN + "\"\r")).length).fillColor = app.activeDocument.swatches.item("C=100 M=0 Y=0 K=0");//CYAN

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
Enthusiast ,
Mar 25, 2023 Mar 25, 2023

Copy link to clipboard

Copied

I replaced the text formatting section with 2 arrays and a loop, and I think it's more along the lines of what you're trying to do? If not, I haven't really worked with ranges in InDesign so this was good practice!
Screen Grab of result below:

iamwickedtall_0-1679776544971.png

Script below replaces original lines 132-155:

sw = app.activeDocument.swatches;
// Set the text content
graphicSpecs.contents = productName + " Graphic Template\r";
// Set the text color to black

col = ["Black","C=100 M=0 Y=0 K=0","Black","C=75 M=5 Y=100 K=0","C=15 M=100 Y=100 K=0","Black"]

theText = ["Document Size: " + pageWidthIN + "\" x " + pageHeightIN + "\"\r",
"Finished Size: " + finishedWidthIN + "\" x " + finishedHeightIN + "\"\r",
"Safe Size: " + safeWidthIN + "\" x " + safeHeightIN + "\"\r",
"Bleed: " + bleedIN + "\" on each of 4 sides\r",
"100 DPI\rAll text should be converted to outlines"
]

for(var i = 0; i<theText.length; i++){
    var curText = theText[i];
    var start = graphicSpecs.contents.length;
    var end = start+curText.length-1;
    graphicSpecs.contents +=curText;
    graphicSpecs.characters.itemByRange(start,end).fillColor = sw.item(col[i]);
}

 

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
Contributor ,
Mar 27, 2023 Mar 27, 2023

Copy link to clipboard

Copied

LATEST

Awesome, this is great. Thank you very much for this!!

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