Copy link to clipboard
Copied
Please can someone tell me the best way to access the tags applied to an item or text.
I have a page that contains tagged items and tagged text on it.
I can see the tags when I have 'View Structure', 'View Tagged Frames' and 'View Tag Markers' switched on.
I can also see tags when i view a story in 'Story Editor'.
I've tried a few things, and read quite a few posts, but as yet I'm not achieving what I'd like.
If I place the cursor at the start of a text box containing tagged copy, and move through it a character at a time using the right arrow key, I can see the assigned tags change in the 'Tags' palette.
I'd like to be able to do the same using Javascript, I'm trying to pick up the start and end tags. Please can someone point me in the right direction.
Thanks in advance.
You could try something like the following, this will traverse the whole document structure and print the tag name and its content on the console.
var doc = app.activeDocument
var rootXMLElement = doc.xmlElements[0]
traverseXMLElement(rootXMLElement)
function traverseXMLElement(xmlElement)
{
$.writeln("Tag Name : " + xmlElement.markupTag.name + " Tag Content :" + xmlElement.contents)
for(var i = 0; i < xmlElement.xmlElements.length; i++)
traverseXMLElement(xmlElement.xmlElements)
}
P.S. :- The code
...Copy link to clipboard
Copied
You could try something like the following, this will traverse the whole document structure and print the tag name and its content on the console.
var doc = app.activeDocument
var rootXMLElement = doc.xmlElements[0]
traverseXMLElement(rootXMLElement)
function traverseXMLElement(xmlElement)
{
$.writeln("Tag Name : " + xmlElement.markupTag.name + " Tag Content :" + xmlElement.contents)
for(var i = 0; i < xmlElement.xmlElements.length; i++)
traverseXMLElement(xmlElement.xmlElements)
}
P.S. :- The code is rough draft with no error checking and handling for tags containing images.
-Manan
Copy link to clipboard
Copied
Thank you for the help and for the code, it's really useful and doing pretty much what I wanted.
It’s interesting seetng your solution to the query.
Thanks once again.
Copy link to clipboard
Copied
Just another quick query to see if I'm understanding the code correctly.
It seems to dynamically generate a nested loop depending on the number xml elements within the root structure?
Have I got that correct?
Thanks.
Copy link to clipboard
Copied
Yes that is correct, it traverses the whole xml structure of the document starting from the Root.
Copy link to clipboard
Copied
Thanks Manan.
Copy link to clipboard
Copied
Thanks once again for the original code. Working forward with that I now have something working pretty much as i want.
In your original post you did mention something about 'handling for tags containing images'. I'm trying to incorporate something takes these into account.
I've set up a very simple document with a text box tagged as a story. Within that text box is an image that is tagged as an artifact. If I execute the code below I can see the artifact tag ok.
var doc = app.activeDocument
var rootXMLElement = doc.xmlElements[0];
$.writeln (rootXMLElement.xmlElements[0].xmlElements[0].markupTag.name);
Please can you tell me what direction I should be looking to incorporate something that handles images.
Why is it that the original code doesn't spot tagged images? Should I be looking at a different part of the XML structure?
Thanks in advance.
Copy link to clipboard
Copied
The reason i said that the code does not handle images is that for an image the property contents would not yield any results. You would be able to get the markuptag name fine for image tags but the content value would be blank, in order to handle images you could try something like below
var doc = app.activeDocument
var rootXMLElement = doc.xmlElements[0]
traverseXMLElement(rootXMLElement)
function traverseXMLElement(xmlElement)
{
if(xmlElement.contents != "")
$.writeln("Tag Name : " + xmlElement.markupTag.name + " Tag Content :" + xmlElement.contents)
else
{
if(xmlElement.graphics.length > 0)
{
var grp = xmlElement.graphics[0]
$.writeln("Tag Name : " + xmlElement.markupTag.name + " Tag Content : Image Name :" + xmlElement.graphics[0].itemLink.name)
}
}
for(var i = 0; i < xmlElement.xmlElements.length; i++)
traverseXMLElement(xmlElement.xmlElements)
}
In the above code i check if the contents is blank, if it then i check if the tag indeed contains an image or not, if it does i get the graphic object and then get the name from the corresponding link object. This would give you a gist of the object model hierarchy in this case, now you can extend this to your purpose.
-Manan
Copy link to clipboard
Copied
Hi Manan,
That's great, thanks for the help and for once again expanding my knowledge of InDesign JS.
The code I've created so far has a try block which, I think, effectively bypasses the images.
Your code has has illustrated I need to treat the graphics differently.
The only slight problem I have at the moment is getting the baseline and horizontalOffset of the frame containing the graphic.
I'm pretty sure I've referred to the correct item in my code because I'm getting an artifact tag. The only thing is it doesn't seem to have the 2 properties I'm after.
Once again, thank you for your help Manan.