Section 8.2. Speaking and Listening


8.2. Speaking and Listening

Just like HAL from 2001: A Space Odyssey, your Mac can talk to you. Granted, it sounds more like a Norwegian struggling to learn English than Douglas Rain, but it's still unmistakably cool.

You don't need AppleScript to make your Mac speak, though. If you just type some words in TextEdit, choose Edit Select All, and then choose Edit Speech Start Speaking, you'll get a sample of what it's like to have your Mac talk to you. This can come in handy, for example, if you've written a long paper, since it's often easier to hear typos than to find them with your eyes.

If you don't like the voice you hear, go to System Preferences Speech Default Voice and select a different one. (The easiest voices to understand are Bruce, Vicki, and Victoria.) You can even set how fast they speak with the Rate slider.

Having AppleScript speak, though, is much more powerful than using TextEdit's dinky speech features. You can make an AppleScript assemble the sentences you want it to speak on-the-fly, for example, or even have AppleScript listen to your spoken commands. But best of all, you can have AppleScript speak into a file, so you can take its prerecorded speech with you on a CD or iPod.

8.2.1. Speaking from a Script

To make AppleScript speak to you, use the say command. Here's a simple example:

say "Hello there"

When you run that script, you'll hear "Heh-lo thay-er"the Mac's valiant attempt at speaking English. Feel free to type whatever you want after the say command, thoughyou're not restricted to dull two-word phrases.

If you want to override the default computer voice, you can even specify a different voice with the using option:

say "lah lah lah lah lah lah lah lah lah lah lah" using "Bad News"

If you enjoy entertaining voices, be sure to try out Bells (the voice of ringing instruments), Princess (a high-pitched girl), and Zarvox (an alien voice) as well. And if you want even more of a selection, there are 21 voices waiting for you in the System Preferences Speech Default Voice tab, as described earlier. Once you find a voice you like, just specify the voice's name after the using option of the say command.

Unlike most things in AppleScript, the text after the using option is case-sensitive. That means typing "bad news" for the voice's name (instead of "Bad News") would result in an AppleScript error.

You can specify nontext items for AppleScript to speak, too, as long as you use as string to convert them to text first. For instance, this simple script would speak the current date and time:

say (current date as string)

When you run that script, Script Editor surrounds the current date command in an extra set of parentheses. Don't worry about it, thoughthat's just an AppleScript quirk, and it doesn't affect how your script runs.

8.2.1.1 Linking your script to a keyboard shortcut

Now that you have a script that tells you the date and time, you can do away with that space-wasting menu bar clock (Figure 8-3). That'll free up more space for, say, the Script Menu (Section 1.1).

Figure 8-3. Top: A normal menu bar, with the clock taking up a big chunk.Middle: To get rid of the clock, -drag it off the menu bar. Bottom: Your new menu bar has more room left over for other menulets, including the Script Menu (far left). If you ever want the clock back, just open System Preferences Date & Time Clock, and choose "Show the date and time."


Your script is a convenient way to get the current date and time, but rather than storing the script in your Dock (Section 2.2.2), it would be even more convenient if you could link the script to a keyboard shortcut. That way, you could press Shift-Control-T, for example, to make your computer speak the time to you. There'd be no clutter in your Dock, and no clutter in your menu bara power user's dream.

Here's how to give your script a keyboard shortcut:

  1. In Script Editor, choose File Save for your date- and time-speaking script. Make sure the File Format is set to Script.

    You can save the script wherever you want, but your Library Scripts Info Scripts folder is as good a place as any. (Remember, if you put your script there, it'll also appear in the Script Menu for convenience.)Name the file something creative (like "Say the Time and Date"), and click Save.

  2. Download Keyboard Maestro from www.keyboardmaestro.com.

    Keyboard Maestro is a shareware program for programming keyboard shortcuts. It's perfect for running your AppleScript.

    There are oceans of other keyboard shortcut programs, each with their own features. If you'd prefer to use one besides Keyboard Maestro, check out http://www.macdevcenter.com/pub/a/mac/2004/04/09/launchers.html for a comparison of your options.

  3. Copy Keyboard Maestro to your Applications folder, and launch it.

    In the main window, click the New Macro button (Figure 8-4).

  4. Type a name for your shortcut in the Macro Name field.

    "Speak the Date and Time" is as good a name as any.

  5. Click Triggers, click New Trigger, and choose Hot Key.

    In the dialog box that appears, turn on the checkboxes for the keys that you want to trigger your script (Figure 8-5, top). Then click Save.

    Figure 8-4. Whenever you want to create a new keyboard shortcut, this is the button to click. Your shortcut can launch programs, scripts, or any other useful file on your system.


  6. Click Actions, click New Action, and choose Execute Execute AppleScript. Then click Set File, and select the script you saved in step 1.

    Once you click Save, you're left with a window that looks like the bottom of Figure 8-5.

    Figure 8-5. Top: Turn on the checkboxes for Shift and Control, and type T in the text box, so you'll be able to run your script by pressing Shift-Control-T in any program. Bottom: The Macro Editor, where you set up what happens when you press a particular key combination. If you'd like, for example, you can run additional scripts when you press Shift-Control-T, by choosing additional actions from the New Action pop-up menu.


  7. Click Save again, and quit Keyboard Maestro.

    Now you can listen to the time and date whenever you want by simply pressing the shortcut you set in step 5.

Having a script at your fingertips is a powerful addition to any AppleScripter's toolbox. If there are any other scripts you find especially useful (like, say, the color-correcting script from Section 7.2.2), give them shortcuts, too, for even more time savings.

8.2.2. Making a File Speak

Among commuters, books on tape are a popular way to listen to long books in a car or on a train. The digital equivalents are commercial audiobooks from the iTunes Music Store or www.audible.com, which you can listen to on an iPod.

Still, the selection of these recordings is pretty small: a few classic novels, bestsellers, and maybe an interview here or there. If you want to listen to your own files on the roadsay, your email or TextEdit documents spoken out loudyou can employ the help of AppleScript to speak your personalized text into an audio file. Best of all, this service is free.

To make AppleScript speak into a sound file instead of through your speakers, you have to use the say command's saving to option. You must also specify the name of an AIFF file, which is where the command saves your sound.

So, put it all together and what do you get? The following script, that's what:

say "Norwegian Wood" using "Bruce" saving to ¬     ":Users:yourUsername:Music:wood.aiff"

Make sure to replace yourUsername with your actual, one-word user name.

When you run that script, you'll end up with a two-second file, named wood.aiff, in your Home Music folder. You can listen to that file in QuickTime Player or iTunes, hypnotized by the sounds of your Mac saying "Norwegian Wood."

8.2.2.1 Converting Clipboard text into audio

Having AppleScript speak two-word phrases into audio files is a great way to spend a blustery afternoon, but it's not going to help you convert your email messages for listening on your iPod, for example. Instead, you need to adapt your script so it takes any text from the Clipboard (which contains something you've copied with either Edit Copy, or -C) and speaks that text into a file. Then, you can run the script regardless of whether you've copied text from your word processor, email program, or Web browser.

Unfortunately, the newest version of Microsoft Word copies text to the Clipboard in a nonstandard format. If you want to save a spoken version of a Word document, therefore, you should first paste the Word document's text in TextEdit, then copy the text from TextEdit, and finally run the script.

The trick is to use the keywords the clipboard, which represent the information currently stored on the Clipboard:

--Part 1: set soundLocation to (choose file name default name "clipboard.aiff") --Part 2: set theText to (the clipboard as string) say theText saving to soundLocation

Here's how the script works:

  • Part 1 presents a dialog box, letting you choose where to save the audio file (Figure 8-6, top). The default name option simply tells your script what file name should be used if you don't specify one manually in the dialog boxkind of like the default answer option for display dialog commands (Figure 4-1).

    Once you click Save in the dialog box, the script puts the name and location of the file you chose into the soundLocation variable.

  • Part 2 gets the contents of the Clipboard, converts it to text, and places that text in the variable theText. AppleScript then speaks this text (the clipboard) into the file you chose in part 1.

AppleScript won't read text into compressed MP3 or AAC filesonly into uncompressed AIFF files. So if you're tight on hard drive space, be sure to compress your sound files afterward using the instructions on Section 8.1.4.

Figure 8-6. Say you've selected and copied some text from www.apple.com/ipod. Top: The script asks you to choose where you'd like to save the spoken text and what you'd like to name the file.Lower left: Unless you've changed the program that opens AIFF files automatically, you'll see an icon like this, named whatever you specified in the dialog box on top.Lower right: Double-click the icon, and it'll open in QuickTime Player. Click the play button to get the computer speaking.


8.2.2.2 Reading multiple emails into a file

For ultra convenience, you can even use this script to read the contents of several emails into a single file. That way, on your ride to work, you could listen to every email your boss has sent you, and catch up on the day's upcoming events.

Here's how:

  1. Launch Mail (it's in your Applications folder).

    As described on Section 9.4, Mail is the free email program that comes with every copy of Mac OS X.

  2. Select all the messages that you want to include in your audio file.

    For example, if you wanted to include every message from Steve Jobs, you would click the little magnifying-glass icon in the upper-right corner of the Mail window; choose In All Mailboxes From; type "Steve Jobs"; and finally select every message in the list (Edit Select All, or -A).

  3. Press the Forward button.

    This trick forces Mail to open a New Message window, incorporating the contents of every email message you selected into a single window.

  4. Delete the line "Begin forwarded message."

    That way, you won't have to hear your Mac say "Begin forwarded message" at the beginning of your audio file.

  5. Select all the text in the New Message window.

    As mentioned earlier, you can choose either Edit Select All, or press -A.

  6. Copy the text to the Clipboard.

    Use Edit Copy or -C.

  7. Close the New Message window.

    Click Don't Save, since you don't need to preserve this compilation message.

  8. Switch to Script Editor, and run the script from Section 8.2.2.1.

    AppleScript asks you where you want to save the audio file and then proceeds to read every message you copied to the Clipboard into that single file.

This process is a huge timesaver over the alternative method: converting individual email messages to audio files, one at a time. Now you have no excuse not to reador listen tothe email that your boss sends you!

8.2.3. Speaking to a Script

OK, now you've got your computer talking to you. But what if you want to talk back? Unfortunately, your computer won't respond when you say, "No! Don't crash without saving my thesis!"

Still, your Mac can respond to certain verbal commands, with a little help from AppleScript.

The trick is to use the listen for command. You give this command a list of phrases to be on the alert for, and once you say one into your Mac's microphone (or iSight camera), the script figures out which one you said and does whatever it's supposed to. In that way, your script can respond to verbal commandsplus, it's a great way to show off your Mac's superiority to all those self-righteous PC users out there.

AppleScript's speech processing is nowhere near a full-blown speech recognition package, at least for Mac OS X Panther (version 10.3.x).

For one thing, AppleScript doesn't always hear accurately with the Mac's built-in microphone (a third-party headset microphone can help, though). For another thing, AppleScript can be quite slow in recognizing what you sayas long as 10 seconds, in some cases. Finally, you have to tell AppleScript beforehand which things you might sayyour Mac doesn't have a built-in dictionary with tens of thousands of words, like IBM's ViaVoice ($130, www.scansoft.com/viavoice/osx/).

Here's how you'd use AppleScript's basic speech recognition:

--Part 1: tell application "SpeechRecognitionServer"     --Part 2:     set heardPhrase to listen for ¬         {"One fish", "Two fish", "Red fish", "Blue fish"} end tell --Part 3: display dialog "You said " & heardPhrase

And here's how the script works:

  • Part 1 invokes the invisible SpeechRecognitionServer program, which is in charge of listening to you speak. The first time you run the script, Mac OS X may ask you, "Where is SpeechRecognitionServer?" The solution: Click the Browse button and navigate to System Library Frameworks Carbon.framework Versions A Frameworks SpeechRecognition.framework Resources SpeechRecognitionServer (phew!). Once you click Choose, the script works just fine.

  • Part 2 starts listening for four possible phrases. To let you know that Mac OS X is all ears, it shows you the microphone window for Apple's Speakable Items (Figure 8-7).

    If you find the Speakable Items microphone window distracting, you can double-click its upper half to minimize it to the Dock.

    Figure 8-7. The Speakable Items microphone. The word Esc indicates what key you have to hold down while talking to Mac OS X (in this case, Escape). If you want to customize this key, visit System Preferences Speech Speech Recognition tab Listening mini-tab, and click Change Key.


    Once you say "One fish," "Two fish," "Red fish," or "Blue fish," the script places your response in the heardPhrase variable.

  • Part 3 displays a dialog box of what you said, using the heardPhrase variable from part 2.

Now, this is simply a demonstration of speech recognition. If you want true power, you have to mix it with a dose of speech generation. For instance, you can create a script that listens for a verbal commandlike "Tell me what date it is"and responds verbally, too. Here's how:

--Part 1: tell application "SpeechRecognitionServer"     set heardPhrase to listen for {"Tell me what date it is", ¬         "Tell me how many programs are open"} end tell --Part 2: if heardPhrase is "Tell me what date it is" then     say (current date as string) --Part 3: else if heardPhrase is "Tell me how many programs are open" then     tell application "System Events"         set programCount to (count every process)     end tell     say "There are " & programCount & " programs running." end if

When you run this script, the Speech Recognition Blob appears and waits for you to say either "Tell me what date it is" or "Tell me how many programs are open." No matter which one you ask for, the script generates the appropriate response and speaks it out loud. Here's how:

  • Part 1 gives the SpeechRecognitionServer a list of two possible phrases you might say. Once you say one (while holding down the key specified in the Speech Recognition Blob), the script puts the one you said in the heardPhrase variable.

  • If you ask for the date, part 2 says it out loud. This say command is an exact copy of the script on Section 8.2.1.

  • If you ask for the number of open programs, part 3 speaks, "There are [however many] programs running." It does this by counting the number of System Events processes (Figure 6-3)in other words, open programs.

With this script in place, you can now talk to your computer without sounding like a total geek.

For more on AppleScript's speech support, visit http://developer.apple.com/ue/speech/applescript.html. Or, if you're a speech-recognition freak, visit http://auc.uow.edu.au/conf/conf00/papers/AUC2000_Woo.pdf to read an ultra-detailed paper on the intricacies of AppleScript's speech features.



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