4.7.
|
|
In Section 4.6.1, you created a script that counted the number of
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
|
|
Up to Speed
The count Command |
|
In the previous script, you used the count command for the first time, to figure out how many words are in a TextEdit document. You'll encounter count again in all sorts of scripts, though, so it's important to understand how it works.
In essence,
count
takes a list and returns how many items are inside it. You can take advantage of this command for all sorts of
Like the exists command, count can come either before or after the object you want to count. That means that both of the following are acceptable commands in AppleScript: count allCharacters --Works allCharacters count --Also works You'll often see the count command used in conjunction with the every keyword (Section 6.3), too. This powerful duo lets you get all the objects of a certain type (like all the words in a TextEdit document) and then count them. You can even use this command pair to get the number of items in a folder, like this:
tell application "Finder"
set itemNumber to (count every item ¬
in the folder "Applications")
display dialog "You have " & ¬
itemNumber & " items inside your ¬
Applications folder."
end tell
Finally, in a quite specialized use, you can use the
count
command on its own to figure out how many characters are in a string. This would be helpful if you ever needed to confirm that an
set yourName to the text returned of ¬
(display dialog ¬
"Enter your first name:" default ¬
answer "Xavier")
set numChars to (count yourName)
display dialog "Your name has " & ¬
numChars & " characters."
|
If you run the script at this point, you'll get a
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
|
Workaround Workshop
Word Miscounting |
|
When you run the script on this page, you see a dialog box listing the character, word, and paragraph counts from your frontmost Microsoft Word document. Unfortunately, this word count often conflicts with the results of Word's
included
word count feature (Tools
This problem occurs because AppleScript, when commanding Microsoft Word, considers punctuation marks their own words . That means Microsoft Word would see three words in "Don't eat yams!", but AppleScript would see five (the three words, plus two punctuation marks). Of course, this can distort your word counts considerablyespecially if you're the type that likes to insert dashes in the middle of your sentences. Fortunately, you can dodge AppleScript's fuzzy math by communicating with a Microsoft Word text object an abstract AppleScript item that represents the text inside a particular document. Unlike communicating with a document directly, communicating with a text object actually produces an accurate word count. So, to remedy your script, you could simply make the following changes: tell application "Microsoft Word" set theText to the content of the ¬ text object of the front document set numberOfCharacters to ¬ (count every character of theText ) set characterText to "Characters: " ¬ & numberOfCharacters set numberOfWords to ¬ (count every word of theText) set wordText to "Words: " & ¬ numberOfWords set numberOfParagraphs to (count ¬ every paragraph of theText) set paragraphText to "Paragraphs: " ¬ & numberOfParagraphs set dialogText to characterText & ¬ return & wordText & ¬ return & paragraphText display dialog dialogText end tell Now when you run your modified script, your word count reflects the actual number of words in your documentwithout counting punctuation. |
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
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.
If you write
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
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
Still, you purchased this book to save you some time, and you can
The definition of the command (also shown in Figure 4-7)
Armed with this information, you can write your script:
tell application "Microsoft Word"
activate
display dialog "Shrink document by a page?"
fit to pages (front document)
end tell
|
This script
If you click OK, the script proceeds with the
If you click Cancel, the script ends immediately.
After clicking OK, Word checks what it'll take to knock one page off your document and
|
The script you have so far is
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.
Instead, you might as well have AppleScript repeat the fit to pages command all by itself, so you can go ride your bike while AppleScript chugs along on its font-shrinking mission.
The key to repeating a command in AppleScript is to use a
repeat
statement. Such statements
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
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
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:
Renaming multiple files automatically (Section 6.4.1)
Repeating a computerized beep at regular intervals to create a virtual metronome (Sidebar 8.1)
Rating every song in your iTunes library (Section 8.1.2)