通过AppleScript设置屏幕共享密码

时间:2013-10-31 09:11:08

标签: applescript screensharing

我想使用Apple脚本设置屏幕共享密码,然后选中“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"
        click button 1 of group 1 of window "Sharing"
        delay 1
        set value of text field 1 to "p"
    end tell
end tell

但上面的代码会提示我一个错误:

Can’t get text field 1 of process "System Preferences". Invalid index

1 个答案:

答案 0 :(得分:0)

这似乎对我有用:

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

具体而言,文本字段(以及“VNC查看器...”复选框和“确定”按钮“是tell sheet 1 of window "Sharing"的成员,因此tell需要进行适当调整。

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

  1. Accessibility Inspector是一个捆绑在OSX中的小工具(或者可能是xcode中的 - 不确定)。它提供了有关鼠标当前所在窗口中任何UI元素的大量信息。
  2. entire contents of允许您在使用Applescript Editor时转储给定范围内的所有UI元素。例如,如果我在此脚本的适当位置插入get entire contents of window "Sharing",它将在Applescript编辑器的事件面板中列出窗口“共享”的所有UI元素。