Re: Question on a script
Re: Question on a script
- Subject: Re: Question on a script
- From: Stan Cleveland <email@hidden>
- Date: Thu, 17 Jan 2008 14:33:24 -0800
- Thread-topic: Question on a script
Title: Re: Question on a script
On 1/17/08 12:54 PM, email@hidden wrote:
I have this script that I run. I want it to write a log ONLY if there is a change in any of the values.
Will this do the job?
set Temp_Outside to (value of "Temperature Outside.web") as integer
set Temp_Upstairs to (value of "Temperature Upstairs.web") as integer
set Temp_Downstairs to (value of "Temperature Downstairs.web") as integer
set Temp_Main_Floor to (value of "Temperature Main Floor.web") as integer
if (value of "Temperature Outside.web") is not equal to Temp_Outside then
if (value of "Temperature Upstairs. web") is not equal to Temp_Upstairs then
if (value of "Temperature Downstairs.web") is not equal to Temp_Downstairs then
if (value of "Temperature Main Floor.web") is not equal to Temp_Main_Floor then
write log "Outside: " & Temp_Outside & " Upstairs: " & Temp_Upstairs & " Downstairs: " & Temp_Downstairs & " Main floor: " & Temp_Main_Floor
end if
end if
end if
end if
Hi Chuck,
No. Because you’re using nested ‘if’ statements, your code will write a log entry only when ALL FOUR temperatures have changed. This is because nested ‘if’ statements put an implied boolean ‘and’ between each of the comparisons, rather than an ‘or’ command, which is what is needed. Also, as written, your code will log the former temperatures, not the current ones. Presumably, you’ll add some sort of loop into the mix to check temperatures over time. You’ll need to update the stored values as they change.
To write a log entry of the current temperatures when ANY of the temperatures have changed over time could look something like this:
-- get initial stored temperatures
set Temp_Outside to (value of "Temperature Outside.web") as integer
set Temp_Upstairs to (value of "Temperature Upstairs.web") as integer
set Temp_Downstairs to (value of "Temperature Downstairs.web") as integer
set Temp_Main_Floor to (value of "Temperature Main Floor.web") as integer
repeat
-- read current temperatures
set new_Outside to (value of "Temperature Outside.web") as integer
set new_Upstairs to (value of "Temperature Upstairs.web") as integer
set new_Downstairs to (value of "Temperature Downstairs.web") as integer
set new_Main_Floor to (value of "Temperature Main Floor.web") as integer
-- compare to stored values
if (new_Outside is not equal to Temp_Outside) or ¬
(new_Upstairs is not equal to Temp_Upstairs) or ¬
(new_Downstairs is not equal to Temp_Downstairs) or ¬
(new_Main_Floor is not equal to Temp_Main_Floor) then
write (log ("Outside: " & new_Outside & " Upstairs: " & new_Upstairs & " Downstairs: " & new_Downstairs & " Main floor: " & new_Main_Floor))
-- update stored values
set Temp_Outside to new_Outside
set Temp_Upstairs to new_Upstairs
set Temp_Downstairs to new_Downstairs
set Temp_Main_Floor to new_Main_Floor
end if
end repeat
The ‘repeat’ loop shown is merely representative of something smarter (perhaps an idle handler), since the logic as it is written would result in dozens or even hundreds of temperature checks per minute. That’s overkill, to say the least.
Good luck with your project!
Stan C.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden