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

How to change the font type for a specific frame using script??

Participant ,
Aug 27, 2020 Aug 27, 2020

Hey all! 

 

I've a doc full of diffranet textframes, and i want to change the font type for a scpicif textframe, how to do it?

I've used this code, it's good and works but change all the text frames, and i want to change only a spicifed frame!

The code

		app.findTextPreferences=app.changeTextPreferences= NothingEnum.nothing;

app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;

app.findTextPreferences.appliedFont = app.fonts.item("Times New Roman MT Std Bold");////Here you can specify your old font name

app.changeTextPreferences.appliedFont = app.fonts.item("Times New Roman Italic");////Here you can specify your NEW font name

doc.changeText();
		

 

and lets say that the frame i want to change 

	var rect2 = doc.pages[0].textFrames.add({
		geometricBounds: [12.7, 12.7, 90, 90]
	});

		rect2.contents = "wooow"

 

and thanks all!

TOPICS
Activation billing and install , Bug , EPUB , Feature request , How to , Import and export , InCopy workflow , Performance , Print , Publish online , Scripting , SDK , Sync and storage , Type
1.0K
Translate
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 2 Correct answers

Community Expert , Aug 27, 2020 Aug 27, 2020

What is the font that you are using? Is it installed on the system or document fonts folder?

Try changing the code to the following

rect.texts[0].appliedFont = "Times New Roman"
rect.texts[0].fontStyle = "Bold"

-Manan 

Translate
Community Expert , Aug 27, 2020 Aug 27, 2020

—once i type the name in the code

 

The name string has to be exact including any whitespace characters, so maybe try this variation on Manan’s suggestion and copy and paste the text from the alert dialog rather than typing the name—appliedFont is the family and style usually separated by a tab

 

 

 

//note that I’m getting the appliedFont.name and not appliedFont.fontFamily
alert(app.selection[0].texts[0].appliedFont.name)

//the appliedFont is the font name and style with a tab separating the 
...
Translate
Community Expert ,
Aug 27, 2020 Aug 27, 2020

If you have the reference of the text frame you don't need to use the find change, just change the font like the following

rect.texts[0].appliedFont = app.fonts.item("Times New Roman MT Std Bold")

-Manan 

Translate
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 ,
Aug 27, 2020 Aug 27, 2020

The font i use is installed, so when i use the code that i've posted it works and change the font, but when id ur code and write the family font, it alerrt me that the requested font family is unaviliable.

Translate
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
Advisor ,
Aug 27, 2020 Aug 27, 2020

Hello,

 

You can give this a try.

var rect2 = doc.pages[0].textFrames.add({
    geometricBounds: [12.7, 12.7, 90, 90]
});

rect2.contents = "wooow"
rect2 = myYTextFrame.parentStory.characters.everyItem();
rect2.appliedFont = app.fonts.item("Helvetica Neue");
rect2.fontStyle = "Medium";  

Regards,

Mike

Translate
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 ,
Aug 27, 2020 Aug 27, 2020

There is error (

myYTextFrame

is undefined)

 

Translate
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
Advisor ,
Aug 27, 2020 Aug 27, 2020

Sorry....

var rect2 = doc.pages[0].textFrames.add({
    geometricBounds: [12.7, 12.7, 90, 90]
});

rect2.contents = "wooow"
myTextObject = rect2.parentStory.characters.everyItem();
myTextObject.appliedFont = app.fonts.item("Helvetica Neue");
myTextObject.fontStyle = "Medium"; 

Regards,

Mike

Translate
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 ,
Aug 27, 2020 Aug 27, 2020

It works, but only with the bulit in fonts (i,e)

Times New Roman
Translate
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 ,
Aug 27, 2020 Aug 27, 2020

Try something like the following

rect.texts[0].appliedFont = app.fonts.item("Times New Roman")
rect.texts[0].fontStyle = "Bold"

-Manan

Translate
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 ,
Aug 27, 2020 Aug 27, 2020

It works, but i think because 

Times New Roman

is a built-in font type. When i use mine, the installed one, it alert me that the requested font are unavaible. The weird thing when i replac the font from the UI not the script it wokrs!  

Translate
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 ,
Aug 27, 2020 Aug 27, 2020

Apply your font using the UI, select the frame and run the following code

alert(app.selection[0].texts[0].appliedFont.fontFamily)

This should give you the name of the font family that is to be used in the code I gave previously.

-Manan

Translate
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 ,
Aug 27, 2020 Aug 27, 2020

indeed yes it gave me the name, and once i type the name in the code and specify the style, it alerts me that the requested font un available

Translate
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 ,
Aug 27, 2020 Aug 27, 2020

What is the font that you are using? Is it installed on the system or document fonts folder?

Try changing the code to the following

rect.texts[0].appliedFont = "Times New Roman"
rect.texts[0].fontStyle = "Bold"

-Manan 

Translate
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 ,
Aug 27, 2020 Aug 27, 2020

I'm using a special font for my company, and yes it's installed in the system. still same issue. the furstrating thing that i can change the font and found the font family from the UI !

Translate
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
Advisor ,
Aug 27, 2020 Aug 27, 2020

Can you share\post the "special font" for testing?

 

Regards,

Mike

Translate
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 ,
Aug 31, 2020 Aug 31, 2020
LATEST

it's for my company, but it works now. apperntly there was a tab space i didn't recognize it.

Translate
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 ,
Aug 27, 2020 Aug 27, 2020

—once i type the name in the code

 

The name string has to be exact including any whitespace characters, so maybe try this variation on Manan’s suggestion and copy and paste the text from the alert dialog rather than typing the name—appliedFont is the family and style usually separated by a tab

 

 

 

//note that I’m getting the appliedFont.name and not appliedFont.fontFamily
alert(app.selection[0].texts[0].appliedFont.name)

//the appliedFont is the font name and style with a tab separating the name and style so try copying from the alert
app.selection[0].texts[0].appliedFont = "Myriad Pro	Regular"

 

 

 

Screen Shot 41.pngexpand image

 

Translate
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 ,
Aug 31, 2020 Aug 31, 2020

THANKS rob!!! it works perfictly! i didn't now about the tab! THANKS A LOT

Translate
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