Skip to main content
Known Participant
September 2, 2022
Answered

Script to automatically rename Paragraph Styles based on content

  • September 2, 2022
  • 1 reply
  • 984 views

I am creating a template for future documents in a company I work for. There will be about 50-60 Paragraph styles based on various fonts and sizes etc. At the moment I am mannually namings these

Title Font Name Weight Size Justification Colour
"H4 Space Grotesk Bold 30 Centre White"

Say I decide to change the font size to 24, I would like to be able to update all the names that have 30 in them to 24. 

Is this possible through scripting?

This topic has been closed for replies.
Correct answer brian_p_dts

Assuming the point size is always offset by spaces and is the only standalone number in the name: 

 

var aps = app.activeDocument.allParagraphStyles;
var i = aps.length;
while(i--) {
   aps[i].name = aps[i].name.replace(/ ^\d+ /g," " + aps[i].pointSize + " ");
}

 

 

 

This also assumes that you've already change the point size in the style. If you wanted to change the point size with a prompt it'd be: 

 

 

var aps = app.activeDocument.allParagraphStyles;
var i = aps.length;
var p1 = prompt("old point size");
var p2 = prompt("new point size");
while(i--) {
   if (aps[i].pointSize == Number(p1)) {
       aps[i].pointSize = Number(p2);
       aps[i].name = aps[i].name.replace(p1, p2);
   }
}

 

 

Haven't tested yet as  I have to take the kids to school

1 reply

Participant
September 2, 2022

If you associate your styles with a reference style using the "based on" property of a text style, you could change all of the derivated text styles in a blink without scripting. But it would change the title attributes for sure. 

For that, indeed a script is necessary and yet it could be a bit challenging due to your naming convention. For exemple, if you change justification from center to letf justified, the script should change the name accordingly. And for that the script would need to know exactly what to change.

Is there any logic beyond your naming convention?

Loic

 

Known Participant
September 2, 2022

Those won't change, the only thing that would change is font size. I will have bold/regular centre left all done but there would be too many font sizes.

I usually start an A4 document with 18 heading, 14 subheading, 11 body, 8 subscript. But say the document is an A1 information poster, the font would be larger. So I don't want to go in and mannually change that. 

The naming convention is really to make sure what I am selecting is right, I have ADHD so I kind of need to spell it out for myself sometimes. 

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
September 2, 2022

Assuming the point size is always offset by spaces and is the only standalone number in the name: 

 

var aps = app.activeDocument.allParagraphStyles;
var i = aps.length;
while(i--) {
   aps[i].name = aps[i].name.replace(/ ^\d+ /g," " + aps[i].pointSize + " ");
}

 

 

 

This also assumes that you've already change the point size in the style. If you wanted to change the point size with a prompt it'd be: 

 

 

var aps = app.activeDocument.allParagraphStyles;
var i = aps.length;
var p1 = prompt("old point size");
var p2 = prompt("new point size");
while(i--) {
   if (aps[i].pointSize == Number(p1)) {
       aps[i].pointSize = Number(p2);
       aps[i].name = aps[i].name.replace(p1, p2);
   }
}

 

 

Haven't tested yet as  I have to take the kids to school