Highlighted

How to apply character style

Community Beginner ,
Jun 20, 2020

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

Rajendran
Adobe Community Professional
Correct answer by Sunil_Yadav1 | Adobe Community Professional

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

Topics

Scripting

Views

150

Likes

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

How to apply character style

Community Beginner ,
Jun 20, 2020

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

Rajendran
Adobe Community Professional
Correct answer by Sunil_Yadav1 | Adobe Community Professional

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

Topics

Scripting

Views

151

Likes

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
Adobe Community Professional ,
Jun 20, 2020

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

Likes

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
Reply
Loading...
Engaged ,
Jun 21, 2020

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();

 

Yusuke S.

Likes

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
Reply
Loading...
Adobe Community Professional ,
Jun 21, 2020

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

Likes

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
Reply
Loading...
yusuke_s LATEST
Engaged ,
Jun 21, 2020

Copy link to clipboard

Copied

Thank you for pointing that out.
Apparently I made a mistake...

Yusuke S.

Likes

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
Reply
Loading...