Skip to main content
Known Participant
February 3, 2024
Answered

Script to change font Change color

  • February 3, 2024
  • 3 replies
  • 1213 views

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?

This topic has been closed for replies.
Correct answer rob day

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});
    }
}

3 replies

rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
February 3, 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.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});
    }
}
eusoujpgAuthor
Known Participant
February 4, 2024

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!

rob day
Community Expert
Community Expert
February 4, 2024

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});
    }
}
James Gifford—NitroPress
Legend
February 3, 2024

*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.

Robert at ID-Tasker
Legend
February 3, 2024

Should be:

 

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

 

eusoujpgAuthor
Known Participant
February 3, 2024

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?

Robert at ID-Tasker
Legend
February 3, 2024

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;"