Skip to main content
Inspiring
April 28, 2021
Answered

Apply justification to just added textFrame (indesign script)

  • April 28, 2021
  • 2 replies
  • 852 views

Hi, 

The target is to add a textFrame with centered page number out under every page. I know can do it with master pages but don't want to affect. It's only for printing proofs.

How to center page number into textFrame?

Can anyone help me to complete this script?

Thanks a lot

 

Mario

 

var doc = app.activeDocument;
var myRuler = doc.viewPreferences.rulerOrigin;
doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.millimeters;
doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.millimeters;
doc.documentPreferences.slugTopOffset = 15;
doc.documentPreferences.documentSlugUniformSize = true;
doc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
doc.zeroPoint = [0,0];
var vmu = doc.pages[0].bounds[2];
var hmu = doc.pages[0].bounds[3];

for (i = 0; i < doc.pages.length; i++) {
doc.pages[i].textFrames.add().properties = {
geometricBounds : [ (vmu + 5),0,(vmu + 15),hmu ],
strokeWidth : 0,
fillColor : "None",
contents : SpecialCharacters.AUTO_PAGE_NUMBER,
// justification = Justification.CENTER_ALIGN, //no error msg with this line but no result
}
 
};

doc.viewPreferences.rulerOrigin = myRuler;
This topic has been closed for replies.
Correct answer Laubender

Hi Grandeforedit,

one can set this up while adding the text frame.

Provide an object as argument to method add().

The other trick is to define text formatting with property parentStory, also with an object.

Text frame preferences can be done as well.

 

Example:

 

var doc = app.documents[0];
doc.textFrames.add
(
	{
		contents : "Hello World!" ,
		geometricBounds : [0,0,"100mm","100mm"],
		strokeWidth : 0 ,
		strokeColor: "None" ,
		
		parentStory : 
			{
				justification : Justification.CENTER_ALIGN ,
				pointSize : 20 ,
				fillColor : doc.colors.itemByName("Magenta") ,
				fillTint : 50 ,
				leftIndent : "20 mm" ,
				rightIndent : "20 mm"
			} ,
		
		textFramePreferences :
			{
				verticalJustification : VerticalJustification.CENTER_ALIGN
			}
	}
);

 

Regards,
Uwe Laubender

( ACP )

2 replies

LaubenderCommunity ExpertCorrect answer
Community Expert
April 28, 2021

Hi Grandeforedit,

one can set this up while adding the text frame.

Provide an object as argument to method add().

The other trick is to define text formatting with property parentStory, also with an object.

Text frame preferences can be done as well.

 

Example:

 

var doc = app.documents[0];
doc.textFrames.add
(
	{
		contents : "Hello World!" ,
		geometricBounds : [0,0,"100mm","100mm"],
		strokeWidth : 0 ,
		strokeColor: "None" ,
		
		parentStory : 
			{
				justification : Justification.CENTER_ALIGN ,
				pointSize : 20 ,
				fillColor : doc.colors.itemByName("Magenta") ,
				fillTint : 50 ,
				leftIndent : "20 mm" ,
				rightIndent : "20 mm"
			} ,
		
		textFramePreferences :
			{
				verticalJustification : VerticalJustification.CENTER_ALIGN
			}
	}
);

 

Regards,
Uwe Laubender

( ACP )

Inspiring
April 28, 2021

It works great,

what a lesson about nested proprerties and preferences!

Thanks, thanks a lot Uwe.

Mario

brian_p_dts
Community Expert
Community Expert
April 28, 2021

Assign a variable to each created frame, say 'tf', then within your for loop after you've created the frame: 

tf.texts[0].justification = Justification.CENTER_ALIGN;

Inspiring
April 28, 2021

Thanks Brian, not sure I understand how to assign a variable to each created frame but you point me to a solution.

Testing it seems to work on the right frame ...I hope

 

thanks again

Mario

 

var doc = app.activeDocument;
var myRuler = doc.viewPreferences.rulerOrigin;
doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.millimeters;
doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.millimeters;
doc.documentPreferences.slugTopOffset = 15;
doc.documentPreferences.documentSlugUniformSize = true;
doc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
doc.zeroPoint = [0,0];
var vmu = doc.pages[0].bounds[2];
var hmu = doc.pages[0].bounds[3];

for (i = 0; i < doc.pages.length; i++) {
doc.pages[i].textFrames.add().properties = {
geometricBounds : [ (vmu + 5),0,(vmu + 15),hmu ],
strokeWidth : 0,
fillColor : "None",
contents : SpecialCharacters.AUTO_PAGE_NUMBER,
}
doc.pages[i].textFrames[0].texts[0].justification = Justification.CENTER_ALIGN;
}

doc.viewPreferences.rulerOrigin = myRuler;
brian_p_dts
Community Expert
Community Expert
April 28, 2021
See bolded line: 
 
for (i = 0i < doc.pages.lengthi++) {
var tf = doc.pages[i].textFrames.add().properties = {
geometricBounds : [ (vmu + 5),0,(vmu + 15),hmu ],
strokeWidth : 0,
fillColor : "None",
contents : SpecialCharacters.AUTO_PAGE_NUMBER,
}
tf.texts[0].justification = Justification.CENTER_ALIGN;