I want to set the screen sharing password using apple script and check the option 'VNC viewers may control screen with password'. I'm new at apple script, and what I have accomplished is only checking the 'Screen Sharing' option under the Sharing pane of System Preferences.

Here's what I have so far:

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

But the code above will prompt me an error:

Can’t get text field 1 of process "System Preferences". Invalid index
有帮助吗?

解决方案

This seems to work for me:

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

Specifically the text field (and "VNC viewers..." checkbox and "OK" button" are members of tell sheet 1 of window "Sharing", so the tell needs to be adjusted appropriately.

In case you don't know them, a couple of tools which make this sort of thing easier:

  1. The Accessibility Inspector is a little utility bundled in OSX (or perhaps its in xcode - not sure). It gives a lot of information about whatever UI element in any window that your mouse is currently over.
  2. entire contents of allows you to dump all UI elements in a given scope when using the Applescript Editor. For example if I inserted get entire contents of window "Sharing" in the appropriate place in this script, it would list out all UI elements of the window "sharing" in the events panel of the Applescript editor.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top