Skip to main content
Participating Frequently
March 16, 2020
Answered

Script for changing font

  • March 16, 2020
  • 4 replies
  • 5497 views

Hello, I am creating .Jsx script  to create automatic text, but i wanted to change font of that Text but i dont know how to do it.

can anyone help with this?

 

This topic has been closed for replies.
Correct answer Manan Joshi

One way to do is just set the font of the content of the textframe and the other way would be to apply a paragraph style to the content of the frame. The paragraph style can be reffered by name, use one of the following

myTextFrame.parentStory.appliedFont = "Arial" //Change with the name of the font you want

Apply paragraph style

myTextFrame.parentStory.appliedParagraphStyle = myDocument.paragraphStyles.itemByName("NameOfParagraphStyle") //Substitute with the name of the paragraph style you want to use

 

The other properties you can use on the story object can be seen from the following link

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Story.html#d1e530047

 

-Manan

4 replies

Braniac
March 17, 2020

Hi patilg5,

just to give you a variant of what Manan said.

There are ways to format text in the moment you are creating that text frame.

 

Just a sample below:

 

var page = app.documents[0].pages[0];

var newTextFrame =
page.textFrames.add
(
	{
		geometricBounds : ["12p", "40p", "10p", "10p"] ,
		contents : "®/™ ©2020. All rights reserved. Used under license in Canada." ,
		
		parentStory : 
		{
			appliedFont : "Arial" ,
			fontStyle : "Bold" ,
			pointSize : 15
		} ,
	
		textFramePreferences :
		{
			autoSizingType : AutoSizingTypeEnum.HEIGHT_ONLY ,
			autoSizingReferencePoint : AutoSizingReferenceEnum.TOP_CENTER_POINT ,
			verticalJustification : VerticalJustification.TOP_ALIGN
		}

	}
);

 

Regards,
Uwe Laubender

( ACP )

Braniac
March 16, 2020

That would be the pointSize and fontStyle property, example below

myTextFrame.parentStory.fontStyle = "Italic"
myTextFrame.parentStory.pointSize = 15

You can find all these properties in the link i shared. Also if you use the paragraphstyle method i mentioned you will not have to do all this in code, make all these changes in the style and it will work no need to change the code everytime

 

-Manan

Participating Frequently
March 16, 2020

Many thanks Manan for helping me.

Manan JoshiCorrect answer
Braniac
March 16, 2020

One way to do is just set the font of the content of the textframe and the other way would be to apply a paragraph style to the content of the frame. The paragraph style can be reffered by name, use one of the following

myTextFrame.parentStory.appliedFont = "Arial" //Change with the name of the font you want

Apply paragraph style

myTextFrame.parentStory.appliedParagraphStyle = myDocument.paragraphStyles.itemByName("NameOfParagraphStyle") //Substitute with the name of the paragraph style you want to use

 

The other properties you can use on the story object can be seen from the following link

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Story.html#d1e530047

 

-Manan

Participating Frequently
March 16, 2020

Hello Manan,

Really appriciate your support,

Could you please tell me if I want to change font style and font size what will be script for same

Braniac
March 16, 2020

Hi,

 

Share what code you have written so far, the code would depend upon the what references you have in your code. For ex:- if you have to change the font of the text selection on the document, or of all the text in a selection of textframe, or find some pattern of text and change its font etc. Looking at your code we will be able to guide on what will best suit the workflow you are designing so that you learn as well

 

-Manan

Participating Frequently
March 16, 2020

Hello Manan,

Thanks for reply,

I write below code to create auto text, but the font of text is as per deafault setting but i have to change its font and size  through scripting. could you help me in that what should i do for that

var myDocument = app.documents.add();
var myTextFrame = myDocument.pages.item(0).textFrames.add();
myTextFrame.geometricBounds = ["12p", "40p", "10p", "10p"];
myTextFrame.contents = "®/™ ©2020 . All rights reserved. Used under license in Canada.";

Braniac
March 16, 2020

Hello  Patilg5!

 

you can try....

      myTextFrame = myTextFrame.parentStory.characters.everyItem();
      myTextFrame.appliedFont = app.fonts.item("Arial");
      myTextFrame.fontStyle = "Bold";

Regards,

Mike