Section 4.6. Adding Word Count


4.6. Adding Word Count

If there's one feature that TextEdit lacks over its competitors, it's the ability to count the number of words in a document. With all the powerful searching, typographical, and speech tools TextEdit has, it almost makes you wonder whether an Apple programmer was just sick the day he was supposed to program that feature.

Thankfully, AppleScript comes to your aidagain. You can write a short script that not only counts the number of words in your document but also counts the paragraphs and even the number of characters in your documentgreat for tricking people into thinking you've written a lot for your term paper.

The key to all this text-based power is the Text Suite, a section of many programs' dictionaries that deals exclusively with the nitty-gritty of text (like counting individual characters). You'll find the Text Suite in the dictionaries of professional programs like Microsoft Word (Section 4.7)but more immediately, you'll also find the Text Suite in TextEdit's dictionary. By employing the Text Suite as described in the following pages, you can write a script to count the number of words in any TextEdit document

4.6.1. Employing the Text Suite

If you open TextEdit's dictionary and browse the lefthand pane, you'll find that the Text Suite already includes entries for word, character, and paragraph. These are the fundamental nouns (or classes) of AppleScript's text handling. By using those keywords as follows, you can create a script to add a basic word count feature to TextEdit:

tell application "TextEdit"     activate     --Count the characters:     set allCharacters to every character of the front document     set numberOfCharacters to (count allCharacters)     set characterText to "Characters: " & numberOfCharacters     --Count the words:     set allWords to every word of the front document     set numberOfWords to (count allWords)     set wordText to "Words: " & numberOfWords     --Count the paragraphs:     set allParagraphs to every paragraph of the front document     set numberOfParagraphs to (count allParagraphs)     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

Here's how the script breaks down:

  • The commands following Count the characters get a list of all the characters in the frontmost TextEdit document, count the number of items in that list (see Sidebar 4-3, and then put together a string something like "Characters: 932" based on how many characters there are in the document.

  • The commands after Count the words do the same thing, just for words instead of characters.

  • The commands after Count the paragraphs do the same thing too, but for paragraphs instead of words.

In AppleScript terms, a paragraph is any block of text that has a return or newline character after it (Table 4-1). That means that even an empty line is considered a paragraph in AppleScript.

  • The commands after Assemble the text for the dialog box concatenate all the previous strings you created, putting each string on its own line.

  • Finally, the script displays the dialog box that lets you know the script has completed its word-counting mission (Figure 4-6, bottom).

Figure 4-6. There's no reason to use other programs to count your words when TextEdit and AppleScript can do it for free. For the ultimate in convenience, add your new word-, character-, and paragraph-counting script to the Script Menu (Section 1.1.16.1).




AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net