This forum is a goldmine to get exercises for scripting ^^
Try this script and tell me if it works for you:
Please note:
– select a text frame, or place the cursor in order to apply the script to the whole story
– an existing character style called “bold” must exist. You can change the name on line 37 (var myCSName = “bold”;)
– script will clear all paragraphs overrides in the selected story, so be cautious
– if you don’t use the download link below and copy/paste your code, please replace opening/closing quotation marks by straight one.
here’s the download link: https://wetransfer.com/downloads/2887b2f63a8f8b3b0fd0adad10e99e7f20180116105700/19df9614bd16a55c5faf1d7bcc0eef8120180116105700/e08e08
and here’s the javascript:
//Written by Vinny
// Feel free to modify and use
main();
function main() {
//Make certain that user interaction (display of dialogs, etc.) is turned on.
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
if (app.documents.length != 0) {
if (app.selection.length == 1) {
switch (app.selection[0].constructor.name) {
case “Text”:
case “TextFrame”:
case “InsertionPoint”:
case “Character”:
case “Word”:
case “Line”:
case “TextStyleRange”:
case “Paragraph”:
case “TextColumn”:
CheckifCharStyleExists();
break;
default:
alert(“The selected object is not a text object. Select some text and try again.”);
break;
}
} else {
alert(“Please select some text and try again.”);
}
} else {
alert(“No documents are open. Please open a document, select some text, and try again.”);
}
function CheckifCharStyleExists() {
var myDocument = app.documents.item(0);
var myParagraphs = app.selection[0].parentStory.paragraphs;
var myCSName = “bold”;
var myCharacterStyle = myDocument.characterStyles.item(myCSName);
if (myDocument.characterStyles.item(myCSName) == null) {
alert(“\””+myCSName+”\” character style does not exist. Please create it.”);
} else {
AddNestedStyle();
}
function AddNestedStyle() {
app.selection[0].parentStory.clearOverrides(OverrideType.PARAGRAPH_ONLY);
for (i = 0; i < myParagraphs.length; i++) {
if (myParagraphs[i].lines.length > 1) {
var myNestedStyle = myParagraphs[i].nestedStyles.add({
appliedCharacterStyle: myCharacterStyle,
delimiter: NestedStyleDelimiters.ANY_WORD,
inclusive: true,
repetition: 2
});
}
}
}
}
}