Development software

Automating Word 2008 - Extra AppleScripts

If you don’t fee like typing in all the AppleScripts from Joe Kissell’s Geek Factor column in the May Macworld, here’s a downloadable version. Just download it, uncompress it, and copy all the scripts therein to your Microsoft User Data/Word Script Menu Items folder.

Also, we didn’t have space for all of Joe Kissell’s AppleScripts for Word 2008 in the magazine. Here are three more. These scripts, too, are in the download file above.

Toggle Keep with Next

When editing a document to make sure the page breaks fall in the best places, it’s convenient to be able to turn the paragraph style "Keep with Next" on or off. Ordinarily this requires choosing Format: Paragraph, clicking on the Line and Page Breaks tab, selecting (or deselecting) Keep with Next, and clicking on OK. You can assign a keystroke to this command within Word, too, but another approach is to use AppleScript.

Because AppleScript doesn’t have a toggle switch as such for Boolean values, this script starts by checking the "keep with next" setting of the paragraph where the insertion point is located (or the first paragraph of the selection, as the case may be). Then, if it’s false, the script changes the value for all selected paragraphs to true and vice versa.

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

Insert Boilerplate and Date

There are at least a dozen ways to insert boilerplate text into a Word document, including Word’s own AutoText features and several third-party utilities. However, if you want your boilerplate to include a Word field code—such as the document’s name, the date, or the cur- rent page number—your best bet is an AppleScript.

In this example, I put a string of text ("Today’s date is: "), followed by a field with the current date, at the insertion point. (The boilerplate will replace any text you might have already selected.) Modify the text and the field insertion lines as necessary to meet your needs.

Like the Paste Plain Text script, this script uses the type text command to simulate typing, so the insertion point will be in the right spot when you’re done. It then uses the create new field command to insert a field code with the current date. (Another AppleScript command, insert date time, can insert the date, but it leaves the insertion point in the wrong place, requiring another line of code for cleanup.)

You can adjust the format of the date by playing with the letters m, d, and y in the script. In the example here, MMMM d, yyyy will render dates in the format April 21, 1961. If you use mm/dd/yy instead, you’ll get 04/21/61. To insert the document’s name instead of the date, replace the insert date line with create new field text range text object of selection field type field file name. For other create new field options, consult Word’s AppleScript dictionary. (In Script Editor, choose File: Open Dictionary, scroll to Microsoft Word, and click on OK. You can then search for the term you want.)

tell application "Microsoft Word" 
	tell selection 
		type text text "Today’s date is: " 
		create new field text range text object of selection field text {"DATE \\@ \"MMMM d, yyyy\""} 
	end tell 
end tell 

Remove from Work Menu

Word’s Work menu is designed to hold documents or commands you want to access frequently. (To add the frontmost document to this menu, for example, choose Work: Add to Work Menu.) To remove items from the menu, you’re supposed to press command-option-minus [–] and then choose the command you want to remove. However, Mac OS X uses the same shortcut for Zoom Out, if you have Zoom enabled on the Seeing tab of Universal Access preferences, in which case it does nothing in Word.

You could solve the problem at the system level by turning off Zoom. You could also solve it in Word by adding the ToolsCustomizeRemoveMenuShortcut command to a menu or assigning a keyboard shortcut to it. But, for the sake of showing yet another approach to customization, I want to provide a way to solve the problem using AppleScript too.

The script is simple: it gets a list of all the items on your Work menu and puts them in a variable called workMenuItemNames. It then displays a dialog showing those names in a scrolling list. When you select one and click on OK, the script issues the delete work menu item command, telling Word to remove that item from the Work menu.

tell application "Microsoft Word"
	try
		set workMenuItemNames to (get name of work menu items)
		set thisItem to {choose from list workMenuItemNames with prompt "Select a Work Menu item to remove:"}
		delete work menu item (thisItem as string)
	end try
end tell

Subscribe to the Create Newsletter

Comments