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

Apply object styles to XML elements

Explorer ,
Oct 02, 2020 Oct 02, 2020

Copy link to clipboard

Copied

Please forgive me for any questions that have an obvious answer. I am fairly new and limited in my javascript abilities, and I'm really struggling with the InDesign API for scripting.

Is it possible to select a set of XML elements in the structure panel and apply object styles to them? I feel like I should be selecting the specific XML elements as an array and then applying the object styles to them. I can't seem to figure out how to do either within the InDesign API. I don't see anything for appling object styles. Is this even possible to do with scripting?

 

And secondly, I can't seem to find anything in the API documentation that specifies how to select XML elements as an array. I know there's a way to use evaluateXPathExpression, but I can't seem to figure out the combination of classes, objects, properties and methods to use to make it work. I can use it with XMLElement but not XMLElements, and I can't figure out the context for XMLElement.

I'm not looking for someone to teach me more complex javascript or write it for me. What would be really helpful is a pointer in the right direction. I've been working on this for hours, and it seems like it should be simple and straightforward, but I've been working at this for hours, and I am totally lost.

I am working on Windows 10 in InDesign 15.0. Any help is greatly appreciated!

TOPICS
How to , Scripting

Views

593

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 , Oct 05, 2020 Oct 05, 2020

First of all, Object styles can be applied on objects like Images, textFrames, rectangles polygon & lines etc.

Object Styles are specifically made only for objects not XML Element. That is why you are not getting any documentation to apply object style to xml structure.

 

Second point, you can select multiple xml element at once using javaScript.

Lets just say anyhow you have two different nodes in two variable say firstNode & secondNode.

app.select([firstNode, secondNode]);

 Or let us just say

...

Votes

Translate

Translate
Advocate ,
Oct 05, 2020 Oct 05, 2020

Copy link to clipboard

Copied

LATEST

First of all, Object styles can be applied on objects like Images, textFrames, rectangles polygon & lines etc.

Object Styles are specifically made only for objects not XML Element. That is why you are not getting any documentation to apply object style to xml structure.

 

Second point, you can select multiple xml element at once using javaScript.

Lets just say anyhow you have two different nodes in two variable say firstNode & secondNode.

app.select([firstNode, secondNode]);

 Or let us just say you have all those nodes which you want to select in an array say allNodesInAnArray, then you can select all nodes at once like this:

app.select(allNodesInAnArray);

 

And third If you want to find xml nodes using xPath then there are several ways like evaluateXPathExpression & XMLRuleProcessor:

This is link for previous post for finding XML Nodes and doing something on it.

////////////////////////////////////////////////////////////////////////

try{
    var myDoc = app.documents[0];
    var allNodes = myDoc.xmlElements[0].evaluateXPathExpression("//*[@*[name()='aid:pstyle']]");
    for(var i = 0; i < allNodes.length; i++){
        var styleName = allNodes[i].xmlAttributes.item("aid:pstyle").value.toString();
        if(myDoc.paragraphStyles.item(styleName).isValid){
            allNodes[i].paragraphs.everyItem().appliedParagraphStyle = styleName;
            }
        }
    }
catch(e){}

////////////////////////////////////////////////////////////////////////

And this is second link to fetch XML Element from InDesign using XML Rule Processor.

getXMLElement();
function getXMLElement(){
    var myDoc = app.documents[0];
    var xPath = ["//p"];
    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.select();
                }
            }
        }
    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