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

Script to change font Change color

Explorer ,
Feb 03, 2024 Feb 03, 2024

Copy link to clipboard

Copied

I need a script that makes the following changes: Change all "QUIRKY SPRING" fonts (assorted sizes) to the "Boldenvan" font (size 35). With this change, I need all changed boxes to apply the "height and width (keep proportions)" option to these changed text boxes. And finally, these changed boxes change the color to "C=69 M=46 Y=85 K=46".
I have:

 

var myDoc = app.activeDocument;

    app.findGrepPreferences = NothingEnum.nothing;
    app.changeGrepPreferences = NothingEnum.nothing;

// Find ant change fonts
app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedFont = app.fonts.item("QUIRKY SPRING"); //oldfont
app.changeTextPreferences.appliedFont = app.fonts.item("BoldenVan"); //new font
myDoc.changeText();

// Change size to 35pt in new fonts
var newTextFrames = myDoc.textFrames.everyItem().texts.everyItem().appliedFont.name === "BoldenVan";
for (var i = 0; i < newTextFrames.length; i++) {
    newTextFrames[i].pointSize = 35;
}

 

But the part of change size not function!
help?

TOPICS
Bug , Feature request , How to , Scripting

Views

248

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

correct answers 2 Correct answers

Community Expert , Feb 03, 2024 Feb 03, 2024

Should be:

 

newTextFrames[i].texts[0].pointSize = 35;

 

Votes

Translate

Translate
Community Expert , Feb 03, 2024 Feb 03, 2024

Hi @eusoujpg , Try this—for the fill color you have to create or reference an existing swatch, in this case I did not assume the swatch exists:

 

var d = app.activeDocument;

//create a fill color swatch if it does not exist
var fc = makeSwatch(d, "C=69 M=46 Y=85 K=46");
fc.properties = {model:ColorModel.PROCESS, space:ColorSpace.CMYK, colorValue:[69,46,85,46]}

app.findTextPreferences = app.changeTextPreferences = app.findChangeTextOptions = null;
app.findTextPreferences.appliedFont = app.font
...

Votes

Translate

Translate
Community Expert ,
Feb 03, 2024 Feb 03, 2024

Copy link to clipboard

Copied

Should be:

 

newTextFrames[i].texts[0].pointSize = 35;

 

Votes

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 ,
Feb 03, 2024 Feb 03, 2024

Copy link to clipboard

Copied

I'll try. Thanks! Would you have any suggestions for applying the color ("C=69 M=46 Y=85 K=46") to these "boldenvan" fonts that I'm going to change? And how would you apply the "height and width (keep proportions)" so the box doesn't burst?

Votes

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
Community Expert ,
Feb 03, 2024 Feb 03, 2024

Copy link to clipboard

Copied

I'm not JS guy and I'm replying from my crappy phone - so can't give you working code - but I would suggest, that instead of applying local overrides "one at a time" - you should create and apply ObjectStyle. 

 

Something like:

 

newTextFrames[i].appliedObjectStyle = myDoc.objectStyles("my style");

 

Instead of the line with "= 35;"

 

Votes

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
Community Expert ,
Feb 03, 2024 Feb 03, 2024

Copy link to clipboard

Copied

*Seems* like something that needs little more than Find/Change, perhaps in GREP mode... but as you have the script, that's a valid approach, too.


╟ Word & InDesign to Kindle & EPUB: a Guide to Pro Results (Amazon) ╢

Votes

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
Community Expert ,
Feb 03, 2024 Feb 03, 2024

Copy link to clipboard

Copied

Hi @eusoujpg , Try this—for the fill color you have to create or reference an existing swatch, in this case I did not assume the swatch exists:

 

var d = app.activeDocument;

//create a fill color swatch if it does not exist
var fc = makeSwatch(d, "C=69 M=46 Y=85 K=46");
fc.properties = {model:ColorModel.PROCESS, space:ColorSpace.CMYK, colorValue:[69,46,85,46]}

app.findTextPreferences = app.changeTextPreferences = app.findChangeTextOptions = null;
app.findTextPreferences.appliedFont = app.fonts.item("QUIRKY SPRING"); //oldfont
var res = app.activeDocument.findText();//returns an array of found texts

for (var i = 0; i < res.length; i++){
    res[i].appliedFont = app.fonts.item("BoldenVan");
    res[i].pointSize = 35
    res[i].parentTextFrames[0].fillColor = fc;
    res[i].parentTextFrames[0].textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_AND_WIDTH_PROPORTIONALLY
};   


/**
* Makes a new named Swatch 
* @ param the document to add the color to 
* @ param color name 
* @ return the new swatch 
*/

function makeSwatch(d, n){
    if (d.colors.itemByName(n).isValid) {
        return d.colors.itemByName(n);
    } else {
        return d.colors.add({name:n});
    }
}

Votes

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 ,
Feb 04, 2024 Feb 04, 2024

Copy link to clipboard

Copied

Thanks a lot for the help. I just had a problem with the line "res[i].parentTextFrames[0].fillColor = fc;" (object not defined). And boxing failed to have the "autosizingtype..." option applied. I keep trying here!

Votes

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
Community Expert ,
Feb 04, 2024 Feb 04, 2024

Copy link to clipboard

Copied

It‘s probably because the point size change is causing the text to overflow. Try this, it gets the text frame object before changing anything:

 

var d = app.activeDocument;

//create a fill color swatch if it does not exist
var fc = makeSwatch(d, "C=69 M=46 Y=85 K=46");
fc.properties = {model:ColorModel.PROCESS, space:ColorSpace.CMYK, colorValue:[69,46,85,46]}

app.findTextPreferences = app.changeTextPreferences = app.findChangeTextOptions = null;
app.findTextPreferences.appliedFont = app.fonts.item("QUIRKY SPRING"); //oldfont
var res = app.activeDocument.findText();//returns an array of found texts

var pf;
for (var i = 0; i < res.length; i++){
    pf = res[i].parentTextFrames[0]
    res[i].appliedFont = app.fonts.item("BoldenVan");
    res[i].pointSize = 35;
    res[i].noBreak = true;
    pf.fillColor = fc;
    pf.textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_AND_WIDTH;
};   


/**
* Makes a new named Swatch 
* @ param the document to add the color to 
* @ param color name 
* @ return the new swatch 
*/

function makeSwatch(d, n){
    if (d.colors.itemByName(n).isValid) {
        return d.colors.itemByName(n);
    } else {
        return d.colors.add({name:n});
    }
}

Votes

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
Community Expert ,
Feb 04, 2024 Feb 04, 2024

Copy link to clipboard

Copied

LATEST

@rob day

 

Why not just apply ObjectStyle to the frame - instead of separate properties? 

 

Then you won't have to care about anything. 

 

Votes

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