启用语音控制。 AppleScript
Enable Voice Control. AppleScript
我尝试创建一个 Apple 脚本以在 Mac 中启用语音控制。
这位于:系统偏好设置 > 辅助功能 > 语音控制 > 启用语音控制
我想我接近了。但是不知道怎么叫左边菜单“语音控制”
这是我试过但行不通的方法:
tell application "System Events"
click checkbox "Enable Voice Control" of window "Voice Control" of window "Accessibility" of application process "System Preferences" of application "System Events"
end tell
您在评论中询问是否有其他方法可以获得解决方案。这是一种修改代码的方法。首先,我将逐步解释解决方案。
Update:正如 user3439894 在下面非常有用的评论中指出的那样,这个 if 语句实际上是必要的,原因有几个。它应该始终包含在内。
if running of application "System Preferences" then
quit application "System Preferences"
delay 1
end if
- 这样做是为了使脚本在 运行 时不会失败。注意,网友还指出应该使用killall来保证进程完全终止。
- 它允许应用程序行为的可预测性。
user3439894 还指出我应该添加这个代码块,以防出现时间问题,导致应用程序在关闭过程中尝试重新打开。
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
接下来,在此代码段中,您单击“系统偏好设置”中的“辅助功能”按钮
tell application "System Events"
click button "Accessibility" of scroll area 1 of window 1 of process "System Preferences" -- click the Accessibility button
delay 2 -- while loads
接下来,您 select 设置中的语音控制行。
select row 13 of table 1 of scroll area 1 of window 1 of process "System Preferences"
delay 0.2 -- delay while page loads
最后,单击复选框。正如我在评论中解释的那样,组用于组织元素。
click checkbox "Enable Voice Control" of group 1 of window 1 of process "System Preferences"
end tell
完整(更新)代码:
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
delay 0.1
end if
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
activate application "System Preferences"
tell application "System Events"
click button "Accessibility" of scroll area 1 of window 1 of process "System Preferences" -- click the Accessibility button
delay 2.0 -- delay while page loads
select row 13 of table 1 of scroll area 1 of window 1 of process "System Preferences"
delay 0.2 -- delay while page loads
click checkbox "Enable Voice Control" of group 1 of window 1 of process "System Preferences"
end tell
我对您的代码所做的更正:
- 您不能引用 windows 中尚未在应用程序中打开的元素(就像您对 window 的“启用语音控制”复选框所做的那样 window“辅助功能”),所以首先我打开辅助功能window(点击一个按钮)->语音控制Window(select行),然后found/clicked复选框.
- 然后,我查看了元素的层次结构,以确定 复选框 在语音控制 Window 中的位置。我发现它在 window 1 (语音控制 Window) 的第 1 组中,所以我点击了它。
- 我还添加了延迟(应用程序需要时间在每个 click/select 之后加载)。
此外,这是我找出元素位置的方法。
如果您还没有使用它,可以使用这个名为 Accessibility Inspector 的内置应用程序来帮助您定位元素。
你也可以在applescript中使用get。例如,
tell application "System Events"
get buttons of scroll area 1 of window 1 of process "System Preferences"
end tell
会return:
... button "Accessibility" of scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events", button "Screen Time" of scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events",...
另一个例子:
tell application "System Events"
get UI elements of window 1 of process "System Preferences"
end tell
这是一些输出
...scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events", toolbar 1 of window "System Preferences" of application process "System Preferences" of application "System Events..."
一个非常有用的工具。
我尝试创建一个 Apple 脚本以在 Mac 中启用语音控制。
这位于:系统偏好设置 > 辅助功能 > 语音控制 > 启用语音控制
我想我接近了。但是不知道怎么叫左边菜单“语音控制”
这是我试过但行不通的方法:
tell application "System Events"
click checkbox "Enable Voice Control" of window "Voice Control" of window "Accessibility" of application process "System Preferences" of application "System Events"
end tell
您在评论中询问是否有其他方法可以获得解决方案。这是一种修改代码的方法。首先,我将逐步解释解决方案。
Update:正如 user3439894 在下面非常有用的评论中指出的那样,这个 if 语句实际上是必要的,原因有几个。它应该始终包含在内。
if running of application "System Preferences" then
quit application "System Preferences"
delay 1
end if
- 这样做是为了使脚本在 运行 时不会失败。注意,网友还指出应该使用killall来保证进程完全终止。
- 它允许应用程序行为的可预测性。
user3439894 还指出我应该添加这个代码块,以防出现时间问题,导致应用程序在关闭过程中尝试重新打开。
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
接下来,在此代码段中,您单击“系统偏好设置”中的“辅助功能”按钮
tell application "System Events"
click button "Accessibility" of scroll area 1 of window 1 of process "System Preferences" -- click the Accessibility button
delay 2 -- while loads
接下来,您 select 设置中的语音控制行。
select row 13 of table 1 of scroll area 1 of window 1 of process "System Preferences"
delay 0.2 -- delay while page loads
最后,单击复选框。正如我在评论中解释的那样,组用于组织元素。
click checkbox "Enable Voice Control" of group 1 of window 1 of process "System Preferences"
end tell
完整(更新)代码:
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
delay 0.1
end if
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
activate application "System Preferences"
tell application "System Events"
click button "Accessibility" of scroll area 1 of window 1 of process "System Preferences" -- click the Accessibility button
delay 2.0 -- delay while page loads
select row 13 of table 1 of scroll area 1 of window 1 of process "System Preferences"
delay 0.2 -- delay while page loads
click checkbox "Enable Voice Control" of group 1 of window 1 of process "System Preferences"
end tell
我对您的代码所做的更正:
- 您不能引用 windows 中尚未在应用程序中打开的元素(就像您对 window 的“启用语音控制”复选框所做的那样 window“辅助功能”),所以首先我打开辅助功能window(点击一个按钮)->语音控制Window(select行),然后found/clicked复选框.
- 然后,我查看了元素的层次结构,以确定 复选框 在语音控制 Window 中的位置。我发现它在 window 1 (语音控制 Window) 的第 1 组中,所以我点击了它。
- 我还添加了延迟(应用程序需要时间在每个 click/select 之后加载)。
此外,这是我找出元素位置的方法。
如果您还没有使用它,可以使用这个名为 Accessibility Inspector 的内置应用程序来帮助您定位元素。
你也可以在applescript中使用get。例如,
tell application "System Events" get buttons of scroll area 1 of window 1 of process "System Preferences" end tell
会return:
... button "Accessibility" of scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events", button "Screen Time" of scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events",...
另一个例子:
tell application "System Events" get UI elements of window 1 of process "System Preferences" end tell
这是一些输出
...scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events", toolbar 1 of window "System Preferences" of application process "System Preferences" of application "System Events..."
一个非常有用的工具。