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

Function for selecting paragraph style (including groups) JS

People's Champ ,
Jul 15, 2010 Jul 15, 2010

Copy link to clipboard

Copied

Hi,

Here's a function that will create a dropdown box and let you select any paragraph style in the document. It works with paragraph styles in groups. If Cancel is pressed, the function returns -1, else it returns the selected paragraph style.

To call the function, use: myStyle = SelectParagraph();

Nothing very spectacular, but I couldn't find any documentation regarding dealing with paragraph groups, so I thought I'd pass this on. Works with InDesign CS4.

If anyone's got a better way of doing this, I'd be interested to hear.

Ariel

function SelectParagraph(){
mydialog = app.dialogs.add({name:"Paragraphs", canCancel:true});
myStyles = mydoc.allParagraphStyles;
var mystring = [];
for (aa = 0; aa < myStyles.length; aa ++){
mystring[aa] = myStyles[aa].name;
if (myStyles[aa].parent.constructor.name == "ParagraphStyleGroup") mystring[aa]+=" ["+myStyles[aa].parent.name+"]";
}
with (mydialog.dialogColumns.add()){
staticTexts.add({staticLabel:"Please choose:"});
}
with (mydialog.dialogColumns.add()){
mymenu = dropdowns.add({stringList:mystring, selectedIndex:0});
}
if (mydialog.show()) myresult = myStyles[mymenu.selectedIndex]}
else myresult =-1;
mydialog.destroy();
return(myresult);
}

TOPICS
Scripting

Views

2.0K

Translate

Translate

Report

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
Advisor ,
Jul 16, 2010 Jul 16, 2010

Copy link to clipboard

Copied

Hey!

Good script, but you are missing 'mydoc' variable,

and at the end just before 'else' you dont need '}'.

tomaxxi

Votes

Translate

Translate

Report

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
People's Champ ,
Jul 16, 2010 Jul 16, 2010

Copy link to clipboard

Copied

Hi tomaxxi,

Thanks for pointing that out. It was part of a bigger script, so I forgot to adjust

Ariel

Votes

Translate

Translate

Report

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 ,
Jul 16, 2010 Jul 16, 2010

Copy link to clipboard

Copied

Ariel,

Your method works very well when the paragraph styles are embedded just once so if you know that that's always the case your code if fine. But styles can be embedded more deeply, and in that case you need to find all parents of a style. Something like this (by the way, calling an array "mystring" is very, very, confusing):

. . .
. . .
myStyles = myDoc.allParagraphStyles;
var myarray = [];
for (aa = 0; aa < myStyles.length; aa ++)
    myarray.push (get_path (myStyles[aa], myStyles[aa].name));
. . .
. . .
return(myresult);
}


function get_path (style, style_name)
    {
    while (style.parent.constructor.name != 'Document')
        return get_path (style.parent, style.parent.name + ' > ' + style_name);
    return style_name;
    }

This prints ">" between node and child, but you can easily change that to your format.

Peter

Votes

Translate

Translate

Report

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
People's Champ ,
Jul 16, 2010 Jul 16, 2010

Copy link to clipboard

Copied

Peter,

My reaction to your post is:

RECURSION.... AAARGHHH!

It's very neat and compact, though. And yes, I forgot when I was scripting that styles can be in sub-sub-groups.

The line:

while (style.parent.constructor.name != 'Document')

well, according to the Model Viewer, "Application" is also sometimes a parent of a paragraph style (not sure when), so perhaps it would be better:

while (style.parent.constructor.name == "ParagraphStyleGroups")

like I had in my original?

But as for the recursion, I still can't quite get my head around it easily. It kind of climbs all the way up to the top of the paragraph style groups, and then unwinds back down quickly, and somehow along the way we get a full style name, even though nowhere in the code does it say anything like "stylename = stylename + style.parent.name.

My brain hurts...

Ariel

Votes

Translate

Translate

Report

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
LEGEND ,
Jul 16, 2010 Jul 16, 2010

Copy link to clipboard

Copied

Ariel,

Peter wrote the necessary recursive function.

The app is the parent of default app styles. You will never get there from a document style...

Harbs

Votes

Translate

Translate

Report

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
People's Champ ,
Jul 16, 2010 Jul 16, 2010

Copy link to clipboard

Copied

Harbs,

Yes, I know he did. And trying to figure it out how it works makes my brain hurt

Ariel

Votes

Translate

Translate

Report

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 ,
Jul 16, 2010 Jul 16, 2010

Copy link to clipboard

Copied

LATEST

Ariel,

You have to use recursion if you don't know how deep something is embedded. This is the case in almost all tree-like structures in InDesign, such as paragraph style groups, subtopics, XML-elements, files in folders, and whatever else. Recursive functions can be brain teasers, but once you get them working you'll always marvel at their brevity and elegance.

As to stepping through a recursive function, yes, it may look counter-intuitive when you see JS retracing the stack. (Now it almost sounds as if I know what I'm talking about:))

Peter

Votes

Translate

Translate

Report

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