I want to add a speakable item that can change a variable that I can access from another script. I’m trying to make an alarm clock that I can “snooze” with a voice command. If this is a stupid question I’m sorry! I’ve done some searching here on the forums and come up empty on how to make this happen.
Thanks!
Ok, so I figured out how the speechrecognition server works, and now realize I was barking up the wrong tree. I’ve got a workable script now that can be run from an iCal event. I’m hoping for a critique if anyone has the time to look over my work.
Thanks!
turnOnSpeech()
set volume 10 --system volume max
set heardPhrase to ""
repeat until heardPhrase is not "" --keep trying till we get a response
set firstAttempt to {"Its time to wake up, sir", "wake up, sir", "Your day awaits you, sir", "Stop snoring sir and wake up.", "Wake up now or you will regret it later, sir", "Please, sir wake up"}
set theFirstChoice to some item of firstAttempt
say theFirstChoice & "It is now " & speakabletime() displaying theFirstChoice with waiting until completion
try --attempt to retrieve a response
tell application "SpeechRecognitionServer"
set heardPhrase to listen for {"snooze", "play music", "Ok, I'm up"}
end tell
end try
end repeat
set snoozePhrase to "snooze"
if heardPhrase is "snooze" then
repeat while snoozePhrase is "snooze"
set secondAttempt to {"Are you ready to wake up, sir?", "Get up, lazy bones", "No more stalling sir get up"}
set theSecondChoice to some item of secondAttempt
set currentTime to (time of (current date))
set snoozeTime to currentTime + 300 --set snooze to +5 minutes
repeat
if snoozeTime < time of (current date) then exit repeat --repeat until 5 minutes have elapsed
end repeat
say theSecondChoice & "It is now " & speakabletime() displaying theSecondChoice with waiting until completion
try --try to get a response
tell application "SpeechRecognitionServer"
set snoozePhrase to listen for {"Ok, I'm up", "snooze"}
end tell
end try
end repeat
else if heardPhrase is "play music" then --open iTunes and play random track from my fav playlist
tell application "iTunes"
activate
set sound volume to 5
tell playlist "big jams"
set randomtrack to some track
play randomtrack
end tell
end tell
end if
turnoffSpeech()
----------------------------------Handlers------------------------------------
on speakabletime() --Returns speech friendly time string
-- Get the "hour"
set timeStr to time string of (current date)
set Pos to offset of ":" in timeStr
set theHour to characters 1 thru (Pos - 1) of timeStr as string
set timeStr to characters (Pos + 1) through end of timeStr as string
-- Get the "minute"
set Pos to offset of ":" in timeStr
set theMin to characters 1 thru (Pos - 1) of timeStr as string
set timeStr to characters (Pos + 1) through end of timeStr as string
--Get "AM or PM"
set Pos to offset of " " in timeStr
set theSfx to characters (Pos + 1) through end of timeStr as string
return (theHour & ":" & theMin & " " & theSfx) as string
end speakabletime
----------------------------------------------------------------
on turnOnSpeech() --turns on speech recognition
activate application "System Preferences"
tell application "System Events"
tell application process "System Preferences"
tell window 1
click button "Show All" of group 1 of group 2 of tool bar 1 --in case another pane is already open
tell button "Speech" of scroll area 1
repeat until exists --wait until the object is accessible
delay 0.2
end repeat
click
end tell
tell radio button "Speech Recognition" of tab group 1
repeat until exists
delay 0.2
end repeat
click
end tell
tell radio button "Settings" of tab group 1 of tab group 1
repeat until exists --wait until the object is accessible
delay 0.2
end repeat
click
end tell
tell radio button "On" of radio group 1 of tab group 1
repeat until exists --wait until the object is accessible
delay 0.2
end repeat
click
end tell
end tell
end tell
end tell
tell application "System Preferences"
quit
end tell
end turnOnSpeech
-----------------------------------------------------------------------
on turnoffSpeech() --turns off speech recognition
activate application "System Preferences"
tell application "System Events"
tell application process "System Preferences"
tell window 1
click button "Show All" of group 1 of group 2 of tool bar 1 --in case another pane is already open
tell button "Speech" of scroll area 1
repeat until exists --wait until the object is accessible
delay 0.2
end repeat
click
end tell
tell radio button "Speech Recognition" of tab group 1
repeat until exists --wait until the object is accessible
delay 0.2
end repeat
click
end tell
tell radio button "Settings" of tab group 1 of tab group 1
repeat until exists --wait until the object is accessible
delay 0.2
end repeat
click
end tell
tell radio button "off" of radio group 1 of tab group 1
repeat until exists --wait until the object is accessible
delay 0.2
end repeat
click
end tell
end tell
end tell
end tell
tell application "System Preferences"
quit
end tell
end turnoffSpeech
I would turn you 2 “turn speech on and off” handlers into this one. You pass true to turn speech on and false to turn speech off. It has 2 advantages, 1) it’s less code and 2) I added a check to see if system preferences (SP) was running when the handler is called. You don’t really want to quit SP if SP was running. So if it wasn’t running then it gets quit, but if it was then it is hidden.
on turnOnOffSpeech(shouldTurnOn) -- true or false... true for turning it on, false for off
-- is system preferences running... we only quit it if it isn't
set sytemPrefsIsRunning to false
try
tell application "System Events"
get first application process whose name is "System Preferences"
end tell
set sytemPrefsIsRunning to true
end try
activate application "System Preferences"
tell application "System Events"
tell application process "System Preferences"
tell window 1
click button "Show All" of group 1 of group 2 of tool bar 1 --in case another pane is already open
repeat until exists (button "Speech" of scroll area 1) --wait until the object is accessible
delay 0.2
end repeat
click button "Speech" of scroll area 1
repeat until exists (radio button "Speech Recognition" of tab group 1) --wait until the object is accessible
delay 0.2
end repeat
click radio button "Speech Recognition" of tab group 1
repeat until exists (radio button "Settings" of tab group 1 of tab group 1) --wait until the object is accessible
delay 0.2
end repeat
click radio button "Settings" of tab group 1 of tab group 1
if shouldTurnOn then
repeat until exists (radio button "On" of radio group 1 of tab group 1) --wait until the object is accessible
delay 0.2
end repeat
click radio button "On" of radio group 1 of tab group 1
else
repeat until exists (radio button "off" of radio group 1 of tab group 1) --wait until the object is accessible
delay 0.2
end repeat
click radio button "off" of radio group 1 of tab group 1
end if
end tell
end tell
end tell
-- if system prefs was running then return to "show all" and hide it else quit it
if sytemPrefsIsRunning then
tell application "System Events"
tell application process "System Preferences"
tell window 1 to click button "Show All" of group 1 of group 2 of tool bar 1
set visible to false
end tell
end tell
else
tell application "System Preferences" to quit
end if
end turnOnOffSpeech
Here’s another one regarding the speakabletime() handler. You can get the hour and the minutes from the current date but just using “hours of current date” or “minutes of current date”.
on speakabletime() --Returns speech friendly time string
-- get the hours and minutes
set theDate to current date
set theMin to minutes of theDate
set theHour to hours of theDate
-- adjust the hour if necessary (given in military time) and calculate "AM or PM"
if theHour is greater than 12 then
set theHour to theHour - 12
set theSfx to "PM"
else if theHour is 12 then
set theSfx to "PM"
else
set theSfx to "AM"
end if
return (theHour as text) & ":" & (theMin as text) & space & theSfx
end speakabletime