Skip to main content
Participating Frequently
March 23, 2023
Question

Write script for applying styles

  • March 23, 2023
  • 3 replies
  • 189 views

Hi,

I want to automate one of my workflows in InDesign for that I need help for writing a script to apply the styles for text imported into InDesign.

 

Raghava

This topic has been closed for replies.

3 replies

rob day
Community Expert
Community Expert
March 25, 2023

Hi Raghava, Here’s an example of how you can get and set paragraph styles:

 

//get a document style named "Your Style Name":
var myStyle = app.documents[0].paragraphStyles.itemByName("Your Style Name")

//gets an array of all the document’s paragraphs:
var p = app.documents[0].stories.everyItem().paragraphs.everyItem().getElements();

//loop thru the array check if a style was imported:
var isImport
for (var i = 0; i < p.length; i++){
    
    //get the name of the paragraph’s paragraph style:
    $.writeln(p[i].appliedParagraphStyle.name)
    
    //returns true if the style was imported with the text:
    isImport = p[i].appliedParagraphStyle.imported
    
    //set the paragraph’s style to myStyle if the style exists and was NOT imported:
    if (myStyle.isValid && !isImport) {
        p[i].appliedParagraphStyle = myStyle
    } 
};  

 

Community Expert
March 25, 2023

Just a terse statement of what you want won't attract any suggestions for something like writing a script. Better explain what you tried or are thinking of writing, some code snippet that we can build upon. Also, look at solutions or ways that already exist like the one mentioned by @Mike Witherell

-Manan

-Manan
Mike Witherell
Community Expert
Community Expert
March 23, 2023

I don't know how to write a script for that, but have you experimented with the new feature called "Style Packs"? Not only can you apply the existing Style Packs, but it makes it possible to make your own customized Style Pack styles.

Mike Witherell