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.


I am not familiar with AppleScript myself, but it would be helpful if we could see how you are going about doing this.

Ed Swangren

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.

Greg Hurlman

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.

Derek Park
+1  A: 

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

Mike Heinz

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

Eric Willis
+1  A: 

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