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

Referencing PageItems associated with XML Elements

New Here ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

I have a .Net app that uses the ServicePortTypeClient to formulate script args and send them to a .jsx script that takes a data-generated json structure as an argument and uses prefab code to convert to reserialize as XML nodes and wraps them in Root and Story nodes. I want to change any prefab code as little as possible and am wondering how do I reference the ExtendScript DOM element associated from a known XML element after the PageItems have all been created.  Do they have the same name and I'm supposed to use XmlElement.item('nameOfItem')?

 

My end goal is to use a Group to repeatedly group 3 elements together (a table header, a table, and an image) so they don't get split up across pages when there's isn't enough space for all three.  (see json below) It looks like I cannot group XmlElements and have to group PageItems, hence the need to find the associated ones to the xml.

 

Sorry for the novice questions and I'm a little baffled by navigating the ExtendScript DOM.

 

Since I'm not able to debug the script curently the best I can do is demonstrate this is the json that gets passed in prior to being converted to XML:

{
    "section": [
        {
            "h1": "general grouping 1",
            "subsection": [
                {
                    "h2": "table description (header) 1",
                    "Image": {
                        "@href": "somefilepath1.jpg"
                    },
                    "Table": "csv values for table1"
                },
                {
                    "h2": "table description (header) 2",
                    "Image": {
                        "@href": "somefilepath2.jpg"
                    },
                    "Table": "csv values for table2"
                },
            ]
        },
        {
            "h1": "general grouping 2",
            "subsection": [
                {
                    "h2": "table description (header) 3",
                    "Image": {
                        "@href": "somefilepath3.jpg"
                    },
                    "Table": "csv values for table3"
                }
            ]
        }
}

 Then after the above gets converted to XML it gets wrapped in this:

<?xml version='1.0' encoding='utf-8' standalone='yes'?>
<Root>
  <Story>
    <section>
    </section>
  </Story>
</Root>

I was trying some code like this in the .jsx:

var xmlTag = myDocument.xmlElements[0];
var subsectionXmlItemArray = xmlTag.evaluateXPathExpression("//subsection");
for (var i=0; i<subsectionXmlItemArray.length; i++) {
  var subsectionChildrenXmlItems = subsectionXmlItemArray[i].getElements();

  var subsectionGroup = myDocument.groups.add(subsectionChildrenXmlItems); // errors here saying the parameter passed in is not the right type (it's XmlElement[], not PageItems[])
  subsectionGroup.name = "subsection" + i;
}

The above intent being for each subsection, add each element within it to a new group of their own.  The comment in the snippet explains the need to access the PageItem derived from the XmlElement.

 

Thank you, thank you, thank you for any suggestions or directions to resources for understanding the InDesign ExtendScript DOM would probably be helpful as I've had a hard time locating good resources to learn this material.  (I know of www.indesignjs.de for API documentation but a lot of the descriptions of object in the API are not very descript and the blog side of the site is in German)

TOPICS
How to , Scripting

Views

273

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
Community Expert ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

Any chance you can instead of grouping those 3 elements - place all of them together as one Story - with image as Anchored / InLine object?

 

▒►ID-Tasker CS6 / CC- most powerful tool ever created for InDesign◄▒

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
Community Expert ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

Hi @Colin26726730pyve, I've never done this, and I can't test because I haven't mocked-up your document and structure. But see if this is any help:

 

var xmlTag = myDocument.xmlElements[0];
var subsectionXmlItemArray = xmlTag.evaluateXPathExpression("//subsection");
for (var i = 0; i < subsectionXmlItemArray.length; i++) {
  var subsectionChildrenXmlItems = subsectionXmlItemArray[i].getElements();

  var textFrames = [];
  for (var j = 0; j < subsectionChildrenXmlItems.length; j++)
    textFrames.push(subsectionChildrenXmlItems[j].parentStory.textContainers);

  var subsectionGroup = myDocument.groups.add(textFrames);
  subsectionGroup.name = "subsection" + i;
}

 

 

If that doesn't help, could you make a small sample document that I could run the code on to see what it's getting back. I assume that subsectionChildrenXmlItems is an Array of XMLElement, but I'm not sure, because my knowledge of Indesign XML is quite limited.

- Mark

(Edit: typo)

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
New Here ,
Mar 20, 2023 Mar 20, 2023

Copy link to clipboard

Copied

Hi and thanks for suggestion.  The array you're asking about is an "XmlItem[]" and that was I tried to descriptively named it that.  I'm guessing that it is indeed fact because I imported some typescript file in order to provide myself intellisense in VS Code and it is saying that is the type.  When I look it up in the API XmlItems are a thing.

 

I have now ran the script using this modified logic and my error became:

  • "Error: Invalid value for parameter 'groupItems' of method 'add'. Expected Array of PageItems, but received (())."

So I'm guessing that the each XmlItem.parentStory.textContainers were empty based on the empty parentheses?

 

Since I'm running on InDesign Server do you know if there is a convenient way to log to the ID Server console?  I've tried $.writeln("stuff") and  app.consoleout(obj) and neither seemed to work for me.  My lack of experience with ExtendScript and in this environment is making it basically impossible debug my code and see state in the script.

 

I can maybe try to get you a document but I will need to make sure it's not generated with customer data.  For my clarification that is the final output PDF, right?  Not the template or mock data passed in?

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
Community Expert ,
Mar 21, 2023 Mar 21, 2023

Copy link to clipboard

Copied

LATEST

Hi @Colin26726730pyve, yeah that was a shot in the dark and yes you did name the xmlItems correctly but I didn't realise that XMLItem was a thing!

 

Another thing to try would be to see what you get when you call the getElements() method of XMLItem. Does it return XMLElements? If so, slotting that into the test code above might work.

 

But, ideally, what I'd like is a sample .indd that I can run the sample script on. I'm just not knowledgable enough about XML in Indesign to set up a sample doc the way yours is.

- Mark

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