You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Applescript批量拒绝MacOS Calendar选定的日历事件

AppleScript批量拒绝苹果日历邀请实现

需求说明

外出办公时需要批量拒绝指定时段的iCal日历邀请,选中目标事件后运行脚本即可自动完成操作,无需手动逐个点击处理。目前已实现选中事件读取、个人参会记录定位逻辑,仅缺少设置拒绝响应的正确方法。

问题解决核心点

无法设置拒绝状态的原因是:Calendar的participation status是系统预设枚举类型,不能传入字符串值,需要直接使用枚举常量declined,去掉值两端的引号即可正常生效。

修正后可运行完整代码

use scripting additions

set defaultsReply to (do shell script "defaults read com.apple.ical SelectedEvents")
set selectedEvents to parseDefaults(defaultsReply)

if selectedEvents = {} then
    display notification "请重试" with title "未选中任何日历事件"
    return
end if

set declinedCount to 0
repeat with sEvent in selectedEvents
    set {eventID, calendarID} to sqlQuery(sEvent)
    tell application "Calendar"
        set eventReference to event id eventID of calendar id calendarID
        set theAttendees to attendees of eventReference
        
        repeat with anAttendee in theAttendees
            -- 可替换为自己的显示名称,也可替换为邮箱判断避免重名
            if display name of anAttendee is "David Johnson" then
                -- 直接使用枚举常量declined,不要加引号
                set participation status of anAttendee to declined
                set declinedCount to declinedCount + 1
                exit repeat
            end if
        end repeat
    end tell
end repeat

display notification "已拒绝" & declinedCount & "个日历邀请" with title "操作完成"

on parseDefaults(resultText)
    set localUIDs to {}
    set {TID, text item delimiters} to {text item delimiters, quote}
    set resultItems to text items of resultText
    set text item delimiters to TID
    repeat with i from 1 to (count resultItems)
        if i mod 2 = 0 then set end of localUIDs to resultItems's item i
    end repeat
    return localUIDs
end parseDefaults

on sqlQuery(localUID)
    local dateString, localUID
    if localUID contains "/" then
        set {TID, text item delimiters} to {text item delimiters, "/"}
        set {dateString, localUID} to text items of localUID
        set text item delimiters to TID
    end if
    
    set sqlText to "
        SELECT DISTINCT zcalendaritem.zshareduid AS eventID
             , znode.zuid as calID
          FROM zcalendaritem
          JOIN znode
            ON znode.z_pk = zcalendaritem.zcalendar
           AND zcalendaritem.zlocaluid = '" & localUID & "'
        ;"
    
    set sqlPath to POSIX path of (path to library folder from user domain) & "Calendars/Calendar Cache"
    set {TID, text item delimiters} to {text item delimiters, "|"}
    set {eID, cID} to text items of (do shell script "echo " & quoted form of sqlText & " | sqlite3 " & quoted form of sqlPath)
    set text item delimiters to TID
    return {eID, cID}
end sqlQuery

运行注意事项

  • 首次运行需要给脚本授予日历访问权限、全磁盘访问权限(用于读取日历缓存数据库)
  • 可将脚本保存为独立应用,或绑定系统快捷键实现快速触发
  • 若存在重名情况,可将参会人判断条件替换为email of anAttendee is "你的个人邮箱地址",识别准确率更高

内容的提问来源于stack exchange,提问作者naven87

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 23:30:05