Applescript and Autoformat (search and replace), Word 2004

P

Peter Edman

OK, for my next trick learning AppleScript and Word 2004, since I cannot
find a utility or script online, I am trying to create for myself a
script that will take care of a recurring editing issue with nested
quotation marks. Apologies for the long explanation. There's a bug
report and a scripting query here.

One of my jobs is to run a blog. I will frequently quote articles that
include dialogue and complicated formatting like dashes next to quoted
phrases. Example of a good "quick brown fox" test sentence follows.
Original:

Mr. Jones's neighbor said, "he sure likes rock 'n' roll," as we stared
at his collection.

After I'm done with it, I want:

"Mr. Jones's neighbor said, 'he sure likes rock 'n' roll,' as we stared
at his collection."

I want, therefore, to create a script that will convert all the single
quotes to double quotes (and vice versa), while respecting the
apostrophes (and checking for dashes and other odd punctuation). Word
has issues with this, apparently.

I have the logic down just fine (well, perhaps kludgy, but I can't see
any more elegant way than to use placeholder variables), and the
original script I wrote for Tex-Edit Plus works great.

(BUG QUERY:) Unfortunately, somewhere in the Word X to Word 2004
transition, pasting into other applications has gotten screwy -- the
sample I was working on randomly stupefied the single quotes and
apostrophes and converted all em-dashes to hyphens (double quotes stayed
"smart"). I have "copy formatted text to clipboard" set on.

(APPLESCRIPT QUERY:) Since the whole point is to save myself manual
editing, I turned to Word's AppleScript. The script snippet below is the
result. It works to an extent (haven't extended the logic to paragraph
marks etc.) but despite the opening commands to turn off the, um, kind
assistance that Word offers on autoformatting, I cannot get it to
respect my wishes with the apostrophes and dashes. Even though the
script accurately catches the first apostrophe in "'n'" (it's a curly
right single quote in Word), it still insists on replacing it with a
curly left single quote instead of the curly right quote that I am
explicitly specifying in replace. When I look at the "find/replace"
dialog in the user interface, I've definitely passed in the right
character. (Ditto with quote marks around dashes -- Word's overriding of
my script makes it completely hopeless.)

So, how do I get Word to stop "helping" my script? Did I miss a
preference I need to disable? Do I need to further kludge a temporary
garble of text before or after my quotes (as I do when typing manually)
to make the autoformat do what I want?

Any ideas welcome.
Cheers
Peter E.


tell application "Microsoft Word"
set oldauto to auto format replace quotes
set oldauto2 to auto format as you type replace quotes
set auto format replace quotes to false
set auto format as you type replace quotes to false
set f to (get find object of selection)
tell f
clear formatting
set match all word forms to false
set format to false
set r to (get replacement of f)
tell r
clear formatting
end tell
end tell
execute find f find text "² " replace with "#CSQ# " replace replace
all
execute find f find text " ³" replace with " #OSQ#" replace replace
all
execute find f find text "‹³" replace with "‹#OSQ#" replace replace
all
execute find f find text "²‹" replace with "#CSQ#‹" replace replace
all
execute find f find text "² " replace with "#CSQ# " replace replace
all
execute find f find text " ¹" replace with " #APO#" replace replace
all
execute find f find text " Œ" replace with " #ODQ#" replace replace
all
execute find f find text "¹ " replace with "#CDQ# " replace replace
all
execute find f find text "‹Œ" replace with " #ODQ#" replace replace
all
execute find f find text "¹‹" replace with "#CDQ# " replace replace
all

execute find f find text "#ODQ#" replace with "³" replace replace all
execute find f find text "#CDQ#" replace with "²" replace replace all
execute find f find text "#APO#" replace with "¹" replace replace all
execute find f find text "#CSQ#" replace with "¹" replace replace all
execute find f find text "#OSQ#" replace with "Œ" replace replace all
set auto format replace quotes to oldauto
set auto format as you type replace quotes to oldauto2

end tell
 
M

matt neuburg

Peter Edman said:
One of my jobs is to run a blog. I will frequently quote articles that
include dialogue and complicated formatting like dashes next to quoted
phrases. Example of a good "quick brown fox" test sentence follows.
Original:

Mr. Jones's neighbor said, "he sure likes rock 'n' roll," as we stared
at his collection.

After I'm done with it, I want:

"Mr. Jones's neighbor said, 'he sure likes rock 'n' roll,' as we stared
at his collection."

The algorithm for this is by no means simple to arrive at, but in any
case I would strongly recommend that you not use Word for this. Since
you are dealing with blog, any text editor will do. I would copy and
paste the text into BBEdit and deal with it there. That way you know
just what you're doing and what you're getting, there won't be any
automatic interference, plus you get to take advantage of PCRE regex. m.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top