Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
var myStyle = myDocument.paragraphStyles.item("Teaser");
myStory.applyParagraphStyle(myStyle",true);
Harbs
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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...
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
"Teaser" will not work. You need a ParagraphStyle reference, not a string...
Harbs
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
Of course, my example will only work if all style names are unique. If not, you'll have to check the style.parent...
Harbs
Copy link to clipboard
Copied
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
Find more inspiration, events, and resources on the new Adobe Community
Explore Now