Highlighted

Get coordinates from XML tag

Community Beginner ,
May 01, 2020

Copy link to clipboard

Copied

Hello,

Using the following script, I am able to loop on the differents elements that are tagged with a “word” xml tag. This browsing occur in the structure panel, but is there any method to get geometrics informations from the tag in the page? I need to access the coordinates and the bounding information.

 

Hopping somebody knows, thanks in advance.

 

 

 

var doc = app.activeDocument;
var xmlTag = doc.xmlElements[0];
var words = xmlTag.evaluateXPathExpression("//word");
for(var i=0;i< words;i++){
	
	// how to get bounds and coordinates of words[i] ???

}

 

 

 

Adobe Community Professional
Correct answer by rob day | Adobe Community Professional

You should be able to get it from the parentPage of the parentTextFrame of the selection. Either get the name or the documentOffset for the absolute page numbering:

 

    $.writeln("Page Name: " + app.selection[0].parentTextFrames[0].parentPage.name);
    $.writeln("Absolute Page Number: " + ((app.selection[0].parentTextFrames[0].parentPage.documentOffset)+1));

 

 

Topics

How to, Scripting

Views

528

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

Get coordinates from XML tag

Community Beginner ,
May 01, 2020

Copy link to clipboard

Copied

Hello,

Using the following script, I am able to loop on the differents elements that are tagged with a “word” xml tag. This browsing occur in the structure panel, but is there any method to get geometrics informations from the tag in the page? I need to access the coordinates and the bounding information.

 

Hopping somebody knows, thanks in advance.

 

 

 

var doc = app.activeDocument;
var xmlTag = doc.xmlElements[0];
var words = xmlTag.evaluateXPathExpression("//word");
for(var i=0;i< words;i++){
	
	// how to get bounds and coordinates of words[i] ???

}

 

 

 

Adobe Community Professional
Correct answer by rob day | Adobe Community Professional

You should be able to get it from the parentPage of the parentTextFrame of the selection. Either get the name or the documentOffset for the absolute page numbering:

 

    $.writeln("Page Name: " + app.selection[0].parentTextFrames[0].parentPage.name);
    $.writeln("Absolute Page Number: " + ((app.selection[0].parentTextFrames[0].parentPage.documentOffset)+1));

 

 

Topics

How to, Scripting

Views

529

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
Enthusiast ,
May 01, 2020

Copy link to clipboard

Copied

Hi,

You might try the following properties for a word: baseline, end baseline, and end horizontal offset

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 ,
May 01, 2020

Copy link to clipboard

Copied

Hi Loic,

 

Adding to the code snippet you shared, you can check which object in the layout does the XMLElement represent and then use the layout objects geometric properties depending upon its type, i have made some edits to your code and added an alert to alert the type of the object that corresponds to the XMLElement

var doc = app.activeDocument;
var xmlTag = doc.xmlElements[0];
var words = xmlTag.evaluateXPathExpression("//word");
for(var i=0; i < words.length; i++)
{	
	var a = words[i]
	alert(a.xmlContent) //Use a.xmlContent to query its geometric properties
}

For properties of a pageitem you can refer the following link

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#PageItem.html

For a text, word type objects you can follow the hints given Steve Hopkins in the previous post.

 

-Manan

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...
Community Beginner ,
May 01, 2020

Copy link to clipboard

Copied

S_Hopkins and Manan, thank you for your quick response.

 

I am able to inspect the xmlContent from my tag, unfortunately, it seems that it only lists all the characters, paragraphs and styles attributes (and a few others). I need to access to the geometricBounds property (if possible).

 

For instance, the XML content I am importing is like :

<xml>
<para>Lorem ipsum <word param="foo">dolor</word> sit amet, consectetur adipisicing elit. Natus rerum ut culpa architecto error a accusantium obcaecati deserunt dolor dignissimos id dolores alias dolorum minus fugit, libero, officiis, expedita ea.</para>
<para>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cumque aperiam nostrum debitis cum at voluptatum quos, praesentium, distinctio porro assumenda vel officiis, <word param="bar">accusamus</word> odio accusantium expedita quod. Et, debitis nulla.</para>
</xml>

I would like to have access to the geometric coordinates to be able to generate a shape at the position of the word (actually, I am using basiljs and some native indesign javascript).

It would be really faster to find a tag instead of parsing the entire text and checking every word (which I am able to do).

But maybe it can't be done with xml because a tag may be splitted between 2 texts blocks ?

 

Thanks again for your help.

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 ,
May 02, 2020

Copy link to clipboard

Copied

Does something like this work? xmlContent returns a range of text but for some reason it doesn’t let you get at all of the text properties, so here I’m making a selection and getting the baseline, and offset properties from the selection

 

 

var doc = app.activeDocument;
var xmlTag = doc.xmlElements[0];
var words = xmlTag.evaluateXPathExpression("//word");
var sel = app.selection = words[0].xmlContent;
var x1, x2, y1, y2

for(var i=0;i< words.length;i++){

    app.selection = words[i].xmlContent;
    x1 = app.selection[0].horizontalOffset
    x2 = app.selection[0].endHorizontalOffset
    y1 = app.selection[0].baseline - app.selection[0].ascent;
    y2 = app.selection[0].baseline + app.selection[0].descent;

    $.writeln("Bounds: " + y1+", "+x1+", "+y2+", "+x2);
}

 

 

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...
Community Beginner ,
May 03, 2020

Copy link to clipboard

Copied

Hello rob_day,

 

And thank you for your answer, it seems to be the right answer for the coordinates.

Have you an idea on how I may know the page number of the active selection?

I cannot find any page or currentpage properties in app.selection or words[i].xmlContent

 

Sorry for all those questions, but there is really a lack of exemples in the indesign script manual.

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 ,
May 03, 2020

Copy link to clipboard

Copied

You should be able to get it from the parentPage of the parentTextFrame of the selection. Either get the name or the documentOffset for the absolute page numbering:

 

    $.writeln("Page Name: " + app.selection[0].parentTextFrames[0].parentPage.name);
    $.writeln("Absolute Page Number: " + ((app.selection[0].parentTextFrames[0].parentPage.documentOffset)+1));

 

 

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...
Loic_H LATEST
Community Beginner ,
May 03, 2020

Copy link to clipboard

Copied

Thanks a lot, that work perfectly !!!!

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