Skip to main content
dublove
Legend
November 11, 2025
Answered

How to quickly read data from paragraph style names using a script?

  • November 11, 2025
  • 1 reply
  • 121 views

I've done this before, but it always feels a bit slow. It seems to take a long time.


I need to first check if the myDat group exists, then verify if it contains the dat1@A@B@C style.
If the myDat group doesn't exist or the dat1@A@B@C style is absent, create them.

If they exist, read the values of A, B, and C into temp1, temp2, and temp3 respectively.

Of course, A, B, and C are variables—I'll assign values to them in other ways.

Thanks.

 

Correct answer rob day

The function I use takes around 0.2 secs to create the style if it doesn’t exist and around .08 secs if it does:

 

var doc = app.activeDocument;
var ps = makeParaStyle(doc, "myDat", "dat1@A@B@C")
ps.properties = {fillColor:"Black", fillTint:50}
$.writeln($.hiresTimer * .00000001)
//0.19232576 sec if the style does not exist
//0.08543395 sec if the style already exists

/**
* Makes a new named group and paragraph style 
* @ param the document to add the group and style to 
* @ param group name
* @ param style name
* @ return the new aragraph style 
*/

function makeParaStyle(d, gn, n){
    var g;
    if (d.paragraphStyleGroups.itemByName(gn).isValid) {
        g = d.paragraphStyleGroups.itemByName(gn);
    } else {
        g = d.paragraphStyleGroups.add({name:gn});
    }
    if (g.paragraphStyles.itemByName(n).isValid) {
        return g.paragraphStyles.itemByName(n);
    } else {
        return g.paragraphStyles.add({name:n});
    }
}

1 reply

rob day
Community Expert
Community Expert
November 12, 2025

I've done this before, but it always feels a bit slow.

 

By slow do you mean the time to execute the code? ExtendScript has a timer you can use to check the run time in microsecounds—add this to the end of your scipt to get the time in secounds. How long is it taking your code to run?:

 

$.writeln($.hiresTimer * .00000001)
dublove
dubloveAuthor
Legend
November 12, 2025

Thanks, I'll optimize it.

There might be an issue with the loop that iterates through dat1.

rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
November 12, 2025

The function I use takes around 0.2 secs to create the style if it doesn’t exist and around .08 secs if it does:

 

var doc = app.activeDocument;
var ps = makeParaStyle(doc, "myDat", "dat1@A@B@C")
ps.properties = {fillColor:"Black", fillTint:50}
$.writeln($.hiresTimer * .00000001)
//0.19232576 sec if the style does not exist
//0.08543395 sec if the style already exists

/**
* Makes a new named group and paragraph style 
* @ param the document to add the group and style to 
* @ param group name
* @ param style name
* @ return the new aragraph style 
*/

function makeParaStyle(d, gn, n){
    var g;
    if (d.paragraphStyleGroups.itemByName(gn).isValid) {
        g = d.paragraphStyleGroups.itemByName(gn);
    } else {
        g = d.paragraphStyleGroups.add({name:gn});
    }
    if (g.paragraphStyles.itemByName(n).isValid) {
        return g.paragraphStyles.itemByName(n);
    } else {
        return g.paragraphStyles.add({name:n});
    }
}