UI scripting: Problematic removal of unwanted Word footnote returns

[previously posted to AppleScripting InDesign Yahoo Group,

Bret and I have been discussing in indesign-talk how to deal with a problem I’ve had recently with Word’s adding unwanted footnote returns that strangely can’t be removed using Replace All. None of the three QuicKeys macro sequences I created for this could handle it; we found that Keyboard Maestro could, but the only repeat function I could find in that program couldn’t run the sequence faster than once per second, which meant it took over a half hour to remove the returns in all the 1900 footnotes of the book concerned. The job involved has already been done twice and I hope the second time was the last, but for possible future occasions I’m still interested in the UI-scripting possibility suggested by Bret.

Fortunately I’ve quickly found a helpful introductory article on the subject athttp://www.mactech.com/articles/mactech/Vol.21/21.06/UserInterfaceScripting/index.html.
It’s good that the writer of this article especially likes the click and keystroke commands, as these are the two I used in my Keyboard Maestro sequence. I may not even need to rerecord the click or redetermine its location.

[…] I’ve downloaded, installed and launched [UI Browser] and it indeed seems very helpful and perhaps even essential. I couldn’t, however, get a script working. This works to change the first of the pesky footnote returns:

tell application “System Events”
tell process “Microsoft Word”
set frontmost to true
keystroke “H” using {command down, shift down}
tell group 1 of tab group 1 of window 1
tell button “Find Next” to click
tell button “Replace” to click
end tell
end tell
end tell

But I can’t get the process to repeat. If I command-home to the top of the Word doc (http://dl.dropbox.com/u/1107821/la%20danghera%20lingvo_ee%2B%20(exp%20returns).doc, 2.8MB), open the Find and Replace dialog, and click on “Find Next” and “Replace”, I can continue clicking on “Find Next” and “Replace”, thus getting rid of the unwanted returns. (This is how I did it - manually - a couple of nights ago, 1903 times, ouch.) But if I run the above script, clicking on “Find Next” afterwards yields the alert “Word has finished the document. The search item was not found.” - sometimes also with an additional scripting alert about some index thing. It’s like doing the same two click commands via a script screwed things up somehow.

It seems like such a simple thing to do, but it’s being so obstinately resistant. Anybody know how to make it work?



P.S. The Find and Replace dialog needs to be set with

Find what: ^p
Format: Style: Fußnotentext

Replace with: [nothing]

Model: MacBook Pro
AppleScript: 2.1.2
Browser: Safari 6531.21.10
Operating System: Mac OS X (10.6)