Script to automate the tagging of paragraph styles based on keyword
Hi,
I'm working on a load of files, aligning them to be accessible PDFs, and so trying to automate some of the small, repetitive tasks.
I'm after an InDesign script that can automate the tagging (for PDF) of paragraph styles (within 'Edit all export tags') based on a keyword (eg 'H1', 'H2' etc.) in the style name.
Can anyone shed any light on this, or is it even possible?
This is as far as I got, but it doesn't apply the tags:
var doc = app.activeDocument;
var styles = doc.paragraphStyles;
// Define keyword-to-tag mappings (uppercase)
var tagMappings = {
"H1": "H1",
"H2": "H2",
"H3": "H3",
"H4": "H4",
"H5": "H5",
"H6": "H6",
"P": "P"
};
// Loop through all paragraph styles
for (var i = 0; i < styles.length; i++) {
var style = styles[i];
var styleName = style.name; // Keep original case (uppercase)
// Check if the style name contains a keyword
for (var key in tagMappings) {
if (styleName.indexOf(key) !== -1) { // Match uppercase names
var tagName = tagMappings[key];
// Apply the export tag for PDF export
try {
style.exportTag = tagName; // Set PDF export tag (corrected)
$.writeln("Mapped: " + style.name + " → <" + tagName + "> for PDF export");
} catch (e) {
$.writeln("Error setting export tag for style " + style.name + ": " + e.message);
}
break; // Exit loop once a match is found
}
}
}
alert("Paragraph styles tagged with PDF export tags successfully!");