Время начала и окончания каждого события может быть выражено как интервал, состоящий из двух целочисленных значений, каждое из которых представляет количество минут с полуночи, между которыми вы недоступны.Эти интервалы представляют собой просто числа в числовой строке от 0 до 1440 (количество минут в дне), которые легко инвертировать и конвертировать в даты, чтобы получить доступность.
use application "Calendar"
use scripting additions
property calendar : a reference to calendar "GENERIC CALENDAR"
--------------------------------------------------------------------------------
tell (current date) to set midnight to (it - (its time))
set _E to a reference to (events of my calendar ¬
whose start date ≥ midnight and ¬
end date ≤ (midnight + 1 * days))
set {|d₁|, |d₂|} to {start date, end date} of _E
repeat with i from 1 to length of |d₁|
set |t₁| to (a reference to item i of |d₁|)
set |t₂| to (a reference to item i of |d₂|)
set |t₁|'s contents to (|t₁| - midnight) / minutes
set |t₂|'s contents to (|t₂| - midnight) / minutes
end repeat
set ranges to flatten({0, transpose(|d₁|, |d₂|), days / minutes})
set availability to {}
repeat with i from 1 to length of ranges by 2
set {|t₁|, |t₂|} to {item i, item (i + 1)} of ranges
set end of availability to {¬
midnight + |t₁| * minutes, ¬
midnight + |t₂| * minutes}
end repeat
return availability
--------------------------------------------------------------------------------
# HANDLERS:
to transpose(A, B)
local A, B
tell {}
repeat with i from 1 to A's length
set its end to {item i of A, item i of B}
end repeat
it
end tell
end transpose
to flatten(L)
local L
if L = {} then return {}
if L's class ≠ list then return {L}
flatten(L's first item) & flatten(rest of L)
end flatten
---------------------------------------------------------------------------❮END❯