Creating custom conversations with speakable items and AppleScript

Hello everyone,

I started to dive into speakable items and it’s use with AppleScript, to create a sort of Siri-like functionality for my mac. I would like to create custom “conversations” through scripting. For example:


say "hey there."
If user-response is "how are you" then say "I'm great!"
Else
If user-response is "hello" then say "how's it going?"
End if

Note: the above is an example of what I’m trying to do, but it will not work as a script yet. I just put it here as a general reference

The problem with scripting anything like this, is I’m not sure how the script would get the speech recognition feedback from the user. I could call the above script “hello”, and place it in the scripts folder of speakable items, but I don’t know how it would be able to interpret or respond to users submitted feedback through speech recognition using speakable items. Anyone have any suggestions?

Also, when using the say command, I can’t figure out how to change the voice that is used, or the speaking rate at which the voice talks. I’ve looked online but haven’t been able to reproduce the effect that I want when I’m writing the script.
And one more thing. I’ve gotten extremely useful and helpful tips for other scripts that I’ve created from engaging in this community. As scriptBuilders is in a sort of coma at the moment, is there anywhere where everyone is posting their completed scrips for the community’s general reference? I would like to post the results of these various scripts for anyone else who might find them useful. :slight_smile: Thank you all for all your help!

Hi rfscripter,

What you would do is use SpeechRecognitionServer.app. Here’s a simple example:

tell application "SpeakableItems" to activate
tell application "SpeechRecognitionServer"
	--path to it
	set user_phrase to (listen for {"hello", "bye"} displaying {"hello", "bye"} giving up after 60)
end tell

There are a few quirks to it that I tested more indepth in another post and tried to find workarounds. See the dictionary for SpeechRecognitionServer.app.

Edited: and btw, you probably need to browse to the app. It is located here on my machine:
alias “Macintosh HD:System:Library:Frameworks:Carbon.framework:Versions:A:Frameworks:SpeechRecognition.framework:Versions:A:SpeechRecognitionServer.app:”

gl,
kel

Here’s a link to part of the testing with iTunes:
http://macscripter.net/viewtopic.php?id=40569

gl,
kel

Thanks so much. I wish there was some easier way of doing all of this, because when I use the speech recognition app, it seems to only listen for the two responses I provide (yes and no) for example. However, if I say “thank you” which should bring up another script, the computer does nothing. When I said thank you again, it brought up iTunes for some reason. lol
I’ll keep trying and playing around with it…

Hi rfscripter,

I ran this script from the link I posted and it worked quite well one time so far:

set itunes_commands to {"play track", "stop track", "pause track", "rewind track"}
set inf_time to 1 * days

tell application "SpeakableItems" to activate -- changed launch to activate

tell application "iTunes"
	--activate
	stop
	tell playlist "Library"
		set track_names to name of every track
	end tell
end tell

tell application "SpeechRecognitionServer"
	listen continuously for itunes_commands with prompt "Say an iTunes command." giving up after inf_time with identifier "command" displaying itunes_commands
	set user_command to result
	stop listening for identifier "command"
	set user_track to missing value
	if user_command is "play track" then
		listen continuously for track_names with prompt "What track?" giving up after inf_time with identifier "track name" displaying track_names
		set user_track to result
		stop listening for identifier "track name"
	end if
end tell

if user_track is not missing value then
	tell application "iTunes"
		--activate
		tell playlist "Library"
			play track user_track
		end tell
	end tell
end if

tell application "SpeakableItems" to quit

(*
tell application "SpeechFeedbackWindow" to quit
tell application "SpeechRecognitionServer" to quit
*)

-- check
tell application "System Events"
	repeat until not (exists process "SpeechFeedbackWindow")
		delay 2
		beep
	end repeat
end tell
say "Finished."

It’s been a while since I’ve played around with this. As I remember, there were a few bugs that had some complicated workarounds for fixing it. This is fun stuff and I might play around with it more later.

gl,
kel

hmmm…I tried running this script, from script editor, and it does not stop running (it never completes) presumably because I can’t get it to actually play any tracks in iTunes. I will say “play track” which has only worked twice out of the 5 times I’ve run the script, and it obviously replies “what track?” but when I give a track name it just sits there…

Also, is there a way I could just say “play track_name” or “play track_name by artist_name”?

This script is awesome for iTunes, but what about simply creating conversations back and forth between the user and the computer? I referenced this last night, and now have come up with the below code. But still can’t figure out how to incorporate the speech recognition server app to listen for different responses and reply with different things:




set theOptions to {"You are very welcome.", "You're welcome.", "No problem, dude!", "Don't mention it.", "I hope you tip well!", "My pleasure!"}
set theChoice to some item of theOptions
say theChoice with waiting until completion


This script is called “thank you” in the speakable items folder.

But what if I wanted to say “no, I don’t” to the response “I hope you tip well.” I would like several other responses to “no I don’t”, but in reference to what was previously said. So in another script, if the computer asked “do you like this?” and I replied “No, I don’t.” this would not yield the same response as in the case of the thank you script above. This is why I didn’t write a script called “no I don’t” and place it in the speakable items folder. I hope all of this makes sense. :slight_smile:

Rocco

Hi rfscripter,

As I mentioned in the link, if you really want this to work fairly well you’ll need at least an external mike. Don’t know if you have one but from what I’ve read it makes a big difference. Once you get that working fairly well and the background noise down, then you can experiment with the scripting part. I’ve never bought an external mike because people might think that I’m crazier than they already do think. Yep, he’s been talking to himself again and is getting replies!

As I remember, out of the four two or three helper apps, one of them slowed everything down. Can’t remember, but you can try experimenting with this. Fun stuff!

gl,
kel

haha :slight_smile: that’s ok. I have a feeling a lot of people tend to think we’re all crazy when it comes to all of this applescript stuff. One of my family members looked at my screen the other day when I was in the middle of writing a finder script for mounting a bunch of network volumes with various passwords, etc. and they were like “What the heck is that?” lol

I do have an external mic, but haven’t tried it yet…my mistake. I figured it would work much better, but I will go grab it and plug it in and experiment more. :slight_smile: thank you for all of your help.

Hi Gl,

I tried this with an external mic and no background noise, and now the iTunes part of this script works great. I just don’t know what to call it to bring it up. So if I save it in the speakable items folder, trying to think of a natural thing to say for the resulting prompt “say an iTunes command.” Again, is there a simpler way of saying simply “play track_name” or “play track_name by this_artist?” I’m still experimenting with this kind of thing. and do you have any suggestions for the “conversational” part I was talking about earlier? :slight_smile:

thanks,
Rocco

Hi,

I think I’ve made a small bit of progress…but now I’m stuck again. lol

The below applescript SHOULD be the resulting script of the computer saying “How are you?” (so essentially this would be the “prompt” of the speech recognition server app) but I’m not sure how to do that. When you see the script, hopefully this will make sense. Saving this as “how are you” in the speakble items folder doesn’t help either, as this is sort of the opposite of what I want to happen (I have to ask the computer “how are you” which I don’t want to do)


tell application "SpeechRecognitionServer"
	set theResponse to listen for {"I'm good", "I'm great", "I'm good, how are you?", "How are you?", "I'm great, how are you?", "really", "yes", "yeah"} with prompt "How are you?"
	if theResponse is "I'm good, how are you?" then
		say "I'm great! Thanks for asking! I appreciate it. No one usually asks about me, you know."
	end if
	if theResponse is "really" then
		say "Yeah, really! It's kinda sad, don't you think?"
	end if
	if theResponse is "yes" or theResponse is "yes" then
		say "I know...oh well, that's too bad."
	end if
	if theResponse is "how are you?" then
		say "Uh, I just asked you that, you idiot. Don't make me repeat myself. However, I'm doing ok."
	else
		say "Good, I'm so glad to hear it!"
	end if
end tell

For example, I should be able to say “Hi” or “Hi, how are you?” It would be nice to make this script one of many options to this question, such as the following:
User: “Hi”
Script:
set theOptions to {“the-above-script”, “Hey”, etc.}
set theChoice to some item of theOptions
say theChoice

Thanks so much for all of your help! This is very confusing to me lol

Rocco