InDesign: How to properly use placeXML()

453 views Asked by At

In my script, I have it set up similar to this:

// "element" is an xml element in the structure that contains text
// "frame" is one page item

element.placeXML( frame );

This works wonderfully. It will take the text from the element and throw it into a frame on the page. However, I need to do something to the text after it is pasted in the frame, and for reasons too long to explain on this question, using mapTagsToStyles is not an option. I thought, this ought to be an easy task, so I tried the following code:

element.placeXML( frame );

// after the xml text gets placed, the frame becomes a text frame...

if ( frame instanceof TextFrame ){
    // some code here
}

BUT, that if statement returns false unless the frame was already a TextFrame before the script was run, which is also undesirable in the event that the XML element is an image. Since images do not support properties like "parentStory," it results in an error.

It seems that it is only a text frame after the entire script is complete. In other words, the XML does not get placed the instant the function is called. It seems to be behaving asynchronously, but I don't know how to use an "onload" event listener in this case.

// say "frame" is an empty rectangle frame...
// inserting text will change it into a text frame.

// This function, although written here, does not execute here.
element.placeXML( frame );

if ( frame instanceof TextFrame ){
    // instanceof will return [object Rectangle],
    // because placeXML has not inserted any text yet.
}

// end of script. ---------
// the placeXML() function executes here.

Is there a more correct way of using this function? Is this an intended effect, or is it a glitch in extendscript?

1

There are 1 answers

3
Loic On

Try (frame.getElements()[0] instanceof TextFrame)