Haven’t been doing Applescript for too long, and now I find myself dealing with JSON for the first time.
So I have wifi lights and I frequently use my own Applescript “normalize lighting conditions”, which first checks what time it is now, then goes through a list of conditions such that “if the time now is between hours x and x, adjust the lighting like so”. The point is that “normal lighting” depends on how dark it is outside, the lighting should promote energized mood in the daytime and aid sleepiness later on. But in my geographic location, the seasons vary so dramatically that in the summer the night barely comes, and in the winter, the opposite is true. The only way to keep this script on point throughout the year is to make it understand the sun’s position.
I found this API for sunset and sunrise times.
I got JSON Helper to help.
As a total JSON newbie I just spent hours and hours just to get everything right and I’m finally able to not just fetch the results but quote an individual one and display it to myself as a notification. (not the result I need, just used the notification action to indicate if and when the script actually works).
In my earlier time comparison scripts, I’ve used this hour only form, here’s a little demo in the spirit of it:
---Asking what time it is
set rightNow to (get current date)
set hoursNow to hours of rightNow
delay 3
---Act based on what time it is
if (hoursNow ≥ 7) or (hoursNow ≤ 23) then
display notification "You should be in bed."
else if (hoursNow ≥ 22) or (hoursNow ≤ 8) then
display notification "Good luck staying awake."
end if
With JSON Helper installed, this test script with test coordinates finally gives me just one of the results, in this case, the sunrise time:
(the others are there in place at this point for nothing, but will actually be necessary in the final script because the time comparison will have to perform quite a bit of cross-checking.)
tell application "JSON Helper"
set AstroQuery to fetch JSON from ("http://api.sunrise-sunset.org/json?lat=36.7201600&lng=-4.4203400")
set nowSunrise to sunrise of results of AstroQuery as string
set nowSunset to sunset of results of AstroQuery as string
set nowSolarNoon to solar_noon of results of AstroQuery as string
set nowDayLength to day_length of results of AstroQuery as string
set nowCivilTwilightBegin to civil_twilight_begin of results of AstroQuery as string
set nowCivilTwilightEnd to civil_twilight_end of results of AstroQuery as string
set nowNauticalTwilightBegin to nautical_twilight_begin of results of AstroQuery as string
set nowNauticalTwilightEnd to nautical_twilight_end of results of AstroQuery as string
set nowAstronomicalTwilightBegin to astronomical_twilight_begin of results of AstroQuery as string
set nowAstronomicalTwilightEnd to astronomical_twilight_end of results of AstroQuery as string
display notification nowSunrise
end tell
But the result of it is in the excessive form of “6:15:25 AM”. I’d like it to be in 24 hour form, and the seconds are an unnecessary piece of information when it comes to determining the luminosity level of the sky. Is there any smart way to manipulate the result into 24-hour form and get rid of the seconds?
And from there, how would I make this format comparable to my Mac’s local time now that the minutes would be included too?
Some kind of “if the last two characters of result includes ‘PM’, then remove the last three characters of result, take the first number of result and add 12, then take this newly tweaked result and whatever characters come before the first separator character, set it as Hour24” -logic?
With my old logic, I manage to isolate the current minutes in the same way as I do hours:
---Test to retrieve hours and minutes
set rightNow to (get current date)
set minutesNow to minutes of rightNow
set hoursNow to hours of rightNow
display notification hoursNow
delay 3
display notification minutesNow
From there on again apparently not quite experienced enough to figure out how I would combine the retrieved hours and minutes into one, I get errors when I try to include both green items in the same test notification. Even so “ in this whole project, the time separator character might not even be necessary, if inserting it back in there again would require additional steps.