Q: AppleScriptObjC and multiple threads
Hi again folks,
I've read a few old discussions here about having multiple threads going in a Cocoa-AppleScript application, but I didn't find any that I think would work for my particular use case.
Here is my situation: I have a method which waits for the user to say a phrase, and then executes another method when it hears the phrase. Here is the code I'm using currently:
on listenForQuery()
set query to ""
try
tell application "/System/Library/PrivateFrameworks/SpeechObjects.framework/Versions/A/SpeechRecognitionServer.app"
set query to listen for {"Hello"} giving up after 5
end tell
end try
if query as text is equal to "Hello" then my newQuery_(null)
performSelector_withObject_afterDelay_("listenForQuery", missing value, 2.0) -- do it again every two seconds
end listenForQuery
The problem is, the program can't do anything else while the Speech Recognition Server is waiting to hear the phrase. I'd like to allocate a separate thread for the above method so it can run with other parts of my program simultaneously. Is this feasible, and if so, how?
Posted on Jun 14, 2016 4:11 PM
AppleScriptObjC is not multi-threaded, but it looks like you forgot that it can use most of Cocoa's classes and methods. Instead of using AppleScript to control another application, take a look at using NSSpeechRecognizer in yours - you can give it a list of commands to listen for and set a delegate method that gets called when something is recognized.
Posted on Jun 14, 2016 5:46 PM