Copy link to clipboard
Copied
Hi,
Can anyone helpme to develop a script to italic the contents inside the <em></em> tag in the InDesign document
Try this code:
applyCharacterStyle("em", "myCharacterStyle");
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function applyCharacterStyle(nodeName, characterStyle){
var myDoc = app.documents[0];
var xPath = ["//"+nodeName];
var root = myDoc.xmlElements[0];
var node = null;
try{
var proc = app.xmlRuleProcessors.add(xPath);
var match = proc.startProcessingRuleSet(root);
while (match !=
Copy link to clipboard
Copied
Try this code:
applyCharacterStyle("em", "myCharacterStyle");
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function applyCharacterStyle(nodeName, characterStyle){
var myDoc = app.documents[0];
var xPath = ["//"+nodeName];
var root = myDoc.xmlElements[0];
var node = null;
try{
var proc = app.xmlRuleProcessors.add(xPath);
var match = proc.startProcessingRuleSet(root);
while (match != undefined){
node = match.element;
match = proc.findNextMatch();
if (node != null && node != undefined){
node.texts[0].appliedCharacterStyle = characterStyle;
}
}
}
catch(ex){}
finally{
proc.endProcessingRuleSet();
proc.remove();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Best
Sunil
Copy link to clipboard
Copied
Rajendran_T:
I do that simply with a search and replace:
var tgRange = app.activeDocument.selection[0]; // If the target is a document, delete ".selection[0]"
var tgStyle = app.activeDocument.characterStyles.item("italic"); // Enter the actual character style name
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "<em>.+?</em>";
app.changeGrepPreferences.appliedCharacterStyle = tgStyle;
tgRange.changeGrep();
// If you want to delete the tag itself, remove the following from the comment out.
// app.findGrepPreferences = app.changeGrepPreferences = null;
// app.findGrepPreferences.findWhat = "</?em>";
// app.findGrepPreferences.appliedCharacterStyle = tgStyle;
// app.changeGrepPreferences.changeTo = "";
// tgRange.changeGrep();
Copy link to clipboard
Copied
That means <em> is not a XML element, it is just content inside paragraph?
If it is just content only you can find & replace like that.
But you asked that you need to apply style for a tag not a string....
Anyway good for you.
Best
Sunil
Copy link to clipboard
Copied
Thank you for pointing that out.
Apparently I made a mistake...