• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to apply character style

Community Beginner ,
Jun 20, 2020 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
TOPICS
Scripting

Views

510

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
community guidelines

correct answers 1 Correct answer

Advocate , Jun 20, 2020 Jun 20, 2020

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 !=

...

Votes

Translate

Translate
Advocate ,
Jun 20, 2020 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

Votes

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
community guidelines
Engaged ,
Jun 21, 2020 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.

Votes

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
community guidelines
Advocate ,
Jun 21, 2020 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

Votes

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
community guidelines
Engaged ,
Jun 21, 2020 Jun 21, 2020

Copy link to clipboard

Copied

LATEST

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

Yusuke S.

Votes

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
community guidelines