Variable not defined

I am having trouble with the script below, every time I try to run it, I get the error

error "The variable speakHour is not defined." number -2753 from "speakHour"



I am fairly new to applescript and as I did not write this script myself, I am unfamiliar with it, any help would be greatly appreciated.



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/GoodMorningWeather.app"
	tell application "Calendar"
		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


googleman76

Hi,

the hour/minutes computation of the default time “6 30” is hours: "6 " and minutes: “30”.
The hours value does not match any compared value “00” to “12”

I recommend to ask for 6:30 and use text item delimiters to separate the values.
A value without a colon is considered as a single hours value


sset 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 textItems to text items of theTime
set text item delimiters to TID
if (count textItems) is 1 then
	set theHour to theTime
else if (count textItems) is 2 then
	set {theHour, theMinutes} to textItems
else
	say "That is an un known time" using "Alex"
	say "You're alarm has not been set" using "Alex"
	return
end if

later it’s easier to work with integers to define speakHours


set theHourAsInteger to theHour as integer
set speakHour to item theHourAsInteger of {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"}

if (theMinutes as integer) = 0 then
	set theMinutes to "Oh Clock"
end if

Consider that your script does not catch errors like a zero value for the hours or any non-numeric value