Applescript and Autoformat (search and replace), Word 2004

From: Peter Edman (edman_at_aya.NoSpam.edu)
Date: 08/26/04


Date: Wed, 25 Aug 2004 22:45:01 -0400

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



Relevant Pages

  • Re: Database/Email Script
    ... Both double and single quotes work for quoting array indexes. ... the relevant code. ... What I would like first would be to get the script working. ... task as a learning experience.) ...
    (comp.lang.php)
  • Re: basic help to run an application from WSH
    ... string values must be enclosed in quotes. ... The Run method will execute on c:\notepad.exe. ... I'm just learning vbscript for wsh and here is my first script, ... Also if I cut and paste the exact file path from the script into the ...
    (microsoft.public.scripting.vbscript)
  • Re: Interesting Discussion with my PHP Teacher
    ... ## For a small script like this one, ... > it wher the sql statement is constructed because there are too many quotes. ...
    (comp.lang.php)
  • Re: How to pass string in command line argument.
    ... > I want to pass a command argument to perl script with double quotes (" ... > just a straight string is geeting passed ...
    (perl.beginners)