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

How to apply a paragraph style to a selected paragraph

Community Beginner ,
Sep 09, 2011 Sep 09, 2011

Hi,

I just can't figure out how to apply an existing paragraph style named "Teaser" to a selectet paragraph

var myStory = app.selection[0].parentStory;

myStory.applyParagraphStyle("Teaser ",true);

Won't work. Any ideas?

Thanks!

TOPICS
Scripting
1.8K
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
LEGEND ,
Sep 09, 2011 Sep 09, 2011

var myStyle = myDocument.paragraphStyles.item("Teaser");

myStory.applyParagraphStyle(myStyle",true);

Harbs

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 ,
Sep 09, 2011 Sep 09, 2011

Hello, Jörg!

First of all your script indicates that you are not working with a selection that is part of one paragraph, but with the whole story of your selection ("Textabschnitt" in german) which can consists of a multitude of paragraphs!

To apply a paragraph style to your selection only try the following:

//if selection is part of exactly ONE paragraph:
if(app.selection[0].paragraphs.length === 1){
    app.selection[0].appliedParagraphStyle = "Teaser";
    };

//if selection is part of MORE than one paragraph:
if(app.selection[0].paragraphs.length > 1){
    for(var n=0;n<app.selection[0].paragraphs.length-1;n++){
        app.selection[0].appliedParagraphStyle = "Teaser";      
        };
    };

Uwe

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
LEGEND ,
Sep 09, 2011 Sep 09, 2011

Laubender,

That's not right. You can apply a style to more than one paragraph at once.

Also, your loop is doing the exact same action over and over...

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 ,
Sep 09, 2011 Sep 09, 2011

Harbs,

thank you for your comment. So it's sufficent to use:

app.selection[0].appliedParagraphStyle = "Teaser";

for formatting the whole selection, be it a single insertion point, a line of text, one or a bunch of paragraphs, even the whole story.
No loop required.

In the case that the user wants to format a single paragraph and we cannot be sure that he selects only one, only then we need an if-statement and deal with the situation in a different way.

Uwe

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
LEGEND ,
Sep 09, 2011 Sep 09, 2011

"Teaser" will not work. You need a ParagraphStyle reference, not a string...

Harbs

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 ,
Sep 09, 2011 Sep 09, 2011

Harbs,

hm, just tested and it does work…

(InDesign CS5.5 7.5.1)

All the same with:

app.selection[0].appliedParagraphStyle = app.documents[0].paragraphStyles.itemByName("Teaser");


Uwe

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
LEGEND ,
Sep 09, 2011 Sep 09, 2011

You are right. It does seem to work. I'm not sure why I thought it doesn't.

It does not work for styles in style groups, but then again, neither will the simplistic approach I used...

Harbs

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 Beginner ,
Sep 09, 2011 Sep 09, 2011

Hi Harbs, hi Laubender

thank you so much for you answers.

Now I see what my problem is: all my paragraph styles are grouped.

app.selection[0].appliedParagraphStyle = "Teaser";

works only, if the style is not grouped.

Now I just need help in how to set the style within a group, let's say the group is named "headers"

Jörg

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
LEGEND ,
Sep 09, 2011 Sep 09, 2011

var myStyle = getParStyle(myDocument,"Teaser");

function getParStyle(doc,name){

  var styles = doc.allParagraphStyles;

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

    if(styles.name == name){

      return styles;

    }

  }

  return null;

}

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
LEGEND ,
Sep 09, 2011 Sep 09, 2011
LATEST

Of course, my example will only work if all style names are unique. If not, you'll have to check the style.parent...

Harbs

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 ,
Sep 09, 2011 Sep 09, 2011

Harbs,

the documentation of the object model shows that:

appliedParagraphStyle

The paragraph style applied to the text. Can also accept: String.

For styles in groups I think we have to loop through allParagraphStyles and check for the name property:

//The first 2 we need not to loop:

for (var n=2;n<app.documents[0].allParagraphStyles.length;n++){
    if(app.documents[0].allParagraphStyles.name == "Teaser"){
        app.selection[0].appliedParagraphStyle = app.documents[0].paragraphStyles.itemByID(app.documents[0].allParagraphStyles.id);
        };
    };

Of course we also have to check if there is more than one paragraph style named "Teaser" and let the user decide which one to apply.

Uwe

Edit: your function is more elegant…

Message was edited by: Laubender

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