Apple script performance issue

We traced the problem to the below sample Apple Script code. We use this code to add/insert bookmarks to each paragraph.

repeat with i from 1 to number of paragraphs of theDocument

            tell application "Microsoft Word"

                            set currentParagraph to paragraph i of theDocument

                            set textObject to text object of currentParagraph

                            set textContent to content of textObject

                            set bookmarkName to "XXX" & i - 1

                            set newBookmark to make new bookmark at theDocument with properties {name:bookmarkName, text object:textObject}

            end tell

end repeat

This script works fine with Microsoft Word 16.10 but when we use Microsoft Word 16.15 the script becomes very slow.

Hi. Welcome to MacScripter.

I hope someone with Word can answer your query. It looks as if the repeat should be inside the ‘tell application “Microsoft Word”’ statement, so that ‘theDocument’ and its ‘paragraphs’ can be properly identified; but I’m not a Word expert and I don’t know what’s in the rest of the script.

Since your query’s about AppleScript, I’ve asked for this topic to be moved to our “AppleScript | Mac OS X” forum.

MacScripter has its own [applescript] and [/applescript] tags. There are buttons for these and other BBCode tags tags just above the text window in each posting page. Wrapping AppleScript code between them makes it appear like this:


repeat with i from 1 to number of paragraphs of theDocument

	tell application "Microsoft Word"

		set currentParagraph to paragraph i of theDocument

		set textObject to text object of currentParagraph

		set textContent to content of textObject

		set bookmarkName to "XXX" & i - 1

		set newBookmark to make new bookmark at theDocument with properties {name:bookmarkName, text object:textObject}

	end tell

end repeat