从 Mac OS 10.9 Mavericks 开始,Apple要求必须为每个想要使用它的应用程序显式启用辅助访问。
对于给定的应用程序,我编写了以下 AppleScript 函数来处理这个问题。该脚本适用于我,但它有两个缺陷:
当操作系统以英语以外的语言运行时,硬编码的按钮名称会出错,脚本会失败。我怎样才能发现操作系统正在运行什么语言,以及“单击锁定进行更改”的名称。按钮会有那种语言?或者,有没有办法在不读取按钮名称的情况下确定此按钮是否处于
locked
、authenticating
或状态?unlocked
该脚本在等待用户输入管理员用户名和密码时使用紧密的重复循环。有没有更好的策略可以用来等到对话被成功解除?
====
set output to allowAssistiveAccessFor("Skype")
if (the |quitWhenDone| of output) then
tell application "System Preferences" to quit
end if
on allowAssistiveAccessFor(applicationName)
set quitWhenDone to not (application "System Preferences" is running)
set output to {quitWhenDone:quitWhenDone}
tell application "System Preferences"
activate
reveal anchor "Privacy_Accessibility" of pane id "com.apple.preference.security"
tell application "System Events"
tell process "System Preferences"
-- Find the table that contains the application icons and checkboxes
try
set appTable to table 1 of scroll area 1 of group 1 of tab group 1 of window "Security & Privacy"
on error errorMessage
return output & {state:-1, message:errorMessage}
end try
set total to the number of rows of appTable
-- Find the row that refers to applicationName
repeat with rowNumber from 1 to total
if (name of UI element 1 of row rowNumber of appTable = applicationName) then
set appCheckbox to checkbox 1 of UI element 1 of row rowNumber of appTable
if (value of appCheckbox as boolean) then
-- Assistive access is already enabled for this application
return output & {state:0, message:"Already enabled"}
else
-- Click the “Click the lock to make changes.” button.
if exists button "Click the lock to make changes." of window "Security & Privacy" then
click button "Click the lock to make changes." of window "Security & Privacy"
-- The user will now have to enter an admin password. This can take some time.
-- The name of the button will change to "Authenticating"...
set unlocking to button "Authenticating…" of window "Security & Privacy"
repeat while exists unlocking
end repeat
-- ... and then to "Click the lock to prevent further changes." ... unless the user cancelled
if exists button "Click the lock to make changes." of window "Security & Privacy" then
return output & {state:-1, message:"User cancelled"}
end if
end if
-- Click the <applicationName> checkbox.
-- If we had to unlock the Security & Privacy pane, then an immediate click might not have
-- an effect. Try as many times as possible for 1 second, and give up if unsuccessful
set failMessage to "Cannot allow the " & applicationName & " application to control your computer"
set endDate to (current date) + 1.0 -- 1 second from now
repeat
try
if ((current date) > endDate) then
-- Time's up
return output & {state:-1, message:failMessage}
end if
click appCheckbox
if (value of appCheckbox as boolean) then
return output & {state:0, message:"Success"}
end if
on error errorMessage
-- Something dreadful happened. Keep trying until time is up
end try
end repeat
end if
end if
end repeat
end tell
end tell
end tell
return output & {state:-1, message:"Application " & applicationName & " not found"}
end allowAssistiveAccessFor