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);
}
}
Just set up your multiarray with those:
[
["UK Body", "Body"],
["UK Title", "Title"],
]
Etc.
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);
}
Copy link to clipboard
Copied
Change this line
if (paragraphStyle.name == parStylesList[i][0])
To:
if (paragraphStyle.name.indexOf(parStylesList[i][0]) >= 0)
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?
Copy link to clipboard
Copied
Could I use a .replace() somehow?
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.
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
Copy link to clipboard
Copied
Just set up your multiarray with those:
[
["UK Body", "Body"],
["UK Title", "Title"],
]
Etc.
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!
Copy link to clipboard
Copied
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);
}