通过 AppleScript 添加 InDesign XMLElements 时如何提高性能?

发布于 2024-07-04 05:12:16 字数 1036 浏览 15 评论 0原文

我有一个 AppleScript 程序,可以在 Adob​​e InDesign 文档中创建 XML 标签和元素。 数据位于表格中,标记每个单元格需要 0.5 秒。 整个脚本需要几个小时才能完成。

我可以发布内部循环代码,但我不确定 SO 应该是通用的还是特定的。 我会让暴民决定。

[编辑] 该代码构建一个列表(在此循环之前),其中表中的每行包含一个项目。 还有一个列表,其中表中的每一列包含一个字符串。 对于每个单元格,程序通过连接两个列表的 [行]/[列] 位置中的项目来创建一个 XML 元素和一个 XML 标记。 它还将该单元格中的文本与新创建的元素相关联。

我对 AppleScript 完全陌生,因此其中一些代码是根据 Adob​​e 的示例进行粗略修改的。 如果代码很糟糕,我不会生气。

这是代码:

repeat with columnNumber from COL_START to COL_END

    select text of cell ((columnNumber as string) & ":" & (rowNumber as string)) of ThisTable

    tell activeDocument

        set thisXmlTag to make XML tag with properties {name:item rowNumber of symbolList & "_" & item columnNumber of my histLabelList}

        tell rootXmlElement

            set thisXmlElement to make XML element with properties {markup tag:thisXmlTag}

        end tell

        set contents of thisXmlElement to (selection as string)

    end tell

end repeat

编辑:我已经重新表述了问题以更好地反映正确的答案。

I have an AppleScript program which creates XML tags and elements within an Adobe InDesign document. The data is in tables, and tagging each cell takes .5 seconds. The entire script takes several hours to complete.

I can post the inner loop code, but I'm not sure if SO is supposed to be generic or specific. I'll let the mob decide.

[edit]
The code builds a list (prior to this loop) which contains one item per row in the table. There is also a list containing one string for each column in the table. For each cell, the program creates an XML element and an XML tag by concatenating the items in the [row]/[column] positions of the two lists. It also associates the text in that cell to the newly-created element.

I'm completely new to AppleScript so some of this code is crudely modified from Adobe's samples. If the code is atrocious I won't be offended.

Here's the code:

repeat with columnNumber from COL_START to COL_END

    select text of cell ((columnNumber as string) & ":" & (rowNumber as string)) of ThisTable

    tell activeDocument

        set thisXmlTag to make XML tag with properties {name:item rowNumber of symbolList & "_" & item columnNumber of my histLabelList}

        tell rootXmlElement

            set thisXmlElement to make XML element with properties {markup tag:thisXmlTag}

        end tell

        set contents of thisXmlElement to (selection as string)

    end tell

end repeat

EDIT: I've rephrased the question to better reflect the correct answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

╭ゆ眷念 2024-07-11 05:12:16

我可以发布内部循环代码,但我不确定 SO 应该是通用的还是特定的。 我会让暴民决定。

您作为示例发布的代码可以是您(或您的老板)满意的具体代码 - 通常,它更容易帮助您提供更具体的细节。

I can post the inner loop code, but I'm not sure if SO is supposed to be generic or specific. I'll let the mob decide.

The code you post as an example can be as specific as you (or your boss) is comfortable with - more often than not, it's easier to help you with more specific details.

从来不烧饼 2024-07-11 05:12:16

如果内部循环代码的长度合理,我认为您没有任何理由不能发布它。 我认为 Stack Overflow 旨在涵盖一般问题和具体问题。

If the inner loop code is a reasonable length, I don't see any reason you can't post it. I think Stack Overflow is intended to encompass both general and specific questions.

镜花水月 2024-07-11 05:12:16

您使用的是 InDesign 还是 InDesign Server? 您的文档有多少页(或者您可以告诉我们有关您的文档/ID 设置的哪些其他信息)?

我进行了大量的 InDesign Server 开发。 您可能会因为一些不一定与代码相关的原因而看到速度变慢。

现在,我在大约 100 秒内几乎完全从 script/xml 生成 100-300 页文档(您可能正在做更大的事情)。

Are you using InDesign or InDesign Server? How many pages is your document (or what other information can you tell us about your document/ID setup)?

I do a lot of InDesign Server development. You could be seeing slow-downs for a couple of reasons that aren't necessarily code related.

Right now, I'm generating 100-300 page documents almost completely from script/xml in about 100 seconds (you may be doing something much larger).

ゃ懵逼小萝莉 2024-07-11 05:12:16

我想出了这个。

该文档包含一堆数据表。 总共大约有 7,000 个数据点需要导出。 我正在创建一个包含 7,000 个子元素的根元素。

不要那样做。 将每个子元素添加到根元素的速度越来越慢,直到大约 5,000 个子元素时 AppleScript 超时并且程序中止。

解决方案是通过从根创建约 480 个子代,每个子代有大约 16 个孙代,从而使我的代码更加脆弱。 节点数量相同,但代码现在运行得足够快。 (处理该文档仍然需要大约 40 分钟,但这比无穷大要短得多。)

顺便说一句,最初的 7,000 名儿童计划并不像看上去那么愚蠢或懒惰。 新的解决方案迫使我使用我无法控制的表中的数据将两个表链接在一起。 现在,如果有太多不该有的空格,程序就会中断。 (但它有效。)

I figured this one out.

The document contains a bunch of data tables. In all, there are about 7,000 data points that need to be exported. I was creating one root element with 7,000 children.

Don't do that. Adding each child to the root element got slower and slower until at about 5,000 children AppleScript timed out and the program aborted.

The solution was to make my code more brittle by creating ~480 children off the root, with each child having about 16 grandchildren. Same number of nodes, but the code now runs fast enough. (It still takes about 40 minutes to process the document, but that's infinitely less time than infinity.)

Incidentally, the original 7,000 children plan wasn't as stupid or as lazy as it appears. The new solution is forcing me to link the two tables together using data in the tables that I don't control. The program will now break if there's so much as a space where there shouldn't be one. (But it works.)

聆听风音 2024-07-11 05:12:16

问题几乎肯定是选择。 无论如何,您是否可以一次提取所有文本然后迭代内部变量?

The problem is almost certainly the select. Is there anyway you could extract all the text at once then iterate over internal variables?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文