Skip to main content
frameexpert
Community Expert
Community Expert
August 12, 2013
Answered

Tag Image with XML Tag

  • August 12, 2013
  • 1 reply
  • 1459 views

Hi All,

I want to tag all of my anchored images in a CS6 document with a particular XML Tag. I am using CS6 on Windows with JavaScript. Any advice would be appreciated. Thanks.

Rick

This topic has been closed for replies.
Correct answer frameexpert

OK, it looks like I have to add the element to the structure and then use "markup" to apply the element to my image.

// Get the selected image.

var image = app.selection[0].getElements()[0];

// Get the Story element.

var root = doc.xmlElements.item(0);

var storyElem = root.xmlElements.item(0);

// Add the "Image" element to the Story element.

var imageElem = storyElem.xmlElements.add ("Image");

// Tag the selected image with the Image tag.

image.markup (imageElem);

1 reply

frameexpert
Community Expert
Community Expert
August 12, 2013

Working through this as I go: First, I need to add the appropriate xmlTag to the document, if it doesn't exist:

#target indesign

var doc = app.activeDocument;

var imageTag = doc.xmlTags.item("Image");

if (imageTag.isValid === false) {

    doc.xmlTags.add ("Image");

}

www.frameexpert.com
Vamitul
Legend
August 12, 2013

myDoc.allGraphics will get you an array of all the graphics in the document.

Iterate it.

if (myGraphics.parent.parent.constructor.name=='Character') you got an anchored image.

The parent of a image is a rectangle. If that rectangle's parent is a charater, you got yourself an achor. might get a bit tricky if you have a image inside a group, and that group is anchored, but even so, you can test parents until you get to a Character object - so it's a anchor or a Document object -> no anchor. Simplest way of doing it is recursively, but beware of possible speed penalties.

tag it:

myGraphics.markup(myXmlElement)