通过AppleScript设置屏幕共享密码

通过AppleScript设置屏幕共享密码,applescript,screensharing,Applescript,Screensharing,我想使用apple脚本设置屏幕共享密码,并选中“VNC查看器可以使用密码控制屏幕”选项。我是apple script的新手,我所做的只是在系统首选项的共享窗格下选中“屏幕共享”选项 以下是我目前掌握的情况: tell application "System Preferences" set current pane to pane "com.apple.preferences.sharing" end tell tell application "System Events" t

我想使用apple脚本设置屏幕共享密码,并选中“VNC查看器可以使用密码控制屏幕”选项。我是apple script的新手,我所做的只是在系统首选项的共享窗格下选中“屏幕共享”选项

以下是我目前掌握的情况:

tell application "System Preferences" set current pane to pane "com.apple.preferences.sharing" end tell tell application "System Events" tell process "System Preferences" tell checkbox 1 of row 1 of table 1 of scroll area 1 of group 1 of window "Sharing" to if value is 0 then click end tell tell process "System Preferences" click button 1 of group 1 of window "Sharing" delay 1 set value of text field 1 to "p" end tell end tell
这似乎对我有用:

tell application "System Preferences" set current pane to pane "com.apple.preferences.sharing" end tell tell application "System Events" tell process "System Preferences" tell checkbox 1 of row 1 of table 1 of scroll area 1 of group 1 of window "Sharing" to if value is 0 then click end tell tell process "System Preferences" delay 1 click button 1 of group 1 of window "Sharing" tell sheet 1 of window "Sharing" tell checkbox "VNC viewers may control screen with password:" to if value is 0 then click delay 1 set value of text field 1 to "p" click button "ok" end tell end tell end tell 告诉应用程序“系统首选项” 将当前窗格设置为窗格“com.apple.preferences.sharing” 结束语 告诉应用程序“系统事件” 告诉流程“系统首选项” 告诉窗口“共享”组1的滚动区域1表1第1行的复选框1,如果值为0,则单击 结束语 告诉流程“系统首选项” 延迟1 单击“共享”窗口第1组的按钮1 告诉窗口“共享”的第1页 告诉复选框“VNC查看器可以使用密码控制屏幕:”如果值为0,则单击 延迟1 将文本字段1的值设置为“p” 单击“确定”按钮 结束语 结束语 结束语 具体而言,文本字段(以及“VNC查看器…”复选框和“确定”按钮”是窗口“共享”的
告知表1的成员,因此需要适当调整
告知

如果您不了解这些工具,可以使用一些工具来简化这类工作:

  • Accessibility Inspector
    是OSX中捆绑的一个小实用程序(或者它在xcode中-不确定)。它提供了有关鼠标当前所在窗口中任何UI元素的大量信息
  • 的全部内容
    允许您在使用Applescript编辑器时转储给定范围内的所有UI元素。例如,如果我在此脚本的适当位置插入了
    获取窗口“共享”的全部内容
    ,它将列出窗口“共享”的所有UI元素“在Applescript编辑器的事件面板中

  • 请向我们展示您目前拥有的内容以及您的位置这是我目前拥有的@mcgraim:tell application“System Preferences”将当前窗格设置为窗格“com.apple.Preferences.sharing”end tell tell application“System Events”tell process“System Preferences”tell checkbox 1,位于窗口组1滚动区域1的表1的第1行“共享”到如果值为0,则单击结束告知流程“系统首选项”单击窗口组1的按钮1“共享”延迟1将文本字段1的值设置为“p”结束告知结束告知,但上面的代码将提示我一个错误:无法获取流程“系统首选项”的文本字段1“.index无效。嗯,你能把它放在你的博文中吗?它起作用了。但是我如何自动输入密码`因为系统首选项正在询问我的mac登录用户的密码?@hnmpk-我不完全确定。发生这种情况是因为
    系统首选项
    对话框被“单击锁以进行更改”?在我的情况下,如果此操作被锁定,则脚本将完全失败,无法获取窗口“共享”的第1页“..
    。但是我解锁它,然后运行脚本,它运行时没有问题。我想我没有解释清楚。一旦设置了新的VNC密码,它将提示我此
    系统首选项要进行更改。键入您的密码以允许此操作。
    我也希望它自动运行。这可能吗? tell application "System Preferences" set current pane to pane "com.apple.preferences.sharing" end tell tell application "System Events" tell process "System Preferences" tell checkbox 1 of row 1 of table 1 of scroll area 1 of group 1 of window "Sharing" to if value is 0 then click end tell tell process "System Preferences" delay 1 click button 1 of group 1 of window "Sharing" tell sheet 1 of window "Sharing" tell checkbox "VNC viewers may control screen with password:" to if value is 0 then click delay 1 set value of text field 1 to "p" click button "ok" end tell end tell end tell