第一步:创建快捷指令

打开 Shortcuts

点击右上角 + 新建快捷指令。

添加动作 1:日期

搜 添加 日期(Current Date) 动作,默认为当前时间

添加动作 2:格式化日期

添加 格式化日期,日期格式 自定义,填 yyyy-MM-dd HH:mm:ss

添加动作 3:Applescript

on run {input, parameters}
  -- 稍微延长一点延迟,确保触发快捷键的手指已经离开键盘
  delay 0.1
  -- display dialog "Current date"

  -- 将 Shortcuts 传入的 list 转换为字符串
  set ts to item 1 of input as string

  tell application "System Events"
    -- 释放可能被系统残留挂起的修饰键状态
    --  键盘区数字的 Key Code 分布是乱序的
    set keyCodeMap to {29, 18, 19, 20, 21, 23, 22, 26, 28, 25}

    key up command
    key up option
    key up control
    key up shift


    repeat with i from 1 to length of ts
      set c to character i of ts
      set charID to id of c
      if c is ":" then
        -- 分号,加 shift 变成冒号
        key code 41 using {shift down}
      else if c is "-" then
        -- 减号,不需要 shift
        key code 27
      else if c is space then
        key code 49
      else if charID ≥ 48 and charID ≤ 57 then
        -- ASCII 码范围过滤 转换算出 1 到 10 的索引
        set targetIndex to charID - 47

        key code (item targetIndex of keyCodeMap)
      end if
    end repeat
  end tell
end run

第二步:设为快速操作

点快捷指令右上角

勾 Use as Quick Action(用作快速操作)

选 任何应用程序

第三步:绑定快捷键

新版macOS可以直接绑定。

之前的:系统设置 → 键盘 → 键盘快捷键 → 服务(或“快速操作”)


我习惯的方式是右手 Cmd+Opt+T 。

以前觉得 applescript 慢,但是现在反而发现需要 delay 0.1 否则会触发 Cmd+Opt 的连招

本来AI给的版本是 keystroke 指令,容易误触 modifier keys,所以改成 key code。

还以为 AI 写错了,没想到 mac 的 0-9 数字键code 居然不是连续的。

不过这JB玩意不稳定,一会儿授权失效了,需要去 设置 - 隐私 - 辅助功能 里删除 Shortcuts 再添加。。