Создать список доступного времени (календарь доступности), используя Applescript - PullRequest
1 голос
/ 16 мая 2019

Я пытаюсь создать календарь доступности, используя applecript для моего Mac.Это запросит приложение «Календарь», получит все события за день (в идеале это будет несколько дней), а затем даст мне время между событиями, чтобы я мог отправить это по электронной почте,чат и т. д.

Я дошел до того, что смог найти события, но не смог найти "обратное" время событий.

set hours of theStartDate to 0
set minutes of theStartDate to 0
set seconds of theStartDate to 0
set theEndDate to theStartDate + (1 * days) - 1

tell application "Calendar"
    tell calendar "GENERIC CALENDAR"
        every event where its start date is greater than or equal to theStartDate and end date is less than or equal to theEndDate

    end tell
end tell

Ответы [ 2 ]

1 голос
/ 16 мая 2019

Это подход, который создает диапазон (startDate / endDate) и затем заполняет список доступными парами дат начала / окончания

-- set startDate and endDate to todays's midnight and tomorrow's midnight
tell (current date) to set startDate to it - (its time)
set endDate to startDate + 86400

-- availableTimes is the result which contains a list of startDate/endDate pairs
set availableTimes to {}
-- temporary variable which is set to the current start date
set currentStartDate to startDate

tell application "Calendar"
    tell calendar "GENERIC CALENDAR"
        set todaysEvents to every event where its start date ≥ startDate and end date ≤ endDate
        repeat with anEvent in todaysEvents
            set end of availableTimes to {currentStartDate, anEvent's start date}
            set currentStartDate to anEvent's end date
        end repeat
        set end of availableTimes to {currentStartDate, endDate}
    end tell
end tell
0 голосов
/ 17 мая 2019

Время начала и окончания каждого события может быть выражено как интервал, состоящий из двух целочисленных значений, каждое из которых представляет количество минут с полуночи, между которыми вы недоступны.Эти интервалы представляют собой просто числа в числовой строке от 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❯
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...