Change a font using extendscript in illustrator

Explorer ,
Aug 06, 2014 Aug 06, 2014

Copy link to clipboard

Copied

I have a script that loops through text items on an illustrator page and when it encounters one of a certain size it changes it to a different point size. I would also like to alter the font. The code I have is

     if (roundedSize== 7.2) {

                item.textRange.characterAttributes.size=9.6;

                $.writeln(item.textRange.characterAttributes.textFont)

                item.textRange.characterAttributes.textFont="Metric-Semibold"

                };

It work fine without the fourth line, but as soon as I try and set a typeface the loop bails. Could someone tell me what I am doing wrong here

Many thanks

TOPICS
Scripting

Views

2.7K

Likes

translate

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
Most Valuable Participant ,
Aug 06, 2014 Aug 06, 2014

Copy link to clipboard

Copied

Hi Bob Haslett,

there are several ways.

One way could be:

item.textRange.characterAttributes.textFont = textFonts.getByName("Metric-Semibold");

Have fun

Likes

translate

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
Explorer ,
Aug 07, 2014 Aug 07, 2014

Copy link to clipboard

Copied

Thanks tha works just as I want it to.

Likes

translate

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
Explorer ,
Apr 06, 2018 Apr 06, 2018

Copy link to clipboard

Copied

Thanks for this solution. I have tried using this, but I get error

var doc = app.activeDocument;

ii= doc.textFrames.add();  //adding text

ii.textRange.characterAttributes.size = 9.5;  //character size for text

ii.textRange.characterAttributes.textFont = app.textFonts.getByName("Times");

ii.contents="wwwwww";

I would be grateful for any suggestions.

Likes

translate

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
Most Valuable Participant ,
Apr 06, 2018 Apr 06, 2018

Copy link to clipboard

Copied

Some notes:

Define your variable for the textFrame (eg var ii = doc... )

Do better not use ii as variable name. This is mostly a 'counter variable'.

Times is not a valid textFont name. Use Times-Roman or Times-Bold instead.

Have fun

Likes

translate

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
Explorer ,
Apr 08, 2018 Apr 08, 2018

Copy link to clipboard

Copied

Thanks a lot, It worked..

Likes

translate

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
New Here ,
Mar 28, 2021 Mar 28, 2021

Copy link to clipboard

Copied

Can you show the complete code for the above requirement  ?

Likes

translate

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 28, 2021 Mar 28, 2021

Copy link to clipboard

Copied

var doc = app.activeDocument;
var text1 = doc.textFrames.add();
text1.textRange.characterAttributes.size = 9.5;
text1.textRange.characterAttributes.textFont = app.textFonts["TimesNewRomanPSMT"];
text1.contents = "wwwwww";

Likes

translate

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