Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript to Use Voice to Capture a Number then assign to a variable

Hi,

I have seen many examples where you can use applescript to listen for a response and then when hearing the specific response, such as Yes or No, then you can process based on that. Is there a way to have Speech listen for any value and then assign that value to a variable that I could use later in the script?


My example is:

Ask the end user for a number between 1 and 5,000

Based on the number they pick, let's just pop up a dialog box that says, the number you said was xxx where xxx is the value of what they said.


Thanks!

Posted on Dec 12, 2017 3:30 PM

Reply
Question marked as Best reply

Posted on Dec 13, 2017 8:23 AM

And a pure AppleScript solution with try block to catch user cancel event and end processing, or loop until user actually speaks (or enters) a number, and presses return.


use scripting additions


set intvalue to ""

set status to false


repeat while status is false

try

set intvalue to text returned of (display dialog "Speak a number: " default answer "") as integer

if intvalue ≥ 1 and intvalue ≤ 5000 then set status to true

on error number -128

return -- user clicked Cancel

end try

end repeat


display dialog "Class of intvalue is :" & class of intvalue

3 replies
Question marked as Best reply

Dec 13, 2017 8:23 AM in response to VikingOSX

And a pure AppleScript solution with try block to catch user cancel event and end processing, or loop until user actually speaks (or enters) a number, and presses return.


use scripting additions


set intvalue to ""

set status to false


repeat while status is false

try

set intvalue to text returned of (display dialog "Speak a number: " default answer "") as integer

if intvalue ≥ 1 and intvalue ≤ 5000 then set status to true

on error number -128

return -- user clicked Cancel

end try

end repeat


display dialog "Class of intvalue is :" & class of intvalue

Dec 13, 2017 10:38 AM in response to Pius Kid

If you have Dictation running, it will automatically convert spoken numbers into integers. This is handy if you have an AppleScript dialog open waiting on user input. The user can speak one thousand fifty three, and Dictation will print tentatively echo that text string in the dialog box, abruptly change its mind, and replace the text with with the integer 1053. No need to attempt to control speech recognition server which cannot do this.


Speech recognition server wants to compare what is spoken with what it can find as a designated list element. It cannot find more than one list element, making it hard to recite numbers. Listen continuously is no help either.


use scripting additions


-- with dictation running, speak a number into the dialog, and press return

set intvalue to text returned of (display dialog "Speak a number: " default answer "") as integer

if class of intvalue is integer then display dialog (intvalue as text) & " is an integer."

Dec 12, 2017 9:44 PM in response to VikingOSX

Better still, capture input and if it is not an integer in the specified range, repeat the dialog until they actually input an integer that is in range.


use scripting additions


set intvalue to ""

set status to false

repeat while status is false

try

set intvalue to text returned of (display dialog "Speak a number: " default answer "") as integer

-- is the integer within specified range? returns true or false

set status to (do shell script "ruby -e \"puts (1..5000) === ARGV.first.to_i\" " & intvalue) as boolean

on error

logstatus -- force a continue

end try

end repeat


display dialog "Class of intvalue is :" & class of intvalue

Applescript to Use Voice to Capture a Number then assign to a variable

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.