InDesign:如何正确使用placeXML()

时间:2022-09-11 20:55:16

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:

这很有效。它将从元素中获取文本并将其放入页面上的框架中。但是,我需要在文本粘贴到框架后对文本执行某些操作,并且由于原因太长而无法解释此问题,因此使用mapTagsToStyles不是一种选择。我想,这应该是一个简单的任务,所以我尝试了以下代码:

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.

但是,if语句返回false,除非在运行脚本之前帧已经是TextFrame,这在XML元素是图像的情况下也是不合需要的。由于图像不支持“parentStory”等属性,因此会导致错误。

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.

在整个脚本完成之后,它似乎只是一个文本框架。换句话说,在调用函数的瞬间,XML不会被放置。它似乎是异步行为,但在这种情况下我不知道如何使用“onload”事件监听器。

// 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?

有没有更正确的方法来使用这个功能?这是预期的效果,还是extendcript中的故障?

1 个解决方案

#1


0  

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

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

#1


0  

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

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