Section 6.7. Getting Lists from Other Programs


6.7. Getting Lists from Other Programs

When it comes to lists, AppleScript really shines when you mix it with other programs. That's because other programs deal with their own unique kinds of informationgraphics, formatted text, and sound, for examplewhich you simply can't create in AppleScript alone.

When commanding programs from AppleScript, you'll find yourself using the every keyword all the time. In this chapter alone, for example, you'll use every to get a list of all the words in a text document (see the next page), a list of all the open documents in a single program (Section 6.7.1.1), and even a list of synonyms for a particular word (see the next page). So don't get listlessyou're about to go on a wild list-making ride!

Figure 6-7. Top: Enter every item you want into the dialog box. If you'd like, you can copy some text from another program, and paste it here using -V. Bottom: When you click OK, the script converts your text into a list. In this layout, it's a lot easier to browse.


6.7.1. TextEdit

When you're scripting TextEdit, lists can come in quite handy for breaking your text into smaller chunks. For instance, you can easily get a list of all the words in the frontmost TextEdit document with this script:

tell application "TextEdit"     set wordList to (every word of the front document) end tell

If there's a particular word you're just dying to findlike which word came 2907th in your documentyou can tweak your script to let you pinpoint a word by its position. Here's how:

tell application "TextEdit"     set wordList to (every word of the front document)     set theNumber to the text returned of (display dialog ¬         "What numbered word would you like to find?" default answer 20)     display dialog "The word you chose is: " & (item theNumber of wordList) end tell

When you run that script, AppleScript asks you what word you want to find. Simply enter the number of the word (1 for the first word, 2 for the second, and so on), and press OK. In the next dialog box, AppleScript tells you what the word is.

While this might seem like a useless script, it's actually quite helpful for students. Just create a TextEdit document containing the list of chemical elements, for example, and when you run the script, you can test your knowledge of the periodic table ("What numbered word would you like to find?", in this case, would be the same as asking "What element would you like to find?").

6.7.1.1 Listing documents

In addition to getting the words within a document, you can get a list of all the open TextEdit documents. Narrowing down this list, moreover, lets you get a list of only modified documents, which is perfect for quickly saving your changes in TextEdit:

tell application "TextEdit"     activate     set unsavedNum to (count every document whose modified is true)     --unsavedNum stores the number of files you haven't saved in TextEdit     display dialog "You have " & unsavedNum & ¬     " unsaved documents. Save them?"     --If you click Cancel, ths script ends before it gets to the next command     save every document whose modified is true end tell

When you run this script, you'll see the dialog box shown in Figure 6-8.

Figure 6-8. This script works because every TextEdit document has a modified property, which lets you know whether you've made any changes to files since the last save. By saving only the modified documents, this scriptunlike the one on Section 5.7.3ensures that no documents are saved unnecessarily.


6.7.2. Microsoft Word

As scriptable programs go, Word is among the most useful for creating lists. Not only does it support all the word-, character-, and paragraph-listing features that TextEdit does, but Word also supports many list-creating commands of its own.

Like all the Microsoft Word scripts in this book, you should be using Word 2004 for these scripts. Section P.4.3.1 in the Introduction explains how to get a free demo version if you haven't bought the program.

Among the most useful such commands is the one for getting synonyms of a word. Open Microsoft Word's dictionary (File Open Dictionary), click in the left pane, and do a search for synonym (Edit Find Find) and you'll come across the get synonym info object command, which you use like this:

--Part 1: set theWord to the text returned of (display dialog ¬     "Enter a word to get its synonyms:" default answer "Enter your word here") --Part 2: tell application "Microsoft Word"     set synonymObject to (get synonym info object item to check theWord)     --Part 3:     set synonymList to the meanings of synonymObject end tell --Part 4: choose from list synonymList

Here's how the commands break down:

  • Part 1: This command asks for the word to find synonyms for, and stores it in the theWord variable.

  • Part 2: This command gets a synonym object for the word you chosea quirky data type that Word uses to store synonyms. (Nobody ever said Microsoft products were straightforward.)

  • Part 3: The script tells the synonymList variable to store the list of synonyms for your word (in Word's odd universe, synonyms are called meanings).

If you wanted a list of the word's opposites, you'd use the antonyms property instead of meanings.

  • Part 4: AppleScript presents a dialog box, displaying the list of synonyms (Figure 6-9).

Figure 6-9. Top: Enter the word you want the synonyms for. Bottom: Using Word's built-in thesaurus, AppleScript displays a list of the word's synonyms. If you like to use TextEdit for your everyday word processing but miss Microsoft Word's thesaurus, this script lets you have the best of both worlds: just type any synonym you want from this list directly into TextEdit.


Obviously, it's impossible to go through the zillions of other Microsoft Word commands one at a time in this book. If they interest you, however, spend some more time poking around in Word's dictionary. You'll find lots of list-producing propertieseverything from every footnote of the front document to the name of every document.

Power Users' Clinic
Using Lists for Other Purposes

In this chapter, you've used lists just as you'd expect: for storing ordered sequences of information. However, you can use AppleScript lists for some other, less obvious tasks, too.

For example, when you want refer to a specific pixel somewhere on your screen, you use a list with two numbers:

set myPoint to {200, 420}

Here, 200 is the x coordinate (counting right from the left edge of the screen), and 420 is the y coordinate (counting down from the top edge of the screen). You can use points like this to position Finder windows anywhere on the screen; for example, this script:

tell application "Finder"     activate     set the position of the ¬         front window to {200, 420} end tell

places the upper-left corner of the front Finder window 200 pixels from the left edge of your screen and 420 pixels down from the top edge of the screen. If you run that script every day before breakfast, you'll come back to find the window in the exact same position as it was the day before200 pixels down and 420 pixels right.

If you'd like more control over the look of your Finder windows, though, you can create a four-item list to store the position and size of a window onscreen, like this:

tell application "Finder"     activate     set the bounds of the front window to ¬         {60, 90, 560, 690} end tell

In a list like this, the first two numbers set the position of the upper-left corner of the windowin this case, 90 pixels down and 60 pixels over from the upper-left corner of the screen. The last two numbers set the position of the lower-right corner of the windowhere, 690 pixels down and 560 pixels over from the upper-left corner of the screen. Therefore, you'd end up with a window that's 500 pixels wide (560-60=500) and 600 pixels tall (690-90=600).




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