Skip to main content
vinny38
Legend
June 23, 2017
Answered

Checking if a para style is based on [Basic Paragraph]

  • June 23, 2017
  • 2 replies
  • 1391 views

Hi community

I'd like to implement a small script that would allow to tell me if there is any para style that is based on [Basic]

And more (and that's where I'm stuck), loop through any other para which would be based on para based on [Basic].

Please see below my code:

I can reach para A which is based on [basic]

I can reach para B which is based on [basic]

But what for C based on B or D based on C... and so on.

It's basically a loop "problem", that my poor JS skills can't solve.

Thanks a lot for your help.

Vinny

var myPstyles = app.activeDocument.allParagraphStyles;

var myBasic = '[Paragraphe standard]' // Change this according to your language (be careful: Upper/Lower case matters)

for (i = 1; i < myPstyles.length; i++) {

  if (myPstyles.basedOn.name === myBasic) {

    alert(myPstyles.name + ' is based on ' + myBasic);

    for (j = 1; j < myPstyles.length; j++) {

      if (myPstyles.basedOn.name === myPstyles.name) {

        alert(myPstyles.name + ' is based on ' + myPstyles.name + ' which is based on ' + myBasic);

      }

    }

  }

}

This topic has been closed for replies.
Correct answer Laubender

Hi Vinny,

now that I tested again Chinna's snippet on my sample you are seeing in the screenshot #3 I see that the snippet's output is:

Style 1

Style 2

Style 1

"Style 3" is missing in the list.

It is based on "Style 2" that is based on "Style 1" that is based on "[Basic Paragraph Style]".

The if statement in the loop is not sufficient to see that "Style 3" is indirectly based on "[Basic Paragraph Style]".

As I said earlier: All paragraph styles are indirectly based on paragraphStyles[0].

We can do a while loop to travel in the hierarchy of based-on-styles from the current investigated paragraph style in the loop up to paragraphStyles[0]. If "[Basic Paragraph Style]" is found on the way the current style should be saved with the output array.

Harbs has it right by showing the hierarchy of dependencies with his script "Based on Styles".

Did you try Harbs' script?

Below my script that is using a while loop to see into dependencies with basedOn.

Now all four styles are correctly recognized with my sample document.

One little obstacle writing this:

basedOn returns a string and no paragraphStyle object if a style is based on paragraphStyles[0]—the "[No Paragraph Style]" style.

Don't ask me why. I knew it, but nearly forgot about it.

var doc = app.activeDocument;

var pstyles = doc.allParagraphStyles;

var pstylesLength = pstyles.length;

var basedOnBasicStyleNames = [];

var basedOnBasicStyleStyles = [];

var noParagraphStyle = doc.paragraphStyles[1].basedOn;

var basicParagraphStyle = doc.paragraphStyles[1];

for(var n=2;n<pstylesLength;n++)

{

  var currentStyle = pstyles;

  var basedOnStyle = currentStyle.basedOn;

  while( basedOnStyle !== noParagraphStyle )

  {

    if( basedOnStyle == basicParagraphStyle )

    {

      // If you want the names only:

      basedOnBasicStyleNames[basedOnBasicStyleNames.length++] =

      currentStyle.name;

      // If you want the styles:

      basedOnBasicStyleStyles[basedOnBasicStyleStyles.length++] =

      currentStyle;

      break

    }

    basedOnStyle = basedOnStyle.basedOn;

  };

};

// Output in index order of allParagraphStyles

// Names:

$.writeln( basedOnBasicStyleNames.join("\r") );

// Paragraph styles listed:

$.writeln( basedOnBasicStyleStyles.join("\r") );

// FWIW: Showing also IDs from the pageItem class of document and paragraph styles:

$.writeln( basedOnBasicStyleStyles.toSource().replace(/,/g,"\r") );

// You can loop now the basedOnBasicStyleStyles array

// to do something with the styles directly.

Regards,
Uwe

2 replies

vinny38
vinny38Author
Legend
June 27, 2017

Thanks to you two.

Both answers were very helpful.

As far as I understand (and tested), Chinndk script catches "indirectly based on" [basic], as well as styles with same name, put in different groups.

Apart from the smart idea of defining var basic = doc.paragraphStyles[1].name;

I don't really understand your explanation Uwe: far too technical for me I'm afraid.

I don't the .prototype part either, but I'll look into that.

Thanks!

Community Expert
June 27, 2017

vinny38  wrote

… As far as I understand (and tested), Chinndk script catches "indirectly based on" [basic], as well as styles with same name, put in different groups. …

Hi Vinny,
I will test Chinndk's script again, but I thought it gave an incomplete output when I tested the first time with CS6 on my Mac.

Regards,
Uwe

Chinnadk
Legend
June 23, 2017

Try this,

Array.prototype.contains = function(obj)

{

        for(var i =0;i<this.length;i++)

        {

                if(this === obj)

                {

                        return true;

                    }

            }

        return false;

    }

var doc = app.activeDocument;

var pstyles = doc.allParagraphStyles;

var basic = "[Basic Paragraph]";

var basedOnBasicStyles = [];

for(var i=2;i<pstyles.length;i++)

{

        if(pstyles.basedOn.name == basic || basedOnBasicStyles.contains(pstyles.basedOn.name))

        {

                $.write(pstyles.name + "\r");

                basedOnBasicStyles.push(pstyles.name);

            }

    }

vinny38
vinny38Author
Legend
June 23, 2017

Hey. Thanks for your answer.

I'll try this om Monday, week end time now^^, but it's looking great