Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Update script to find and replace paragraph style names

Explorer ,
Jun 11, 2021 Jun 11, 2021

Copy link to clipboard

Copied

Could anyone help me tweak this script to search and replace a certain character string within the name of the paragraph style, rather than just searching for a whole name? I'm sure it's easy, I'm just not sure how to change it:

 

main();

function main() {
	try { // if something goes wrong in the try-catch block, the batch processor won't stop here. It will log the error message and continue further	
		var parStylesList = [
			["H1", "BKRM_H1"], // [ "find this", "change to this" ]
			["H2", "BKRM_H2"],
			["H3", "BKRM_H3"],
			["H4", "BKRM_H4"],
			["TXT", "BKRM_TXT"],
			["UK ", ""] // no comma after the last element!
		];	
		
		var paragraphStyle, characterStyle,
		doc = app.activeDocument, // The frontmost document
		paragraphStyles = doc.allParagraphStyles;
		
		// Change in paragraph styles
		for (var p = 2; p < paragraphStyles.length; p++) {
			paragraphStyle = paragraphStyles[p];
			for (var i = 0; i < parStylesList.length; i++) {
				if (paragraphStyle.name == parStylesList[i][0]) {
					paragraphStyle.name = parStylesList[i][1];
				}
			}
		}
	}
	catch(err) { // if an error occurs, catch it and write the  document's name, error message and line# where it happened into the log
		gErrorLog.push(doc.name + " - " + err.message + ", line: " + err.line);
	}
}
TOPICS
Scripting

Views

321

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Adobe Community Professional , Jun 11, 2021 Jun 11, 2021
Just set up your multiarray with those: [ ["UK Body", "Body"], ["UK Title", "Title"],  ] Etc. 

Likes

Translate

Translate
Adobe Community Professional , Jun 19, 2021 Jun 19, 2021
If it ain't broke, don't fix it, right?  But, what you were going for originally was:  var allStyles = app.activeDocument.allParagraphStyles; var msg = ""; for (var i = 0; i < allStyles.length; i++) { try { allStyles[i].name = allStyles[i].name.replace(/^UK /g,""); } catch(e) { msg += e + "\n"; } } if (msg != "") { alert(msg); }

Likes

Translate

Translate
Adobe Community Professional ,
Jun 11, 2021 Jun 11, 2021

Copy link to clipboard

Copied

Change this line

 

 

if (paragraphStyle.name == parStylesList[i][0])

 

To:

if (paragraphStyle.name.indexOf(parStylesList[i][0]) >= 0)

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 11, 2021 Jun 11, 2021

Copy link to clipboard

Copied

OK... but how would I capture the "find" bit and replace it with the "replace" bit in the manner of 

IE if my style name is "UK body" I want it to just be "body" afterward!

I'm not just adding your suggestion would do that?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 11, 2021 Jun 11, 2021

Copy link to clipboard

Copied

Could I use a .replace() somehow?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 11, 2021 Jun 11, 2021

Copy link to clipboard

Copied

The second index in the multiarray is the replace value. Ie

 

["UK", "body"], 

 

Would change any paragraph style name containing the phrase "UK" to "body" with my modification. 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 11, 2021 Jun 11, 2021

Copy link to clipboard

Copied

Yes... But what I need to do is replace the UK in all the style names:

 

 

 

UK Body

 

UK Title

 

UK Bullets

 

 

 

Should become:

 

Body

 

Title

 

Bullets

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 11, 2021 Jun 11, 2021

Copy link to clipboard

Copied

Just set up your multiarray with those:

[

["UK Body", "Body"],

["UK Title", "Title"], 

]

 

Etc. 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 19, 2021 Jun 19, 2021

Copy link to clipboard

Copied

OK... I was hoping for a more elegant solution, but this worked in the end.

Of course, if I do this method then there's no need to tweak the if statement.

 

Thank you!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 19, 2021 Jun 19, 2021

Copy link to clipboard

Copied

LATEST

If it ain't broke, don't fix it, right? 

 

But, what you were going for originally was: 

var allStyles = app.activeDocument.allParagraphStyles;
var msg = "";
for (var i = 0; i < allStyles.length; i++) {
   try {
       allStyles[i].name = allStyles[i].name.replace(/^UK /g,"");
   } catch(e) { msg += e + "\n"; } 
}
if (msg != "") { 
    alert(msg);
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines