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

macOS锁屏状态下读取通知的AppleScript问题求助

macOS Ventura 锁屏下读取通知的解决方案

核心问题原因

AppleScript通过UI元素读取通知中心的方式,在系统锁屏后会因系统UI权限限制失效,此时无法访问Notification Center的UI组件,导致脚本无输出。以下是可行的修复方案与替代实现。


方案1:修复AppleScript(带错误处理+后台定时执行)

带错误处理的脚本代码

on run
    repeat
        try
            tell application "System Events"
                -- 定位通知中心的UI结构
                set notificationCenterWindow to window "Notification Center" of application process "NotificationCenter"
                set scrollArea to scroll area 1 of group 1 of notificationCenterWindow
                set notificationGroups to groups of UI element 1 of scrollArea
                
                if notificationGroups is not {} then
                    set latestGroup to first item of notificationGroups
                    set latestNotification to first UI element of latestGroup
                    set notificationTitle to value of static text 1 of latestNotification
                    
                    -- 将标题传递给目标脚本(替换为你的脚本路径)
                    do shell script "osascript /path/to/your/target/script.scpt '" & quoted form of notificationTitle & "'"
                    log "[SUCCESS] 读取通知: " & notificationTitle
                else
                    log "[INFO] 无新通知"
                end if
            end tell
        on error errMsg number errNum
            log "[ERROR] 代码 " & errNum & ": " & errMsg
            -- 锁屏时的权限错误无需中断循环,继续执行
        end try
        delay 10
    end repeat
end run

后台定时配置(使用launchd,更可靠)

手动循环脚本容易被中断,用launchd实现后台定时执行:

  1. 创建plist文件(路径:~/Library/LaunchAgents/com.your.notification.reader.plist):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.your.notification.reader</string>
    <key>ProgramArguments</key>
    <array>
        <string>osascript</string>
        <string>/path/to/your/notification/script.scpt</string>
    </array>
    <key>StartInterval</key>
    <integer>10</integer> <!-- 每10秒执行一次 -->
    <key>RunAtLoad</key>
    <true/> <!-- 开机自动启动 -->
    <key>KeepAlive</key>
    <true/> <!-- 进程意外退出时自动重启 -->
    <key>StandardErrorPath</key>
    <string>~/Library/Logs/notification-reader-error.log</string> <!-- 错误日志路径 -->
    <key>StandardOutPath</key>
    <string>~/Library/Logs/notification-reader-output.log</string> <!-- 输出日志路径 -->
</dict>
</plist>
  1. 加载并启动服务:
launchctl load ~/Library/LaunchAgents/com.your.notification.reader.plist
  1. 权限配置:在「系统设置-隐私与安全性-辅助功能」中添加osascript或终端,确保脚本能访问UI元素。

方案2:直接读取通知数据库(无UI依赖,锁屏可用)

macOS将通知存储在SQLite数据库中,直接读取数据库可绕过UI权限限制,兼容性更强:

Bash脚本实现

#!/bin/bash
NOTIFICATION_DB="$HOME/Library/Application Support/com.apple.notificationcenter/db/db"
TARGET_SCRIPT="/path/to/your/target/script.scpt"

# 查询最新通知标题(按时间倒序)
LATEST_TITLE=$(sqlite3 "$NOTIFICATION_DB" "SELECT title FROM record ORDER BY date DESC LIMIT 1;")

if [ -n "$LATEST_TITLE" ]; then
    # 传递标题给目标脚本
    osascript "$TARGET_SCRIPT" "$LATEST_TITLE"
    echo "$(date): 读取到通知: $LATEST_TITLE" >> ~/Library/Logs/notification-db-reader.log
else
    echo "$(date): 无新通知" >> ~/Library/Logs/notification-db-reader.log
fi

定时执行配置

同样使用launchd,只需将plist中的ProgramArguments替换为:

<array>
    <string>/bin/bash</string>
    <string>/path/to/your/db-reader-script.sh</string>
</array>

注意事项

  • 权限:需在「系统设置-隐私与安全性-完全磁盘访问权限」中添加bash或终端,确保能读取数据库文件。
  • 数据库兼容性:不同macOS版本的数据库结构可能微调,Ventura版本的SQL语句有效,后续版本可能需要调整字段名。
  • 去重优化:若需避免重复处理同一通知,可记录最后处理的record_id,查询时添加WHERE record_id > [last_id]条件。

内容的提问来源于stack exchange,提问作者V.G.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 11:35:04