【问题标题】:Create list of available times (availability calendar) using Applescript使用 Applescript 创建可用时间列表(可用日历)
【发布时间】:2019-05-17 18:19:33
【问题描述】:

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

我已经能够找到事件,但无法找到事件的“倒数”时间。

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

【问题讨论】:

    标签: applescript


    【解决方案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
    

    【讨论】:

    • 这符合并有效,但似乎采取了最后一个事件,然后输出从那个到一天结束的时间。例如,今天我的最后一个活动在晚上 10 点结束,所以输出是从晚上 10 点到凌晨 12 点。但是,我也有 3-5 的“可用性”。但是谢谢,我会看看这是否有助于开始!
    • 如果您不希望从最后一个事件到一天结束的条目删除end repeat之后的行。
    • Vadian,我的错,我没有意识到日历脚本不会引入重复事件(这就是为什么我的脚本没有提取所有正确的事件)。我已将此标记为答案,但我有一个后续问题,您能想到这如何与两个日历一起使用(即交叉引用两个日历并且只找到两个日历上空白的“可用”时间?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 2014-10-08
      • 2014-11-15
      相关资源
      最近更新 更多