I have recently been trying to create my own jarvis based on http://projectjarvis.com/
Right now I am writing the applescript for the alarm function but can’t seem to get it to work properly. When I run the script I am trying to get the application to say the time that I input, instead I get the following error message
error “The variable speakHour is not defined.” number -2753 from “speakHour”
Here is the code
set theBoolean to "True"
say "What Time Would You Like To Wake Up?" using "Alex"
set theTime to the text returned of (display dialog "What Time Would You Like To Wake Up?" default answer "6 30")
if the length of theTime is 1 then
set theHour to "0" & theTime as string
set theMinutes to "00" as string
else if the length of theTime is 2 then
set theHour to theTime as string
set theMinutes to "00" as string
else if the length of theTime is 3 then
set theHour to "0" & character 1 of theTime as string
set theMinutes to characters 2 thru 3 of theTime as string
else if the length of theTime is 4 then
set theHour to characters 1 thru 2 of theTime as string
set theMinutes to characters 3 thru 4 of theTime as string
else
say "That is an un known time" using "Alex"
say "You're alarm has not been set" using "Alex"
return
end if
if (theMinutes as integer) is greater than 60 then
say "That is an un known time!" using "Alex"
say "You're alarm has not been set!" using "Alex"
set theBoolean to "False"
return
end if
if (theHour as integer) is greater than 12 then
say "That is an un known hour!" using "Alex"
say "You're alarm has not been set!" using "Alex"
set theBoolean to "False"
return
end if
set theTime to theHour & ":" & theMinutes & ":00"
say "On which day?" using "Alex"
set theWeekday to the text returned of (display dialog "Which Day?" default answer "")
-- The Weekday of this week (may be before, after, or on today's date)
if theWeekday is equal to "Monday" then
set theDay to DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(current date, Monday, 0)
else if theWeekday is equal to "Tuesday" then
set theDay to DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(current date, Tuesday, 0)
else if theWeekday is equal to "Wednesday" then
set theDay to DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(current date, Wednesday, 0)
else if theWeekday is equal to "Thursday" then
set theDay to DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(current date, Thursday, 0)
else if theWeekday is equal to "Friday" then
set theDay to DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(current date, Friday, 0)
else if theWeekday is equal to "Saturday" then
set theDay to DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(current date, Saturday, 0)
else if theWeekday is equal to "Sunday" then
set theDay to DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(current date, Sunday, 0)
else
set theBoolean to false
say "That is an un known day!" using "Alex"
say "You're alarm has not been set!" using "Alex"
return
end if
say "Would you like me to wake you up in the morning? Or afternoon!" using "Alex"
set theAMPM to the text returned of (display dialog "AM or PM?" default answer "AM")
set theDayOfWeek to weekday of theDay as string
set theMonth to month of theDay as string
set theDayofTheMonth to day of theDay as integer
set theYear to year of theDay as integer
set theDate to (date (theDayOfWeek & ", " & theMonth & " " & theDayofTheMonth & ", " & theYear & " " & theTime & " " & theAMPM))
if theHour is equal to "01" then
set speakHour to "one"
else if theHour is equal to "02" then
set speakHour to "two"
else if theHour is equal to "03" then
set speakHour to "three"
else if theHour is equal to "04" then
set speakHour to "four"
else if theHour is equal to "05" then
set speakHour to "five"
else if theHour is equal to "06" then
set speakHour to "six"
else if theHour is equal to "07" then
set speakHour to "seven"
else if theHour is equal to "08" then
set speakHour to "eight"
else if theHour is equal to "09" then
set speakHour to "nine"
else if theHour is equal to "10" then
set speakHour to "ten"
else if theHour is equal to "11" then
set speakHour to "eleven"
else if theHour is equal to "12" then
set speakHour to "twelve"
end if
if theMinutes is equal to "00" then
set theMinutes to "Oh Clock"
end if
if theBoolean is not equal to "False" then
set theFile to "/Users/porter1/Desktop/Projects Jarvis/AppleScripts/goodMorningWeather.scpt"
tell application "iCal"
set newEvent to make new event at end of events of calendar "Alarm" with properties {summary:"Alarm", start date:theDate}
set theAlarm to make new open file alarm at end of open file alarms of newEvent with properties {trigger interval:0, filepath:POSIX path of theFile}
end tell
if theAMPM is equal to "AM" then
say "Okay! I have set your alarm for, " & theDayOfWeek & " morning, " & " at " & speakHour & theMinutes using "Alex"
else
say "Okay! I have set your alarm for, " & theDayOfWeek & " afternoon, " & " at " & speakHour & theMinutes using "Alex"
end if
else
say "Your alarm has not been set" using "Alex"
end if
on DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(d, w, i) -- returns a date
-- Keep an note of whether the instance value *starts* as zero
set instanceIsZero to (i is 0)
-- Increment negative instances to compensate for the following subtraction loop
if i < 0 and d's weekday is not w then set i to i + 1
-- Subtract a day at a time until the required weekday is reached
repeat until d's weekday is w
set d to d - days
-- Increment an original zero instance to 1 if subtracting from Sunday into Saturday
if instanceIsZero and d's weekday is Saturday then set i to 1
end repeat
-- Add (adjusted instance) * weeks to the date just obtained and zero the time
d + i * weeks - (d's time)
end DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate
Any help would be greatly appreciated,
googleman76
set theBoolean to "True"
say "What Time Would You Like To Wake Up?" using "Alex"
set theTime to the text returned of (display dialog "What Time Would You Like To Wake Up?" default answer "6 30")
set {TID, text item delimiters} to {text item delimiters, {":", " "}}
set theTime to text items of theTime
set text item delimiters to {""}
set theTime to theTime as text
if the length of theTime is 1 then
set theHour to "0" & theTime as text
set theMinutes to "00" as text
else if the length of theTime is 2 then
set theHour to theTime as text
set theMinutes to "00" as text
else if the length of theTime is 3 then
set theHour to "0" & character 1 of theTime as text
set theMinutes to characters 2 thru 3 of theTime as text
else if the length of theTime is 4 then
set theHour to characters 1 thru 2 of theTime as text
set theMinutes to characters 3 thru 4 of theTime as text
else
say "That is an un known time" using "Alex"
say "You're alarm has not been set" using "Alex"
return
end if
if (theMinutes as integer) is greater than 60 then
say "That is an un known time!" using "Alex"
say "You're alarm has not been set!" using "Alex"
set theBoolean to "False"
return
end if
if (theHour as integer) is greater than 12 then
say "That is an un known hour!" using "Alex"
say "You're alarm has not been set!" using "Alex"
set theBoolean to "False"
return
end if
set theTime to theHour & ":" & theMinutes & ":00"
say "On which day?" using "Alex"
set theWeekday to the text returned of (display dialog "Which Day?" default answer "")
-- The Weekday of this week (may be before, after, or on today's date)
if theWeekday is equal to "Monday" then
set theDay to DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(current date, Monday, 0)
else if theWeekday is equal to "Tuesday" then
set theDay to DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(current date, Tuesday, 0)
else if theWeekday is equal to "Wednesday" then
set theDay to DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(current date, Wednesday, 0)
else if theWeekday is equal to "Thursday" then
set theDay to DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(current date, Thursday, 0)
else if theWeekday is equal to "Friday" then
set theDay to DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(current date, Friday, 0)
else if theWeekday is equal to "Saturday" then
set theDay to DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(current date, Saturday, 0)
else if theWeekday is equal to "Sunday" then
set theDay to DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(current date, Sunday, 0)
else
set theBoolean to false
say "That is an un known day!" using "Alex"
say "You're alarm has not been set!" using "Alex"
return
end if
say "Would you like me to wake you up in the morning? Or afternoon!" using "Alex"
set theAMPM to the text returned of (display dialog "AM or PM?" default answer "AM")
set theDayOfWeek to weekday of theDay as text
set theMonth to month of theDay as text
set theDayofTheMonth to day of theDay as integer
set theYear to year of theDay as integer
set theDate to (date (theDayOfWeek & ", " & theMonth & " " & theDayofTheMonth & ", " & theYear & " " & theTime & " " & theAMPM))
if theHour is equal to "01" then
set speakHour to "one"
else if theHour is equal to "02" then
set speakHour to "two"
else if theHour is equal to "03" then
set speakHour to "three"
else if theHour is equal to "04" then
set speakHour to "four"
else if theHour is equal to "05" then
set speakHour to "five"
else if theHour is equal to "06" then
set speakHour to "six"
else if theHour is equal to "07" then
set speakHour to "seven"
else if theHour is equal to "08" then
set speakHour to "eight"
else if theHour is equal to "09" then
set speakHour to "nine"
else if theHour is equal to "10" then
set speakHour to "ten"
else if theHour is equal to "11" then
set speakHour to "eleven"
else if theHour is equal to "12" then
set speakHour to "twelve"
end if
if theMinutes is equal to "00" then
set theMinutes to "Oh Clock"
end if
if theBoolean is not equal to "False" then
set theFile to "/Users/porter1/Desktop/Projects Jarvis/AppleScripts/goodMorningWeather.scpt"
tell application "iCal"
set newEvent to make new event at end of events of calendar "Alarm" with properties {summary:"Alarm", start date:theDate}
set theAlarm to make new open file alarm at end of open file alarms of newEvent with properties {trigger interval:0, filepath:POSIX path of theFile}
end tell
if theAMPM is equal to "AM" then
say "Okay! I have set your alarm for, " & theDayOfWeek & " morning, " & " at " & speakHour & theMinutes using "Alex"
else
say "Okay! I have set your alarm for, " & theDayOfWeek & " afternoon, " & " at " & speakHour & theMinutes using "Alex"
end if
else
say "Your alarm has not been set" using "Alex"
end if
on DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(d, w, i) -- returns a date
-- Keep an note of whether the instance value *starts* as zero
set instanceIsZero to (i is 0)
-- Increment negative instances to compensate for the following subtraction loop
if i < 0 and d's weekday is not w then set i to i + 1
-- Subtract a day at a time until the required weekday is reached
repeat until d's weekday is w
set d to d - days
-- Increment an original zero instance to 1 if subtracting from Sunday into Saturday
if instanceIsZero and d's weekday is Saturday then set i to 1
end repeat
-- Add (adjusted instance) * weeks to the date just obtained and zero the time
d + i * weeks - (d's time)
end DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate
if theHour is equal to "01" then
set speakHour to "one"
else if theHour is equal to "02" then
set speakHour to "two"
else if theHour is equal to "03" then
set speakHour to "three"
else if theHour is equal to "04" then
set speakHour to "four"
else if theHour is equal to "05" then
set speakHour to "five"
else if theHour is equal to "06" then
set speakHour to "six"
else if theHour is equal to "07" then
set speakHour to "seven"
else if theHour is equal to "08" then
set speakHour to "eight"
else if theHour is equal to "09" then
set speakHour to "nine"
else if theHour is equal to "10" then
set speakHour to "ten"
else if theHour is equal to "11" then
set speakHour to "eleven"
else if theHour is equal to "12" then
set speakHour to "twelve"
end if
can be written as:
set theHours to "01|02|03|04|05|06|07|08|09|10|11|12"
set speakHours to {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "twelve"}
set speakHour to item (((offset of theHour in theHours) + 2) div 3) of speakHours
set theHour to “10” will return “One” in that example
You could simply do this:
set theHour to "06"
set speakHours to {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "twelve"}
set speakHour to item theHour of speakHours
edit: Maybe some useful code for a timer here as well. the code below sets the day to the next weekday, if you choose the same day as today it returns the current date.
set timer to current date
set dayOfTimer to Wednesday
timer + (24 * 3600 * ((((dayOfTimer) + 7) - (timer's weekday)) mod 7))
edit2: Very good day, maybe I need to go to bed, had a few drinks in the bar tonight.
It’s some years since I wrote this handler, since when, weekday arithmetic has become possible in AppleScript. (The weekdays can be coerced ” even automatically ” to the numbers 1 to 7, with Sunday as 1.) The code could therefore be reduced to:
on DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(d, w, i) -- returns a date
set shift to (w - (d's weekday) - 7 * ((i is not 0) as integer)) mod 7 * days
if ((i < 0) and (shift < 0)) then set shift to shift + weeks
return d + shift + i * weeks - (d's time)
end DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate
Or perhaps:
on DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(d, w, i) -- returns a date
tell d's weekday to return d + ((w - it - 7 * ((i is not 0) as integer)) mod 7 * days + ((((i < 0) and (it is not w)) as integer) + i) * weeks - (d's time))
end DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate
The value of 0 you’re using for the ‘i’ parameter returns a date in the same Sunday-start week as the input date.
As you may have seen by now, the fora on this site have [applescript] and [/applescript] tags. Enclosing your code between these when you post causes it to be displayed with the “Open this Scriplet in your Editor:” link as above.