使用 Applescript 创建可用时间列表(可用性日历)

Create list of available times (availability calendar) using Applescript

我正在尝试使用 applescript 为我的 mac 创建可用性日历。这将查询应用程序 "Calendar",获取当天的所有事件(理想情况下希望它持续多天),然后给我 事件之间的时间,以便我可以通过电子邮件、聊天等方式发送。

我已经能找到事件了,但还没有找到事件的 "inverse" 时间。

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

这是一种创建范围 (startDate/endDate) 然后用可用的 start/end 日期对

填充列表的方法
-- 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 到 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❯