Question on current scripting (improvements)

I have a question on improving my current script…

basically with doing time calculations

what this does is grab the current sunset and sunrise times
and sets my 3 screens to various wallpaper sets throughout the day

also uses hue menu to set various presets of lights (that match my wallpapers)

when I take the sunset time - sunrise time and get a time diff between them and am using whole numbers rather than a time diff as i couldn’t figure out how to get a decent time calculation
IE sunset is 5:45 and a diff is 1:30, i want the sum of those to be 7.15 rather than 6.75

thanks
Darrin

set morningEarly to “Morning Early”
set morningLate to “Morning Late”
set afternoonEarly to “Afternoon Early”
set afternoonLate to “Afternoon Late”
set eveningEarly to “Evening Early”
set eveningLate to “Evening Late”

– get current days details from API
tell application “JSON Helper”
set AstroQuery to fetch JSON from “http://api.sunrise-sunset.org/json?lat=47.6768927&lng=-122.2059833&formatted=0
set nowSunrise to my isoDateTimeString2Date(sunrise of results of AstroQuery)
set nowSunset to my isoDateTimeString2Date(sunset of results of AstroQuery)
end tell
set sunriseToday to (AST format date “HH.mm” of date nowSunrise) as number
set sunsetToday to (AST format date “HH.mm” of date nowSunset) as number
set diff to (sunsetToday - sunriseToday) div 4

– get current hour
set today to current date
set rightnow to (hours of today) & “.” & (minutes of today) as string as number

if (rightnow ≥ sunriseToday - 1 and rightnow < sunriseToday + diff) then
set periodOfDay to morningEarly
else if (rightnow ≥ sunriseToday + diff and rightnow < (sunriseToday + (diff * 2))) then
set periodOfDay to morningLate
else if (rightnow ≥ (sunriseToday + (diff * 2)) and rightnow < (sunsetToday - (diff * 2))) then
set periodOfDay to afternoonEarly
else if (rightnow ≥ (sunsetToday - (diff * 2)) and rightnow < sunsetToday - diff) then
set periodOfDay to afternoonLate
else if (rightnow ≥ sunsetToday - diff and rightnow < sunsetToday) then
set periodOfDay to eveningEarly
else if (rightnow ≥ sunsetToday or rightnow < sunriseToday - 1) then
set periodOfDay to eveningLate
end if

if (rightnow < 22.3 and rightnow > 5) then
tell application “/Applications/Hue Menu.app”
groups “Den”
preset periodOfDay
end tell
else
tell application “/Applications/Hue Menu.app”
all Off
end tell
end if

tell application “Finder”
try
set mainDisplayPicture to my getImageRight(periodOfDay)
set mainMonitorPicture to my getImageMain(periodOfDay)
set secondaryDisplayPicture to my getImageLeft(periodOfDay)

	tell application "System Events"
		set theDesktops to a reference to every desktop
		set picture of item 3 of the theDesktops to secondaryDisplayPicture
		set picture of item 2 of the theDesktops to mainMonitorPicture
	end tell
	set desktop picture to mainDisplayPicture
end try

end tell

on isoDateTimeString2Date(str)
return AST date with format “yyyy-MM-dd’T’HH:mm:ssZ” date string str
end isoDateTimeString2Date

on getImageRight(folderName)
tell application “Finder”
return some file of folder (“Pictures:Wallpapers:right:Time of Day:” & folderName) of home as text
end tell
end getImageRight

on getImageLeft(folderName)
tell application “Finder”
return some file of folder (“Pictures:Wallpapers:left:Time of Day:” & folderName) of home as text
end tell
end getImageLeft

on getImageMain(folderName)
tell application “Finder”
return some file of folder (“Pictures:Wallpapers:main:Time of Day:” & folderName) of home as text
end tell
end getImageMain

Model: macbook pro
Browser: Safari 602.3.12
Operating System: Mac OS X (10.13 Developer Beta 3)

Quick and dirty answer :

set beg to "5:45"
set dur to "1:30"
set HHmm to my addHHmm(beg, dur)

on addHHmm(beg, dur)
	set beg2 to my decoupe(beg, ":")
	set begNum to (beg2's item 1) * 60 + (beg2's item 2)
	set dur2 to my decoupe(dur, ":")
	set durNum to (dur2's item 1) * 60 + (dur2's item 2)
	set theEndNum to begNum + durNum
	set hourNbr to theEndNum div 60
	set the minNum to theEndNum mod 60
	return (hourNbr as text) & ":" & minNum
end addHHmm

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) lundi 31 juillet 2017 19:52:50

Assuming you do actually want to define the split between morning and afternoon as halfway between sunrise and sunset, the AppleScript date arithmetic equivalent to your calculations would be:

-- get current days details from API
tell application "JSON Helper"
    set AstroQuery to fetch JSON from "http://api.sunrise-sunset.org/json?lat=47.6768927&lng=-122.2059833&formatted=0"
    set sunriseToday to my isoDateTimeString2Date(sunrise of results of AstroQuery)
    set sunsetToday to my isoDateTimeString2Date(sunset of results of AstroQuery)
end tell

set diff to (sunsetToday - sunriseToday) / 4 -- A number of seconds.

-- get now     
set rightnow to (current date)

-- Ignore the dates and just compare the times.
set sunriseToday to time of sunriseToday
set sunsetToday to time of sunsetToday
set rightnow to time of rightnow

if (rightnow ≥ sunsetToday or rightnow < sunriseToday - hours) then
	set periodOfDay to eveningLate
else if (rightnow < sunriseToday + diff) then
	set periodOfDay to morningEarly
else if (rightnow < (sunriseToday + (diff * 2))) then
	set periodOfDay to morningLate
else if (rightnow < (sunsetToday - (diff * 2))) then
	set periodOfDay to afternoonEarly
else if (rightnow < sunsetToday - diff) then
	set periodOfDay to afternoonLate
else
	set periodOfDay to eveningEarly
end if

set currentHour to rightnow / hours
if (currentHour < 22.5 and currentHour > 5) then
	tell application "/Applications/Hue Menu.app"
		groups "Den"
		preset periodOfDay
	end tell
else
	tell application "/Applications/Hue Menu.app"
		all Off
	end tell
end if

if … else if … logic is eliminative, so if ‘rightnow’ proves not to be earlier than a certain time, there’s no need to test again to see if it’s the same as or later than that time. However, to take advantage of elimination in this particular script, the very early and very late times have to be eliminated first.

Edit: My code edited to compare the date objects’ times (seconds after midnight) instead of the date objects themselves.

There are two assumptions here:

  1. That sunrise and sunset always occur in that order between any two successive midnights in your part of the world. (Assumed in your original code.)
  2. That the date/times received from the Web site are always those for the calendar date in progress in your part of the world at the instant the script’s run. (An additional assumption in my version before I edited it.) Comparing just the times instead of the dates and times lessens the potential effect on the script if the downloaded data happen to be those for the previous or following day.