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

Script for changing font size based on character style

New Here ,
Nov 03, 2021 Nov 03, 2021

Is there a script where I could change the font sizes in a document based on character style? For example:

  1. If character style = "Style1", then change font size to 10pt
  2. If character style = "Style2", then change font size to 13pt
TOPICS
Scripting
882
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 1 Correct answer

Community Expert , Nov 03, 2021 Nov 03, 2021

Okay, here you go. Just change the array where I've commented "change these". Add or subtract lines with [ 'style name', point-size ] with comma between each. - Mark

 

function main() {

    setCharacterStylesPointSize(

        // the document
        app.activeDocument,

        [
            // change these:
            ['Style1', 10],
            ['Style2', 13],
            ['Style3', 20],
            ['Style4', 22]
        ]
    )

    function setCharacterStylesPointSize(doc, changes) {
    
...
Translate
Community Expert ,
Nov 03, 2021 Nov 03, 2021

It is possible with a script, but I need to ask: why not just change the point size *in* the character styles? I think there may more to your question.

- Mark

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
New Here ,
Nov 03, 2021 Nov 03, 2021

Thanks Mark. The only problem with that is I have about 15 character styles and was hoping for a more streamlined way to do it. If all else fails, I could just update the character styles.

 

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 ,
Nov 03, 2021 Nov 03, 2021

I think I see: so you need to change these point sizes in a predictable way, again-and-again, and you don't want to do it manually? Then maybe what you need is a script that changes the font sizes in the character styles? So you'd need to specify the changes in the script, eg. setStylesFontSize([ ['Style1',10], ['Style2',13], ...  ]). It wouldn't be hard to write the script, but it would take much longer than the time it would take to change 15 character styles manually, so you'd want to judge if it was worth it.

- Mark

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
New Here ,
Nov 03, 2021 Nov 03, 2021

Exactly! I create wedding magazines and we use a template, so we do this over and over again. Long term I think it would save time to produce the script that does it. I just don't know where to start, or if there's a framework out there to begin with?

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 ,
Nov 03, 2021 Nov 03, 2021

Okay, here you go. Just change the array where I've commented "change these". Add or subtract lines with [ 'style name', point-size ] with comma between each. - Mark

 

function main() {

    setCharacterStylesPointSize(

        // the document
        app.activeDocument,

        [
            // change these:
            ['Style1', 10],
            ['Style2', 13],
            ['Style3', 20],
            ['Style4', 22]
        ]
    )

    function setCharacterStylesPointSize(doc, changes) {
        for (var i = 0; i < changes.length; i++) {
            var style = doc.characterStyles.itemByName(changes[i][0]),
                size = changes[i][1];
            if (style.isValid) style.pointSize = size;
        }
    }

} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Update Character Styles');

 

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
New Here ,
Nov 03, 2021 Nov 03, 2021
LATEST

THANK YOU! This worked perfectly!!

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