Automating Word with AppleScript

Prior to Office 2008, you could automate repetitive tasks and add entirely new features in Microsoft Word by using Visual Basic for Applications (VBA) macros. Unfortunately, Word 2008 doesn’t support VBA. In its stead, Microsoft recommends that you use AppleScript and Automator. The good news is that, with a little tinkering, almost anything you could do with a VBA macro in previous versions of Word, you can now do with AppleScript in Word 2008.

There’s bad news as well. One, Office 2008 doesn’t support the recording of AppleScripts as it once did for VBA. That means you must code AppleScripts by hand. Two, you can’t attach AppleScripts to toolbar buttons. (You can put them in a menu and assign keyboard shortcuts to them, though.) And three, you can’t embed AppleScripts in a Word document (as you could do with VBA macros); if you want someone else to use a script you’ve written, you’ll have to send them the script file along with the document.

As examples of what you can do with AppleScript in Word 2008, I created the following scripts. They solve some basic problems in Word; some of them replicate macros I created with VBA in earlier versions of Word.

To use these scripts, type them into Script Editor (in /Applications/AppleScript) and save them, with the file format Script, in the Microsoft User Data/ Word Script Menu Items folder. (By default, it’s in your user name/Documents.) Alternatively, if you’d rather not type them out yourself, you can download them and copy them into the same folder. However you do it, the scripts should then appear in Word’s Scripts menu (just to the right of the Help menu, shown with the stylized S icon).

Editor’s Note: We didn’t have for all of Joe Kissell’s AppleScripts for Word 2008 in the original magazine article, so we added three more to this article. These scripts are all in the enclosed download file.

Paste plain text

Most word processors have an alternative Paste command—often called Paste Plain Text or Paste And Match Style—that strips the copied or cut text of its original formatting, so that when you paste it, the text assumes the style of the text you’re pasting it into. To do that in Word, after you’ve copied or cut your text, you have to choose Edit: Paste Special, select Unformatted Text, and click on OK; you can assign a shortcut to Paste Special but not to the procedure as a whole.

Paste Plain Text: This AppleScript enables Word to paste text that adopts the style of surrounding characters, via a single menu command or a keyboard shortcut.

I wanted a way to do this in Word 2008 with a single keystroke, and without having to install any third-party software. (Loads of utilities will let you do this.) Microsoft’s Web site suggests one paste-as-plain-text script. It looks wonderfully simple, but there’s a problem: unless the insertion point is at the end of the document when you run the script, Word leaves the insertion point before the pasted text, not after, as you would normally expect when pasting text.

So I made a script of my own (see “Code Box” below). It doesn’t actually paste the contents of the Clipboard; rather, it simulates typing by using the type text command. This method puts the insertion point at the end of the pasted text, where you’d expect it. This script works only with text. If your Clipboard contains only graphics, the script does nothing; if the Clipboard contains text plus other elements, the script inserts only the text.

Enclose in HTML tags

Word isn’t the best tool for editing raw HTML code. But sometimes you may need to insert some HTML in a document. If you select some text and run this script (see “Code Box” below), it encloses that text in <a> (anchor) tags and, as a bonus, moves the insertion point between the quotation marks so that you can type or paste in the URL to which the text should link. (You can easily modify this script to insert different HTML tags.)

The line set newPoint to (selection start + 9) determines the new location of the insertion point. (You want the insertion point to follow the nine characters in the text string <a href=" .) The line set selection start to newPoint actually moves it.

Clean up text

When you paste text into a Word document that you’ve copied from a Web browser or an e-mail message, you may find that every line has a hard return at the end, and sometimes extra spaces. This script (see “Code Box” below) cleans up such text by removing extra returns and spaces; it assumes that any sequence of two returns was meant to be a genuine paragraph break, while single returns should be spaces. Select the range of text you want to clean up, and run this script.

This script consists of a series of find-and-replace commands. The first replaces all double returns (which will be real paragraph breaks) with a placeholder character. I used the paragraph symbol (¶; option-7), but you can use any dummy character you like, as long as it doesn’t already exist in your text. The second command replaces all remaining returns with a space. The third replaces the placeholder characters with single returns. The fourth replaces all white space (any number of spaces and tabs) with a single space. The fifth removes any extra spaces after returns.

Assign keyboard shortcuts to AppleScripts

Now that you’ve got your AppleScript “macros” in Word 2008’s Scripts menu, here are two easy ways to assign keyboard shortcuts to them.

System Preferences Go to the Keyboard & Mouse pane of System Preferences and click on Keyboard Shortcuts. Click on the plus-sign (+) button at the bottom of the window. From the pop-up menu, choose Microsoft Word (you may need to choose Other and then locate it on your disk). Then, in the Menu Title field, type your script name exactly as it appears, including capitalization—for example, Paste Plain Text. Click in the Keyboard Shortcut field, press the key(s) you want to use as your shortcut, and click on Add.

Script Name You can build a keyboard shortcut directly into your script by tacking a few extra characters onto the end of the script’s name when you’re saving it in Script Editor. Immediately after the name (without an intervening space), add \\s (to indicate that what follows is a shortcut), then one or more modifier keys (m for the command key, c for control, and/or o for option), and then a single character. If you enter a capital letter or some other shifted character, your shortcut will include the shift key. For example, \\smV is command-shift-V; \\scoh is control-option-H.

Final thoughts

To learn more about using AppleScript in Word, visit Microsoft’s Office Developers page, where you can download the 526-page PDF “Microsoft Word 2004 AppleScript Reference” (most of which still applies to Word 2008). If you’re accustomed to VBA, you should also download the free PDF “MacTech’s Guide to Making the Transition from VBA to AppleScript”.

Code Box

Paste Plain Text

tell application "Microsoft Word"
tell selection
try
set theClip to Unicode text of (the clipboard as record)
type text text theClip
end try
end tell
end tell

Enclose in HTML Tags

tell application "Microsoft Word"
tell selection
set newPoint to (selection start + 9)
set myString to "<a href=\"\">" & content of text object & "</a>"
set content of text object to myString
set selection start to newPoint
end tell
end tell

Clean Up Text

tell application "Microsoft Word"
set findRange to find object of selection
tell findRange
execute find find text "^p^p" replace with "¶" replace replace all
execute find find text "^p" replace with " " replace replace all
execute find find text "¶" replace with "^p" replace replace all
execute find find text "^w" replace with " " replace replace all
execute find find text "^p^w" replace with "^p" replace replace all
end tell
end tell

[Joe Kissell is the senior editor of TidBits and the author of numerous e-books about Mac OS X.]

Subscribe to the Best of Macworld Newsletter

Comments