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

在Automator的AppleScript中为时间加小时创建日历事件出错,如何解决?

AppleScript调整Calendar事件时差报错解决方案

问题说明

原本用于Automator的AppleScript可从指定格式文本提取日期时间并创建macOS Calendar事件,但尝试将事件时间提前6小时时触发错误:

The action “Run AppleScript” encountered an error: “Calendar got an error: Can’t make 8 into type date.”

示例输入文本

4/29/24 - 2:30pm-3:30pm - XYASHDK
4/29/24 - 6:30pm-9:30pm - DLHEOSH

错误根源

原脚本中set apptBeginTZ to ((hours of apptBegin) + 6)仅提取并修改了小时数值,得到的是纯数字(如8),但Calendar的start date和end date参数要求的是date类型对象,而非单纯的小时数,因此触发类型不匹配错误。

修正后的脚本

通过直接对date对象进行时间偏移来实现时差调整,保持参数类型为date:

on run {input, parameters}
    set apptList to input
    repeat with i from 1 to count of apptList
        set appt to item i of apptList
        set AppleScript's text item delimiters to " - "
        set stringItems to text items of appt
        set apptDate to item 1 of stringItems
        set AppleScript's text item delimiters to ""
        
        set apptTimes to item 2 of stringItems
        set AppleScript's text item delimiters to "-"
        set hoursItems to text items of apptTimes
        set apptBegTime to item 1 of hoursItems
        set apptEndTime to item 2 of hoursItems
        set apptBegin to date (apptDate & " " & apptBegTime)
        set apptEnd to date (apptDate & " " & apptEndTime)
        
        -- 对date对象直接添加6小时,返回新的date类型对象
        set apptBeginTZ to apptBegin + (6 * hours)
        set apptEndTZ to apptEnd + (6 * hours)
        
        tell application "Calendar"
            tell calendar "ABC"
                make new event with properties {summary:"busy", start date:apptBeginTZ, end date:apptEndTZ}
            end tell
        end tell
    end repeat
end run

核心要点

  • AppleScript中hours是内置时间常量,代表3600秒,6 * hours等价于6小时的总秒数,与date对象相加会生成包含正确时间偏移的新date对象,完全符合Calendar的参数要求。
  • 避免单独提取小时数修改,这种操作会丢失日期的完整上下文(如年月日、时区信息),且无法满足Calendar对参数类型的要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 23:38:18