I recently found speech commands; and might I add, I am in love. Currently scripting a weather command that will relay the weather and suggest a proper dress for my daughter when she is picking out her clothes. Everything works great with the exception of my if-then statements that have to to with being greater than a number and less than another. I personally haven’t been able to find anything in regards to combining both greater than and less than, though i am sure its very simple.
The commented out part is where I am experiencing trouble:
set weather_loc to "/Scripts/weather"
set weather_spk to ""
set weather_cur to do shell script (weather_loc & " 48413")
set d to text item delimiters
set text item delimiters to "F"
set weather_cur to weather_cur's text items
set text item delimiters to ""
tell weather_cur to set weather_cur to item 1 & ({""} & rest)
set text item delimiters to d
if weather_cur is less than or equal to "32" then
set weather_spk to "The temperature is currently a freezing " & weather_cur & " degrees. Suggested dress: multiple, heavy layers."
end if
--if weather_cur is greater than 32 and less than or equal to 55 then
--set weather_spk to "The temperature is currently a chilly " & weather_cur & " degrees. Suggested dress: medium layers."
--end if
--if weather_cur is greater than 55 and less than or equal to 75 then
--set weather_spk to "The temperature is currently a plesant " & weather_cur & " degrees. Suggested dress: light layers. T-shirt and jeans are acceptable."
--end if
--if weather_cur is greater than 75 and less than or equal to 85 then
--set weather_spk to "The temperature is currently a warm " & weather_cur & " degrees. Suggested dress: T-shirt and jeans, shorts optional."
--end if
--if weather_cur is greater than 85 and less than or equal to 95 then
--set weather_spk to "The temperature is currently a hot " & weather_cur & " degrees. Suggested dress: T-shirt and jeans. Shorts recommended, tank top optional."
--end if
if weather_cur is greater than "95" then
set weather_spk to "The temperature is currently a blistering " & weather_cur & " degrees. Suggested dress: Tank top and shorts."
end if
say weather_spk
You always have to compare something with something else. So if you compare the same yeriable two times, you have to repeat it.
So, this:
if weather_cur is greater than 32 and less than or equal to 55 then
set weather_spk to "The temperature is currently a chilly " & weather_cur & " degrees. Suggested dress: medium layers."
end if
will have to change into this:
if weather_cur is greater than 32 and weather_cur is less than or equal to 55 then
set weather_spk to "The temperature is currently a chilly " & weather_cur & " degrees. Suggested dress: medium layers."
end if
or
if weather_cur > 32 and weather_cur ≤ 55 then
set weather_spk to "The temperature is currently a chilly " & weather_cur & " degrees. Suggested dress: medium layers."
end if
or you can write out a handler
if validateNumber(weather_cur, 32, 55) then
-- do your thing
end if
on validateNumber(nrtc, x, y)
return (nrtc > x and nrt ≤ y)
end validateNumber
Hope it helps,
ief2
PS: Script Editor will automatically convert <= and => to ≤ and ≥ respectively.
As ief2 has explained, complete comparison statements are needed for each end of the range you’re testing.
Also, the results you’re comparing in your script aren’t numbers but string representations of numbers, so “9” will be judged greater than “85”. (It begins with “9” whereas “85” begins with “8”.) You should either use the ‘considering numeric strings’ attribute (if you’re running Tiger or later) or coerce weather_cur to a number before comparing it.
Logically, if a number is not less than or equal to 32, it must be greater than 32. You can avoid having to test for this again by using ‘else if’, which also saves having to test the later conditions if an earlier one is met:
considering numeric strings
if weather_cur is less than or equal to "32" then
set weather_spk to "The temperature is currently a freezing " & weather_cur & " degrees. Suggested dress: multiple, heavy layers."
else if weather_cur is less than or equal to 55 then
set weather_spk to "The temperature is currently a chilly " & weather_cur & " degrees. Suggested dress: medium layers."
else if weather_cur is less than or equal to 75 then
set weather_spk to "The temperature is currently a plesant " & weather_cur & " degrees. Suggested dress: light layers. T-shirt and jeans are acceptable."
else if weather_cur is less than or equal to 85 then
set weather_spk to "The temperature is currently a warm " & weather_cur & " degrees. Suggested dress: T-shirt and jeans, shorts optional."
else if weather_cur is less than or equal to 95 then
set weather_spk to "The temperature is currently a hot " & weather_cur & " degrees. Suggested dress: T-shirt and jeans. Shorts recommended, tank top optional."
else -- weather_cur is greater than "95"
set weather_spk to "The temperature is currently a blistering " & weather_cur & " degrees. Suggested dress: Tank top and shorts."
end if
end considering
Comparison is a binary operation, so you can just compare two values at a time.
In your case with two variables the simplest is to build up a hierarchy of nested if clauses.
if x less than y then
if temp less than x then
else if temp is x then
else -- temp greater than y
end if
else if x is y then
if temp less than x then
else if temp is x then
else -- temp greater than y
end if
else -- y is greater than x
if temp less than x then
else if temp is x then
else -- temp greater than y
end if
end if
Here I considered even the equal states between two variables just for completeness. Because when many variables are included, it is ok to be sure that one has covered each possibility.
The number of variables raised to two (3^2 in this case) yields the number of cases.
This can be good keep in mind when the logic gets complex.
Have you express this into statements is another story you can combine several cases with less than or equal to
and you can even combine several such statements into bigger statements with and and or.
Something like this the first branch that evaluates TRUE will be executed.
i.e. weather_cur < 0 gives “very cold”
0 <= weather_cur < 32 gives “build snowman”
32 <= weather_cur < 50 gives “wear jacket”
etc.
set weather_cur to (text returned of (display dialog "What's the temp" default answer "")) as number
if weather_cur < 0 then
display dialog "very very cold"
else if weather_cur < 32 then
display dialog "build a snowman"
else if weather_cur < 50 then
display dialog "wear a jacket"
else if weather_cur < 80 then
display dialog "nice day today"
else if weather_cur < 100 then
display dialog "warm summer"
else
display dialog "Welcome to Needles!"
end if