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

如何检测系统偏好设置安全与隐私面板中选中的Anchor?

检测系统偏好设置「安全与隐私」面板当前选中的Anchor

要获取「安全与隐私」面板中当前选中的Anchor,AppleScript本身没有直接可调用的属性,需借助System Events进行UI脚本操作,具体实现如下:

前提条件

需在「系统偏好设置」→「安全与隐私」→「隐私」→「辅助功能」中,添加并允许以下程序权限:

  • System Events
  • 运行此脚本的程序(如Script Editor或你打包的应用)

实现代码

tell application "System Preferences"
    activate
    set current pane to pane id "com.apple.preference.security"
end tell

tell application "System Events"
    tell process "System Preferences"
        -- 定位安全与隐私面板的侧边栏
        set sidebar to outline 1 of scroll area 1 of window "安全与隐私"
        -- 遍历侧边栏项,找到选中的条目
        repeat with itemRef in rows of sidebar
            if value of attribute "AXSelected" of itemRef is true then
                set selectedItemName to value of static text 1 of itemRef
                exit repeat
            end if
        end repeat
    end tell
end tell

-- 侧边栏显示名称与Anchor名称的映射表
set anchorMapping to {¬
    "通用":"General", ¬
    "防火墙":"Firewall", ¬
    "文件Vault":"FDE", ¬
    "高级":"Advanced", ¬
    "隐私":"Privacy", ¬
    "辅助功能":"Privacy_Accessibility", ¬
    "通讯录":"Privacy_Contacts", ¬
    "日历":"Privacy_Calendars", ¬
    "提醒事项":"Privacy_Reminders", ¬
    "麦克风":"Privacy_Microphone", ¬
    "摄像头":"Privacy_Camera", ¬
    "定位服务":"Privacy_LocationServices", ¬
    "系统服务":"Privacy_SystemServices", ¬
    "所有文件":"Privacy_AllFiles" ¬
}

set selectedAnchor to item selectedItemName of anchorMapping
-- 判断是否选中目标Anchor
if selectedAnchor is "Privacy_Accessibility" then
    display dialog "当前选中了「辅助功能」项(对应Privacy_Accessibility Anchor)"
else
    display dialog "当前选中的是「" & selectedItemName & "」,对应Anchor:" & selectedAnchor
end if

代码说明

  1. 先激活并定位到「安全与隐私」面板;
  2. 通过System Events获取面板侧边栏的UI元素,遍历找到AXSelected属性为true的条目,即当前选中的侧边栏名称;
  3. 利用映射表将侧边栏显示名称转换为对应的Anchor名称,最终判断是否为目标Anchor。

注意:不同macOS版本的UI元素结构可能略有差异,若脚本报错,可在Script Editor的「显示」→「显示检查器」中查看UI元素层级,调整代码中的元素定位路径。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 06:41:27