Author Topic: AppleScript - Help needed ...  (Read 757 times)

wurzelmac

  • Storm
  • *****
  • Posts: 1165
    • ITIROLPR2
    • Wetterstation Prägraten am Großvenediger
  • Station Details: Davis Vantage PRO2 Plus (24h fan aspirated, wireless) with UV/Solar | Weather Envoy Data Logger | Mac mini Mid 2011 2,7 GHz Intel Core i7 with 12 GB of RAM | 2x Canon EOS 1100D | macOS 10.13.6 HighSierra
Re: AppleScript - Help needed ...
« Reply #15 on: October 27, 2018, 08:30:02 AM »
Dear Edouard and scripters  [computer],

after taking some time to dive into AppleScript a bit deeper and using Dr. Google to look around a bit on the web i found the Introduction to AppleScript Language Guide and here is what I need.  :)
So the script I am running at the moment is working and doing what I want so far:

Code: [Select]
set msg to do shell script "/usr/local/bin/python2.7 /Users/reinhard/schnee_client.py"

set height to 2030 #height of sensor above ground

considering case
try
set x to msg as number
set x to height - x
#resetting to zero to avoid negative numbers
if x is less than 0 then
set x to 0
end if
set snow_cm to x
#converting from mm to cm
set snow_cm to snow_cm / 10
#rounding to nearest ½ cm
set quantum to 0.5
set snow_cm to (round snow_cm / quantum) * quantum
set t to (time string of (current date))
#resetting to zero to force floating graphdraw beginning at 0
#but leaving gaugedraw with a true daily Hi Lo value till short before midnight
if t is greater than "23:59:00" and t is less than "23:59:59" then
set snow_cm to 0
end if
return snow_cm
end try
end considering
Reinhard


xairbusdriver

  • Storm
  • *****
  • Posts: 2174
    • EW7115 (E7115)
    • KTNGERMA20
    • Mid-South Weather
  • Station Details: Davis VP2 wireless + remote Anemometer/2014 Mac min - 10.13.4/WC 3.0
Re: AppleScript - Help needed ...
« Reply #16 on: October 27, 2018, 05:05:03 PM »
Sounds familiar... ;)

elagache

  • Global Moderator
  • Storm
  • *****
  • Posts: 4968
    • DW3835
    • KCAORIND10
    • Canebas Weather
  • Station Details: Davis Vantage Pro-2, MacBook Pro (Early 2011), macOS 10.13.6, WeatherCat 3
Two answers (Re: AppleScript - Help needed ...)
« Reply #17 on: October 27, 2018, 10:21:17 PM »
Dear Reinhard, X-Air, and WeatherCat scripters,

Well I'm still sore and it didn't help that I had to continue trimming our hedge of Oleanders this afternoon, but hopefully I can get back to your questions today.

Yes, indeed - but since I wasn't able to round it to at least 1 cm, how should I be able to round it to the nearest ½ cm? Ahh I know, Edouard will point me into the right direction...  [bounce]

Well, I had to search the web for a solution myself, but it turns out to be extremely simple.  The goal is to get a value to the nearest ½ value.  So first multiply by 2.   Then round the result to the nearest whole integer.  Then divide by 2 (of course as a floating point operation!)  Here is a little AppleScript function that does the trick:

Code: [Select]
set snow_mm to 75.26
set snow_mm to roundToHalf(snow_mm)

on roundToHalf(numberToRound)
set numberToRound to (round (numberToRound * 2)) as real
return (numberToRound / 2)
end roundToHalf

Another thing that could be possible via AppleScript is to "set" the value to zero ( "0" ) every one minute after midnight? With this trick I can force the graph to begin at zero. Possible?

I see that you have already come up with an alternative, but in my AppleScripts I do this differently.  There is a way to get AppleScript to return the number of seconds since midnight and that's what I've use.  Here is a snippet that shows the basic idea:

Code: [Select]
set daySeconds to time of (current date) -- get number of seconds since midnight

if (daySeconds ≤ 60) then -- If midnight occurred less than a minute ago
set dailySnowfall to 0
end if

I took a look at your script.  So I assume that you are putting all this AppleScript into one of WeatherCat's synthetic channels?  This might be a bit risky because WeatherCat has to wait for the script to complete before it can do anything else in that particular thread.  I don't remember exactly, but this might be the main thread.  Stu would know for sure.

I have a similar situation for WeatherCat memory.  I collect that memory data in an independent AppleScript that creates a tiny file with just memory used as a single number.  The synthetic channel therefore only needs to read the number and can immediately move on.  Perhaps you should ask Stu if what you are doing is safe.  If it is a bit of a risk, I can explain how to move the processing of the data from the ultrasonic sensor out of the synthetic channel and just have a tiny bit of AppleScript that reads the snow level from a file.

Cheers, Edouard

wurzelmac

  • Storm
  • *****
  • Posts: 1165
    • ITIROLPR2
    • Wetterstation Prägraten am Großvenediger
  • Station Details: Davis Vantage PRO2 Plus (24h fan aspirated, wireless) with UV/Solar | Weather Envoy Data Logger | Mac mini Mid 2011 2,7 GHz Intel Core i7 with 12 GB of RAM | 2x Canon EOS 1100D | macOS 10.13.6 HighSierra
Re: AppleScript - Help needed ...
« Reply #18 on: October 28, 2018, 06:01:22 AM »
Hello Edouard,

thanks for looking into my script and your thoughts about it - much appreciated!
Reinhard


wurzelmac

  • Storm
  • *****
  • Posts: 1165
    • ITIROLPR2
    • Wetterstation Prägraten am Großvenediger
  • Station Details: Davis Vantage PRO2 Plus (24h fan aspirated, wireless) with UV/Solar | Weather Envoy Data Logger | Mac mini Mid 2011 2,7 GHz Intel Core i7 with 12 GB of RAM | 2x Canon EOS 1100D | macOS 10.13.6 HighSierra
Re: AppleScript - Help needed ...
« Reply #19 on: October 28, 2018, 09:50:00 AM »
Reinhard