GUI Scripting the Sharing System Preference

I’m looking for a way to toggle the Screen Sharing check box on or off in the Sharing System Preference. GUI scripting System Preferences has never been my expertise. Can anyone help get me started? Thanks.

Browser: Safari 533.18.5
Operating System: Mac OS X (10.6)

This is what I have so far:


tell application "System Preferences"
	activate
	set current pane to pane "Sharing"
end tell

tell application "System Events"
	click checkbox 1 of row 2 of table 1 of scroll area 1 of group 1 of window "Sharing" of application process "System Preferences"
end tell

Anyone know if you can get the value of a check box as either on or off before you click it?

Hi this wiil return 0 (off) or 1 (on), allowing you to turn the pref on or off

tell application "System Preferences"
    activate
    set current pane to pane "Sharing"
end tell

tell application "System Events"
    get value of checkbox 1 of row 2 of table 1 of scroll area 1 of group 1 of window "Sharing" of application process "System Preferences"
    if result is 0 then --turn on the pref
        click checkbox 1 of row 2 of table 1 of scroll area 1 of group 1 of window "Sharing" of application process "System Preferences"
    else if result is 1 then --turn off the pref
        click checkbox 1 of row 2 of table 1 of scroll area 1 of group 1 of window "Sharing" of application process "System Preferences"
    end if
end tell

Hi,

try this


property on_ : 1
property off_ : 0

switchScreenSharing(on_)

on switchScreenSharing(val)
	tell application "System Preferences"
		activate
		reveal anchor "Main" of pane id "com.apple.preferences.sharing"
	end tell
	tell application "System Events"
		tell process "System Preferences"
			tell table 1 of scroll area 1 of group 1 of window 1
				tell (1st row whose value of static text 1 is "Screen Sharing")
					if value of checkbox 1 is not val then
						click checkbox 1
					end if
				end tell
			end tell
		end tell
	end tell
end switchScreenSharing

Thanks! This is a tremendous help.