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

时间:2021-08-13 02:44:27

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.

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

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.

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

[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.

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

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.

我是AppleScript的新手,所以这些代码中的一些是从Adobe的样本中粗略修改的。如果代码是残暴的,我将不会被冒犯。

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.

编辑:我已经改写了这个问题,以便更好地反映正确的答案。

5 个解决方案

#1


1  

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

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

#2


1  

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.

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

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.

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

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.)

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

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.)

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

#3


0  

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.

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

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.

您作为示例发布的代码可以与您(或您的老板)感到满意一样具体 - 通常情况下,更容易帮助您了解更具体的细节。

#4


0  

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.

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

#5


0  

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

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

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.

我做了很多InDesign Server开发。您可能会因为一些不一定与代码相关的原因而看到减速。

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).

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

#1


1  

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

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

#2


1  

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.

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

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.

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

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.)

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

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.)

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

#3


0  

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.

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

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.

您作为示例发布的代码可以与您(或您的老板)感到满意一样具体 - 通常情况下,更容易帮助您了解更具体的细节。

#4


0  

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.

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

#5


0  

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

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

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.

我做了很多InDesign Server开发。您可能会因为一些不一定与代码相关的原因而看到减速。

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).

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