2009-06-12

Applescript to give a primative command line interface to MS Word 2008

One of the things I hate about MS Word is having to use the mouse to click through menus to do simple things. Of course you can create keyboard shortcuts for stuff, but I find it hard to remember really complicated keyboard combos. What I prefer is the old XyWrite approach, which is to hit a command key, then enter a two or three letter code for an action, and then hit enter to have it execute.

Back when I was using Word 2000 for Windows I found a simple solution: Create a keyboard shortcut for the Run Macro command (Ctrl-M), and then write macros with 2-3 character names. For example: I wrote a macro to accept all revisions in a document, then named it 'aa' (for accept all). Then to accept all revisions all I had to do was hit Ctrl-M, type aa, hit enter, and boom, all the revisions were accepted.

When I switched to Mac OS X and Word 2008 for Mac I couldn't find any obvious way to recreate this functionality, so I suffered through clicking through menus for a lot of commands. However, one day I just reached my limit with click-click-click-click and decided to find a solution using Applescript. Here is what I ended up with:

-- Open a dialog box for user input
set var_input to display dialog "Word command: " default answer ""
-- For some reason you need to do this to get the user input into a string variable
set var_input to (text returned of var_input) as string
-- Applescript apparently doesn't have SELECT CASE statements so use If Else to take different
-- actions based on user input
if var_input = "st" then
    -- Convert selected text to strikethrough font
    tell application "Microsoft Word"
        set strike through of font object of selection to true
    end tell
else if var_input = "aa" then
    -- Accept all changes in the document
    tell application "Microsoft Word"
        accept all revisions active document
    end tell
else if var_input = "kt" then
    -- Toggle keep text together (so text doesn't break over page break)
    tell application "Microsoft Word"
        tell selection
            if ((keep with next of paragraph 1 as boolean) is false) then
                set keep with next of every paragraph to true
            else
                set keep with next of every paragraph to false
            end if
        end tell
    end tell
else if var_input = "uc" then
    -- Convert the selected text to all UPPERCASE
    tell application "Microsoft Word"
        set case of characters of selection to upper case
    end tell
end if
What this does is open up a dialog box where you can type in a string of characters, and then based on what you entered it tells MS Word to do some command or other.

Then I saved the Applescript in the folder:
Users/myusername/Documents/Microsoft User Data/Word Script Menu Items

With the name:
Command Line\cM.scpt

Although its not at all obvious, the \cM.scpt at the end creates a Ctrl-M as a keyboard shortcut for the Applescript. For more on naming Applescripts in this folder so they have keyboard shortcuts see:

http://www.entourage.mvps.org/script/add_shortcuts.html

Its for Entourage, but the script naming conventions appear to be the same.

No comments: