Copy link to clipboard
Copied
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!");
2 Correct answers
There's another problem. I'd forgotten how those tags should be scripted. You should use this format:
app.documents[0].paragraphStyles.item('H1').styleExportTagMaps.add (
'PDF', // exportType
'H1', // exportTag
'', // exportClass
'' //exportAttributes
);
Hi @Phil5C41 you already have @Peter Kahrel's perfect answer so you're probably done, but I wanted to learn a bit about scripting style tag mapping myself, so I wrote a little script that does what your script is trying to do. I figured I may as well share it.
I changed your mapping system so that the tag names are the object keys and the values are RegExps. This is because I wanted to be able to have multiple paragraph styles that would be mapped to the same tag. For example, when headings ap
...Copy link to clipboard
Copied
If your styles are in style groups, then your script doesn't see them. Change this line:
var styles = doc.paragraphStyles;
to:
var styles = doc.allParagraphStyles;
and see if it makes a difference.
Copy link to clipboard
Copied
Thanks, Peter, they are not in Style Groups, just separate styles.
I get the same result, the script seems to run as I get the confirmation alert, but on looking at the Tags, they're all still set to '[Automatic]'.
Copy link to clipboard
Copied
There's another problem. I'd forgotten how those tags should be scripted. You should use this format:
app.documents[0].paragraphStyles.item('H1').styleExportTagMaps.add (
'PDF', // exportType
'H1', // exportTag
'', // exportClass
'' //exportAttributes
);
Copy link to clipboard
Copied
Hi @Phil5C41 you already have @Peter Kahrel's perfect answer so you're probably done, but I wanted to learn a bit about scripting style tag mapping myself, so I wrote a little script that does what your script is trying to do. I figured I may as well share it.
I changed your mapping system so that the tag names are the object keys and the values are RegExps. This is because I wanted to be able to have multiple paragraph styles that would be mapped to the same tag. For example, when headings appear in different colors to match a chapter color theme, we could name them "H1-Chapter 1", "H1-Chapter 2", etc, and the script would tag them all "H1". (This would have worked with your approach—using .indexOf—but it isn't as configurable as RegExp .test and it may give false positives, such as tagging a style named "MONTH1" as "H1".)
- Mark
/**
* @file Assign Paragraph Style Export Tags.js
*
* Will assign a pdf export tag to any matched
* paragraph style in active document.
*
* I have set the `tagMappings` to match a paragraph style name, either:
* - being the tag name by itself eg. "P", or
* - starting with tag name followed by a hyphen, eg. "P-Small" and "P-Medium".
*
* @author m1b
* @version 2025-02-27
* @discussion https://community.adobe.com/t5/indesign-discussions/script-to-automate-the-tagging-of-paragraph-styles-based-on-keyword/m-p/15179157
*/
function main() {
var doc = app.activeDocument;
if (!doc) {
alert("No active document found.");
return;
}
// tag name mapped to style name test RegExp
var tagMappings = {
"H1": /^H1($|-)/,
"H2": /^H2($|-)/,
"H3": /^H3($|-)/,
"H4": /^H4($|-)/,
"H5": /^H5($|-)/,
"H6": /^H6($|-)/,
"P": /^P($|-)/,
};
var paragraphStyles = doc.allParagraphStyles;
var counter = 0;
for (var i = 0; i < paragraphStyles.length; i++) {
var style = paragraphStyles[i];
tagMappingLoop:
for (var tagName in tagMappings) {
if (
tagMappings.hasOwnProperty(tagName)
&& tagMappings[tagName].test(style.name)
) {
var tagMap = style.styleExportTagMaps[0];
if (tagMap.isValid)
// update the first export tag map
tagMap.properties = {
exportTag: tagName,
exportType: 'PDF',
exportClass: '',
exportAttributes: '',
};
else
// add a new export tag map
tagMap = style.styleExportTagMaps.add('PDF', tagName, '', '');
counter++;
break tagMappingLoop; // Stop checking once a match is found
}
}
}
alert('Updated ' + counter + " paragraph styles with export tags.");
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Assign Paragraph Style Export Tags');
Copy link to clipboard
Copied
Thank you, @Peter Kahrel and thanks also to you @m1b, this works well and yes, I had thought about the issue around style names eg 'MONTH1', but hadn't put a fix for that, so what you have supplied is great. Many thanks!!

