• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

562

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

Community Expert , Jun 11, 2021 Jun 11, 2021

Just set up your multiarray with those:

[

["UK Body", "Body"],

["UK Title", "Title"], 

]

 

Etc. 

Votes

Translate

Translate
Community Expert , 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);
}

Votes

Translate

Translate
Community Expert ,
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)

Votes

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?

Votes

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?

Votes

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
Community Expert ,
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. 

Votes

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

Votes

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
Community Expert ,
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. 

Votes

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!

Votes

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
Community Expert ,
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);
}

Votes

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