Skip to main content
Participant
July 25, 2024
Answered

Script for Assigning Paragraph Styles to text with similar settings

  • July 25, 2024
  • 2 replies
  • 2545 views

I'm looking for a script or some way to easily assign different paragraph styles to hundreds of text boxes inside a document all based on the format of the text. All of the text is identical in format, but on different pages. Obviously, I could just go and create styles based on the first page's text and then go and assign it on each subsequent page, but that is hundreds of pages and I'd like to avoid that if I can. 

 

The backstory: I have a designer who reports to me that is unfamiliar with Paragraph styles, but they just completed a document that I would like to make more evergreen. They ran a data merge, but didn't assign any styles prior to creating the merged document and then had hours of work afterwards adjusting graphics on each page. All of the text is the same and based on the initial text box, so is there a faster way than going through one by one to assign styles to all of the pages? That way, if anything needs changed moving foward we can easily adjust the style instead of having to adjust each page individually. 

 

Any help would be greatly appreciated — I'm new to using scripts and have no idea how to make them myself, but after using a few to speedup my workflow I figure it's worth asking before doing it all manually. 

Correct answer rob day

This would get all of the document’s text frames where the first word is "Compliments" then style the next 5 lines:

 

var doc = app.activeDocument;
var api = doc.allPageItems;

var tf;
for (var i = 0; i < api.length; i++){
    //gets all text frames where the first word of the frame is "Compliments"
    if (api[i].constructor.name  == "TextFrame" && api[i].paragraphs[0].words[0].contents == "Compliments") {
        tf = api[i]
        //Assigns Paragraph Styles named Name, Title, Email, Phone, and Company to lines 2 thru 6
        tf.paragraphs[1].appliedParagraphStyle = makeParaStyle(doc, "Name")
        tf.paragraphs[2].appliedParagraphStyle = makeParaStyle(doc, "Title")
        tf.paragraphs[3].appliedParagraphStyle = makeParaStyle(doc, "Email")
        tf.paragraphs[4].appliedParagraphStyle = makeParaStyle(doc, "Phone")
        tf.paragraphs[5].appliedParagraphStyle = makeParaStyle(doc, "Company")
    } 
};   


/**
* Makes a new named ParagraphStyle if it does not already exist 
* @ param the document to add the style to 
* @ param style name 
* @ return the new paragraph style 
*/

function makeParaStyle(d, n){
    if (d.paragraphStyles.itemByName(n).isValid) {
        return d.paragraphStyles.itemByName(n);
    } else {
        return d.paragraphStyles.add({name:n});
    }
}

 

 

2 replies

rob day
Community Expert
Community Expert
July 25, 2024

Hi @Zach Field , Just to clarify, do all of the pages have the same number of text frames, and each text frame on page 1 is styled differently?

Participant
July 29, 2024

The original template that we used to generate the merge had one text frame. 

 

It was formatted like this:

 

Rep's Name

Title

Email

Phone number

companyname.com

 

The rep's name was stylized differently, but the remaining text was formatted the same and each has a paragraph break.

 

If it weren't for the edits and deadline , I would have them redo the merge with the proper styles. I may just need to redo the document, but if there was a way to apply styles using a script it would help me with this and other documents. Until I joined the team, no one used styles properly.Most documents were created by duplicating pages and formatting each copy block individually. 

Robert at ID-Tasker
Legend
July 30, 2024

@Zach Field

 

If you work on a PC - it would be a breeze with my ID-Tasker - not free, but I can give you access to the full version for a few days. 

 

You could process only specific pages, layers, locations, etc. And you can easily change number of paragraphs to be styled and their order. 

 

And you can of course process unlimited number of files automatically - even overnight. 

 

jmlevy
Community Expert
Community Expert
July 25, 2024

Can you show us an exemple of the text frames? Maybe you don't need a script, but a simple (or GREP) find and replace could work.

Participant
July 29, 2024

It's a single text frame per spread with the same basic information. 

 

When they did the merge it went as follows:

 

Rep's Name

Title

Email

Phone number

companyname.com

 

Each rep name woul need to be it's own style and then the info that follows could be one paragraph style — I would like some of the elements to be bolded but I know just enough GREP that I think I can easily create a character style and apply those. The format should be consistent on each page with maybe a few exceptions, but so long as I can do a majority automatically I wouldn't mind doing some manual formatting. 

rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
July 29, 2024

This would get all of the document’s text frames where the first word is "Compliments" then style the next 5 lines:

 

var doc = app.activeDocument;
var api = doc.allPageItems;

var tf;
for (var i = 0; i < api.length; i++){
    //gets all text frames where the first word of the frame is "Compliments"
    if (api[i].constructor.name  == "TextFrame" && api[i].paragraphs[0].words[0].contents == "Compliments") {
        tf = api[i]
        //Assigns Paragraph Styles named Name, Title, Email, Phone, and Company to lines 2 thru 6
        tf.paragraphs[1].appliedParagraphStyle = makeParaStyle(doc, "Name")
        tf.paragraphs[2].appliedParagraphStyle = makeParaStyle(doc, "Title")
        tf.paragraphs[3].appliedParagraphStyle = makeParaStyle(doc, "Email")
        tf.paragraphs[4].appliedParagraphStyle = makeParaStyle(doc, "Phone")
        tf.paragraphs[5].appliedParagraphStyle = makeParaStyle(doc, "Company")
    } 
};   


/**
* Makes a new named ParagraphStyle if it does not already exist 
* @ param the document to add the style to 
* @ param style name 
* @ return the new paragraph style 
*/

function makeParaStyle(d, n){
    if (d.paragraphStyles.itemByName(n).isValid) {
        return d.paragraphStyles.itemByName(n);
    } else {
        return d.paragraphStyles.add({name:n});
    }
}