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

Can the script read the paragraph style name as a script setting value?

Guide ,
Jul 31, 2025 Jul 31, 2025

For example, my layout only has one column.
However, my text box has three columns with a column spacing of 6 mm.
But the settings for each ID document may be different.

If frequent switching may cause errors,

 

I came up with a solution to set 3 and 6 in a paragraph style.
For example, the paragraph style is named: columnSeting@3@6

When I run the script, it automatically sets 3 as the number of columns and 6 as the spacing.

 

Are there any other better methods?

66600000.png

TOPICS
How to , Scripting
302
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 , Aug 04, 2025 Aug 04, 2025

This makes a group and returns a Pstyle if they do not exist, or returns the existing Pstyle if it does

 

var ps = addParaStyleGroup(app.activeDocument,"myGroup","my@SpaceCol@7@3@")
$.writeln(ps.name)
//returns the style’s name my@SpaceCol@7@3@


/**
* Returns a ParagraphStyle named sn in a group named g 
* @ param the document to add the group and style 
* @ param the paragraphStyle group name 
* @ param the paragraphStyle name 
* @ returns the paragraph style named sn 
*/

function addParaSty
...
Translate
Guide ,
Jul 31, 2025 Jul 31, 2025

This should be a very practical method.
It's much more convenient than modifying the JSON every time.

 

However, the style names are not fixed, so they are difficult to find. The only fixed part is that they start with “columnSeting@

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
Guide ,
Aug 03, 2025 Aug 03, 2025

Thank you very much.
No need, I seem to have found the direction.
I went through all the paragraph 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
Guide ,
Aug 04, 2025 Aug 04, 2025

@rob day 

I did it, but I have some doubts.

Not found the first time, why is it still 0 after creating a new style?

checkColSet();
alert(csp);
alert(cn);

Is the variable occupied? Do you need to clear the cache?

Below is the complete code:

//The reference mark is SpaceCol.
var colSetParStn = "my@SpaceCol@7@3@";
var myDoc = app.activeDocument;

//Reference settings
var myTemp = colSetParStn.replace(/^[^@]+@/, "").replace(/\.jsx$/, "").split('@');
//var changeTo = decodeURIComponent(myTemp[0]);
var startMark = myTemp[0];
//alert(startMark);
var myParas = myDoc.allParagraphStyles;
var csp = 0, cn = 0;

//First, check if there is a target paragraph style. If there is, read it.
checkColSet();
function checkColSet() {
    for (var i = 0; i < myParas.length; i++) {
        getParaName = myParas[i].name.replace(/^[^@]+@/, "").replace(/\.jsx$/, "").split('@');
        if (startMark == getParaName[0]) {
            csp = getParaName[1];
            cn = getParaName[2];
        }
    }
}

//If csp is not present, cp remains 0. Create a new paragraph style.
if (cn == 0 || csp == 0) {
    SetParGroup();
}
//Double-check
checkColSet();
alert(csp);
alert(cn);

function SetParGroup() {
    var wn = [colSetParStn];
    for (var i = 0; i < wn.length; i++) {
        w = newParGroStyle(app.activeDocument, "myGroup", wn[i])
    };
}

function newParGroStyle(d, g, n,) {
    // Get the group by name
    var group = d.paragraphStyleGroups.itemByName(g);
    // If group exists, check or create style
    if (!group.isValid)
        group = d.paragraphStyleGroups.add({ name: g })
    if (!group.paragraphStyles.itemByName(n).isValid)
        group.paragraphStyles.add({ name: n });
}

 

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 04, 2025 Aug 04, 2025

Your script throws multiple errors— myParas, cn, cps, colSetParStn are not defined

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
Guide ,
Aug 04, 2025 Aug 04, 2025

Okay, no paragraph style updates.

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 04, 2025 Aug 04, 2025

I don’t understand what StartParGroup function does

 

function SetParGroup() {
    var wn = [colSetParStn];
    $.writeln(wn.length)
    //if this function runs length always returns 1, so why the loop?
    $.writeln(wn[0])
    //returns "my@SpaceCol@7@3@", but only when wn.length = 1
    for (var i = 0; i < wn.length; i++) {
        w = newParGroStyle(app.activeDocument, "myGroup", wn[i])
    };
}
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
Guide ,
Aug 04, 2025 Aug 04, 2025

Are you referring to SetParGroup()?
I would like to assign certain paragraph styles to specific groups.

First, iterate through all paragraph styles. If SpaceCol is present, read the content after @ as csp and cn.

If not, create a new paragraph style named my@SpaceCol@7@3@.

 

Before the second checkColSet() check, add the following again:
var myParas = myDoc.allParagraphStyles;

This refreshes the paragraph styles.

dublove_0-1754324520223.jpeg

 

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 04, 2025 Aug 04, 2025

This makes a group and returns a Pstyle if they do not exist, or returns the existing Pstyle if it does

 

var ps = addParaStyleGroup(app.activeDocument,"myGroup","my@SpaceCol@7@3@")
$.writeln(ps.name)
//returns the style’s name my@SpaceCol@7@3@


/**
* Returns a ParagraphStyle named sn in a group named g 
* @ param the document to add the group and style 
* @ param the paragraphStyle group name 
* @ param the paragraphStyle name 
* @ returns the paragraph style named sn 
*/

function addParaStyleGroup(d,g,sn){
    //check if group exists if not make a group named g
    if (d.paragraphStyleGroups.itemByName(g).isValid) {
        g = d.paragraphStyleGroups.itemByName(g);
    } else {
        g =  d.paragraphStyleGroups.add({name:g});
    }
    //check if a p style named sn exists if not make
    if (g.paragraphStyles.itemByName(sn).isValid) {
        return g.paragraphStyles.itemByName(sn);
    } else {
        return g.paragraphStyles.add({name:sn})
    } 
} 

 

 

Screen Shot 19.png

 

 

 

Screen Shot 20.png

 

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
Guide ,
Aug 04, 2025 Aug 04, 2025
LATEST

Hi rob day.

Thank you very much.

 

I've already achieved it using a clumsy method.

@7@3@It's not fixed, only my@SpaceCol is fixed.
You still need to iterate through all paragraph 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