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