[Update 15-Mar-08: An improved version of script below appears in "," 2008-03-15. -Joe Kissell]
Don't get me started on Word 2008. Suffice it to say that it's about, which is not saying much. In any case, one of the significant changes we all saw coming was the loss of VBA (Visual Basic for Applications) scripting. I had a few VBA macros, but wasn't losing any sleep over the change, since I figured I could always find another way to automate my work, or at worst switch back to Word 2004 when needed. However, after only a few hours of using Word 2008 I discovered how much I'd been relying on one particular macro: a simple script that pastes whatever's on the clipboard as plain text, without any style information.
It is, of course, unforgivable that Word lacks a built-in Paste Plain Text (or Paste and Match Style) command - even TextEdit has one. What Microsoft expects you to do, in the event that you want pasted text to adopt the style of the surrounding text, is to choose Edit > Paste Special, select Unformatted Text, and click OK. If I had to do that only once a day, it might not be too bad, but it's something I happen to do very often, and my fingers had become accustomed to pressing Command-Shift-V to run my little macro. The code for that VBA macro, for anyone interested, was this:
Selection.PasteSpecial Link:=False, DataType:=wdPasteText, Placement:= _
Of course, I never actually learned any VBA; back in 2003, in the days of Word X, I created my macro simply by telling Word to start recording my actions, choosing the Paste Special menu command, selecting Unformatted Text, and clicking the Stop button; I then assigned a keyboard shortcut to the resulting macro. (Incidentally, doing the same thing in Word 2004 results in a macro that does not in fact paste unformatted text, though you can write one that does or use one created in an older version of Word.)
Now, in Word 2008, my goal was to reproduce that functionality using AppleScript. Word 2008 does have pretty good AppleScript support, after all. Unfortunately, it doesn't support recording one's actions as I'd done previously, so I had to figure out how to write the actual code. I assumed that wouldn't be a huge problem since I'm a fair hand (though certainly no expert) at AppleScript, but it took me an hour of fiddling with Word's odd implementation of this already odd scripting language to get to the point where I thought I'd solved the problem. I had a short script that seemed to work, using the AppleScript command for Word's Paste Special feature, and I even figured out how to assign a keystroke to it. Then I noticed that whenever I used the command, it left my insertion point at the beginning of the pasted text, rather than at the end where it should be. Ugh. Back to the drawing board.
To make an increasingly long story somewhat shorter, the script I ended up with after another hour's fiddling, which does in fact work exactly the way I wanted it to, is this (copy and paste this into Script Editor, or better yet, just):
tell application "Microsoft Word"
set theClip to string of (the clipboard as record)
set newPoint to (selection start + (length of theClip))
set content of text object to theClip
set selection start to newPoint
set selection end to newPoint
It works around the insertion-point-placement problem, somewhat awkwardly, by determining where the insertion point is (or where the selection begins, as the case may be) before you paste, counting the number of characters on the clipboard, and then moving the insertion point that number of characters forward after pasting. I have a hard time believing this is the easiest or most efficient way to accomplish this task, though, and it could well be the case that a more elegant solution exists. If you know of one (and have tested it to make sure it actually works in Word 2008), drop me a line and I'll update this article accordingly. (And yes, I know there are oodles of third-party clipboard and macro utilities that can do this too, but my preference was for a solution that required no extra software.) Meanwhile, the above macro has functioned perfectly in my testing so far, and if you're looking for an easy way to paste plain text in Word 2008, enjoy it with my compliments.
The final step is to get this thing working via a keyboard command. To do this, I saved the script in ~/Documents/Microsoft User Data/Word Script Menu Items/ and gave it a name with special characters on the end to indicate what keyboard shortcut I wanted it to use (my old favorite, Command-Shift-V). The name I selected was "Paste Plain Text\smV.scpt". The \s indicates that whatever characters follow are to be interpreted as a shortcut. The m means "Command" (I could have used other options, too, like c for "Control"), and the capital V means Shift-V. Et voilà. Without even having to restart Word, Command-Shift-V once again pastes plain text at the insertion point, and I only had to waste two hours of my life to make it happen! (I'm now wondering how many times I'll have to use that command in the future so that the cumulative gain turns out to be worth it. But even if I never actually recoup that investment, I'll be less irritated every time I use Word, and that counts for something!)
Script Suggestions -- Are there other common, everyday tasks in Word that you previously accomplished with a VBA script and are looking to recreate in AppleScript? Whether or not you already have a solution, let me know (by sending a note to firstname.lastname@example.org). I'm sure that if we put our collective heads together we can solve even more of these pesky problems.