Team LiB   Previous Section   Next Section
Speech Listener Application

Dictionary commands

listen for

listen for is the only command in the Speech Listener application's dictionary. The Speech Listener app is located in the startup disk:System Folder:Scripting Additions folder. Figure 30-3 shows the Speech Listener icon.

Figure 30-3. Speech Listener app icon
figs/ascr_3003.gif

listen for allows a script to "listen for" any text provided in a list of strings (or numbers), then respond accordingly when it hears one of the listed words or numbers. The return value of the listen for command is the text or number that is recognized. The following example first sets a fam variable to a list of names. It then uses the listen for command to prompt the user to say one of the names. If the name is recognized (let's say it's "Emily") then the computer responds by saying "Hey guys, Emily is my family member too!" The [[emph - ]] syntax is an embedded speech command that (in this case) de-emphasizes the pronunciation of the following word. Embedded speech commands are explained elsewhere in this chapter. If the script listens but does not hear any spoken commands in 60 seconds, it will time out and raise error number -1712. The example script catches this error, says "bye-bye," and exits the repeat loop (effectively terminating the script). Listen for also raises an error when text is heard but does not match any of the specified text options.

(* repeat the prompt until a family name is identified

or the script times out *)

repeat

try

tell application "Speech Listener"

set fam to {"Stacy", "Bruce", "Rachel", "Emily", "Anne",¬

"Dean", "Bob"}

(* listen for returns the recognized text, which the fam_member

variable is set to *)

set fam_member to (listen for fam with prompt "Say a¬

family [[emph - ]] member")

end tell

say "Hey guys, " & fam_member & " is my family [[emph -

]]¬

member too!"

exit repeat

on error number errnum

If errnum is -1712 then

say "Bye-Bye"

exit repeat

else

say "I'm sorry, try again."

end if

end try

end repeat
listen for list of strings or numbers

The listen for command has to be nested in a tell block targeting the Speech Listener application, as in tell application "Speech Listener"...end tell. Chapter 7, describes the tell statement. Listen for's required parameter is a list of strings or numbers comprising the text that the machine listens for. The example below listens for certain numbers and, if it hears one, will speak that number squared. In other words, if it hears "5," then the script will speak the result of 5 * 5. This example uses embedded speech commands, such as [[ slnc 500 ]] (which produces half a second of silence). These commands are explained elsewhere in this chapter. The three listen for labeled parameters are optional.

with prompt string

The machine says this prompt before listening for the designated text, as in with prompt "say your name".

giving up after integer

You can designate a number of seconds for the Speech Listener app to wait before it returns a timeout error (error number -1712) and quits listening. If you do not specify an integer for giving up after, then the default timeout will occur in 60 seconds.

filtering boolean

If filtering is true, the Speech Listener app skips phrases that contain special characters:

tell application "Speech Listener"

   set numList to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "cancel"}

   repeat (* keep repeating the prompt until a number from 1-10 or

"cancel" is *)

   heard

      try

        set n to (listen for numList with prompt "say a¬

        number, between  1 and [[ emph - ]] 10, and I will¬

        square [[¬ emph - ]]it." giving  up after 15)

        if n is equal to "cancel" then

          say "bye bye"

          return -- exit the applet

        end if

        say "The answer is [[ slnc 1000 ]]  [[ emph - ]]" &¬

        ((n  * n) as  text)

        on error number errnum

        if errnum is -1712 then

          return

        else

          say "Sorry, please try again."

        end if

      end try

    end repeat

  end tell
end tell
    Team LiB   Previous Section   Next Section