• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Question on a script
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >Question on a script (From: email@hidden)

  • Prev by Date: Re: "a reference to"
  • Next by Date: Re: Weird reply in Tiger vs. Leopard....is this a bug?
  • Previous by thread: Question on a script
  • Next by thread: Re: Question on a script
  • Index(es):
    • Date
    • Thread