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

时间:2019-05-16 14:56:10

标签: applescript

我正在尝试使用适用于Mac的applescript创建可用性日历。这将查询应用程序“ Calendar”,获取当天的所有事件(最好是连续几天),然后给我事件之间的时间,以便我可以通过电子邮件发送该事件,聊天等

我已经能够找到事件,但还没有找到事件的“倒数”时间。

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 个答案:

答案 0 :(得分:1)

这是一种创建范围(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

答案 1 :(得分:0)

每个事件的开始时间和结束时间可以表示为由两个整数值组成的间隔,每个整数值表示自午夜以来不可用的分钟数。然后,这些时间间隔就是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❯