Skip to main content
Inspiring
January 7, 2023
Answered

scripting issue centering text and bleed

  • January 7, 2023
  • 2 replies
  • 591 views

I almost have this working but I can't seem to figure out 2 things.

 

1.  Not a deal breaker because it still kind of works, but when I create the document it is not setting the bleed. The black rectangle still extends to bleed but the its not setting in the document setup/bleed.

 

2. I can't seem to get the text to center, I get "Error Number: 2 Error String: justification is undefined" and I don't understand why. Any help would be greatly appriciated!

 

// Create a new document with a width of 6 inches, a height of 9 inches, and a bleed of .125 inches
var doc = app.documents.add({
  documentPreferences: {
    pageWidth: 6,
    pageHeight: 9,
    bleedTop: .125,
    bleedLeft: .125,
    bleedBottom: .125,
    bleedRight: .125
  }
});

// Create a black rectangle that extends to the full bleed
var rect = doc.rectangles.add({
  geometricBounds: [-.125, -.125, 9.125, 6.125]
});
rect.fillColor = "Black";

// Create a text frame with the text
var textFrame = doc.textFrames.add();
textFrame.properties =
{
  geometricBounds: [2.96,0.25,4.3,5.75],
  contents: "Text Here\nand More Here"
};

// Format text
var textRange = textFrame.characters.everyItem();
textRange.appliedFont = "Helvetica Neue LT Std\t77 Bold Condensed";
textRange.pointSize = 51;
textRange.leading = 55;
textRange.fillColor = "Paper";
textRange.justification = justification.CENTER;
This topic has been closed for replies.
Correct answer Robert at ID-Tasker

1) can you show a screenshot?

2) why are requesting array / collection of all Characters - I'm not JS guy so not sure what everyItem() returns - but you should rather do:

 

 

 

var textRange = textFrame.texts.item(0);

 

 

 

Or even:

 

 

 

var textRange = TextFrame.parentStory;

 

 

Justification isn't property of the Character. 

 

2 replies

brian_p_dts
Community Expert
Community Expert
January 7, 2023

To answer your first question, you're calling incorrect properties for the bleed settings. Should be: 

documentPreferences: {
    pageWidth: 6,
    pageHeight: 9,
    documentBleedTopOffset: .125,
    documentBleedInsideOrLeftOffset: .125,
    documentBleedBottomOffset: .125,
    documentBleedOutsideOrRightOffset: .125
  }

 

Or more simply: 

documentBleedTopOffset: .125,
documentBleedUniformSize: true

 

See: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#DocumentPreference.html

Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Legend
January 7, 2023

1) can you show a screenshot?

2) why are requesting array / collection of all Characters - I'm not JS guy so not sure what everyItem() returns - but you should rather do:

 

 

 

var textRange = textFrame.texts.item(0);

 

 

 

Or even:

 

 

 

var textRange = TextFrame.parentStory;

 

 

Justification isn't property of the Character. 

 

Inspiring
January 7, 2023

everyItem here just includes every character in the text frame. There is most likely a better way to do it but for now your observation was correct. Center is not a property of character, I got it working correctly by adding this:

 

thanks again!

 

textFrame.parentStory.justification = Justification.CENTER_ALIGN;

 

Robert at ID-Tasker
Legend
January 7, 2023

You are welcome 🙂 

 

But it's still not the right way to interact with text objects in the InDesign. 

 

If you have more than one character to process - you should get a reference to the .texts(0) - not to the array/collection of all individual characters - unless, of course, your goal is to process each character individually.

 

Properties of the Character object are a subset of the properties of the Text object - and Text object is much more universal so if you first get a reference to the Text object - you'll avoid similar errors in the future.