必须排除与 GUI 的交互(在远程系统上)。
使用 远程系统进行测试 macOS Big Sur 11.6 并在 [√] Allow full disk access for remote users 中选中>系统偏好设置 > 共享 > 远程系统上的远程登录,然后是示例 shell 脚本 代码 在 本地系统 上的 终端 在 ssh 会话 中执行远程系统将为您提供原始转储系统偏好设置中可访问性下列出的内容> 安全和隐私 > 隐私,无需使用 AppleScript 进行 UI 脚本。
sqlite3 '/Library/Application Support/com.apple.TCC/TCC.db' 'SELECT client FROM access WHERE service="kTCCServiceAccessibility";'
在测试系统上,它的输出是:
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/AE.framework/Versions/A/Support/AEServer
com.apple.AccessibilityInspector
com.apple.Automator
com.apple.ScriptEditor2
com.apple.Terminal
com.latenightsw.ScriptDebugger8
如果你真的需要使用 AppleScript,你可以说你需要 输出 漂亮。换句话说,使用 AppleScript script 保存为 shell 脚本 使用 #!/usr/bin/osascript shebang 输出在同一个远程系统将是例如:
AEServer, Accessibility Inspector, Automator, Script Editor, Terminal, Script Debugger
示例 AppleScript 代码:
#!/usr/bin/osascript
set theAccessibilityList to paragraphs of (do shell script "sqlite3 '/Library/Application Support/com.apple.TCC/TCC.db' 'SELECT client FROM access WHERE service=\"kTCCServiceAccessibility\";'")
set theAccessibilityApplicationNamesList to {}
repeat with thisItem in theAccessibilityList
if thisItem starts with "/" then
set shellCommand to (do shell script "f=" & quoted form of thisItem & "; echo ${f##*/}")
set end of theAccessibilityApplicationNamesList to shellCommand
else
try
set end of theAccessibilityApplicationNamesList to the name of application id thisItem
end try
end if
end repeat
return theAccessibilityApplicationNamesList
注意事项:
我在本地系统上创建、保存并使其可执行示例 AppleScript 代码,如上所示然后使用scp将其从本地系统复制到远程系统。
注意:示例 AppleScript 代码 就是这样,并且没有任何包含的错误处理不包含任何适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript Language Guide 中的try statement 和error statement。另请参阅Working with Errors。此外,在适当的情况下,可能需要在事件之间使用delay 命令,例如delay 0.5,延迟的值设置得当。