4.7. Commanding Microsoft Word
If TextEdit were a sardine, Microsoft Word would be a blue whale. Mentioning the two together invites all sorts of comparisons: TextEdit takes up 5 MB, while Word takes up 20; TextEdit has six menus, Word has a dozen. The two programs, in fact, are illustrative of their respective companies' approaches: TextEdit is straightforward and simple, while Word is complex and feature-packed.
Still, Word's AppleScript support puts TextEdit to shame. Word includes the standard Text Suite (Section 4.6.1) but adds more than 50 commands of its own.
4.7.1. Adapting a TextEdit Script
In Section 4.6.1, you created a script that counted the number of characters, words, and paragraphs in an open TextEdit document. Of course, Word already has a word count feature (Tools Word Count). Still, it's a good exercise to adapt your existing TextEdit script for use with Word, to get a feel for the differences in what it's like to script the two programs.
The first step in adapting a TextEdit script is to replace the tell statement with one that directs Word instead of TextEdit.
tell application "Microsoft Word" activate . . . end tell
If you run the script at this point, you'll get a strange result: both the character and word counts will be 0. This is caused by an odd quirk in Microsoft Word, which requires that you use the count and every keywords on the same line. To get your script to work properly, therefore, you must merge some of your existing lines to create this new script:
tell application "Microsoft Word" activate --Count the characters: set numberOfCharacters to (count every character of the front document) set characterText to "Characters: " & numberOfCharacters --Count the words: set numberOfWords to (count every word of the front document) set wordText to "Words: " & numberOfWords --Count the paragraphs: set numberOfParagraphs to (count every paragraph of the front document) set paragraphText to "Paragraphs: " & numberOfParagraphs --Assemble the text for the dialog box: set dialogText to characterText & return & wordText & return ¬ & paragraphText display dialog dialogText end tell
You'll notice that the two-liners that counted various itemsfor example, set allWords to every word of the front document and set numberOfWords to (count allWords)have been put together into single-line commands. This is enough to satisfy Word's requirement for using count and every together; when you run the script now, it works properly.
When writing your own scripts for different programs, make sure to try using count and every together (as used here) and on different lines. Chances are, the script will work at least one of the ways, but the only way to find out is to try both.
4.7.2. Shrinking Documents by a Page
If you write letters or emailsor computer books, for that matteryou've probably experienced the dreaded one-page-too-long problem. It goes like this: you're typing your document, inserting all sorts of different fonts and sizes, but when you come to the end, you realize your document is just one page longer than you want it.
If you're like most people, you'd sigh deeply, and then get to work modifying each individual font in your document, shrinking them in size so the document is one page shorter. The trouble with this approach, of course, is that it can take a long time to select blocks of text and change their font sizes, especially if you need to modify multiple fonts on dozens of pages.
Word, conveniently enough, provides an AppleScript command to take care of this whole text-squeezing business for you: fit to pages. The trouble is, though, that Word has hundreds of different commands in its dictionary. Does Microsoft really expect you to spend several minutes scanning all the entries in the left pane to find the fit to pages command? Yep.
Still, you purchased this book to save you some time, and you can prove Microsoft wrong. Once you've opened Word's dictionary (Section 3.2.2), simply follow the procedure in Figure 4-7 to find the correct dictionary entry.
The definition of the command (also shown in Figure 4-7) tells you that it's meant to squeeze your fonts "just enough so that the document will fit on one fewer pages." The light blue document text, furthermore, indicates that you have to tell fit to pages which document you want it to work with.
tell application "Microsoft Word" activate display dialog "Shrink document by a page?" fit to pages (front document) end tell
This script brings Microsoft Word forward and then displays a confirmation dialog box, asking you if you want to shrink the document by a page. Since you haven't defined special buttons for the dialog box (Sidebar 4.2), you're left with only two options:
After clicking OK, Word checks what it'll take to knock one page off your document and adjusts the font sizes throughout the document until the mission is complete.
4.7.3. Repeating the Shrinking
The script you have so far is convenient, but what if you wanted to shrink your document by more than just one page? For example, you might be badgered for writing too much in a business proposal but find it easier to shrink the size of your writing than to actually cut text out.
In this situation, of course, you could run your script more than once, returning to Script Editor each time to click Run again. But that would defeat the purpose of using AppleScript in the first place: to automate tasks that you would normally have to do by hand.
repeat <some number> times --Commands you want to repeat go here end repeat
When AppleScript encounters a repeat statement, it goes through the commands insideone at a timeand then repeats the entire process however many times you specify. For running the fit to page command multiple times, you simply have to add a repeat statement to your existing script, as shown here:
tell application "Microsoft Word" activate display dialog "Shrink document by 5 pages?" repeat 5 times fit to pages (front document) end repeat end tell
Now when you run the script, Word tries to shrink the front document by five pages instead of just one. (It accomplishes this, of course, by repeating the fit to page command five times.)
4.7.4. Shrinking a Variable Number of Times
Again, the script you have is convenient. However, it still lacks one crucial feature: you can't specify how many times you want it to repeat (at least, not without changing the numbers in Script Editor by hand). It would be great if, using a dialog box, you could have the script ask how many times to repeat the fit to pages command.
To do so, you simply have to change a few more lines of your script:
tell application "Microsoft Word" activate set theNumber to the text returned of (display dialog ¬ "Shrink by how many pages?" default answer 3) repeat theNumber times fit to pages (front document) end repeat end tell
Now the repeat statement runs however many times you specify in the dialog box. In other words, you get to tell Word how many pages you want to shrink the front document by, instead of hard-coding that number into the script. That's the power of AppleScript for you!
There are other instances when you might want to use repeat statements, including: