commandapplescriptconditional-statementsdictation

If check box is already selected, then quit application


I created a script to be activated by using the dictation command "Enable Sharing" which successfully opens up System preferences/ Sharing/ then it will click the checkbox automatically whether it is enabled or disabled already.

I would like to have two dictation commands, one for enable sharing and one for disable sharing. How can I add a conditional that when I say "Enable Sharing" it will run through the script and if the sharing checkbox is already selected, just quit system preferences instead of clicking that checkbox if it is already selected which actually would deselect that checkbox?

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

Solution

  • Eureka!!! After trying all sorts of different combinations.. This is the script that actually worked as I needed it to..

    activate application "System Preferences"
    delay 1
    tell application "System Events"
        tell process "System Preferences"
            click button "Sharing" of scroll area 1 of window "System Preferences"
        end tell
    end tell
    activate application "System Preferences"
    delay 1
    tell application "System Events"
        tell process "System Preferences"
            tell checkbox 1 of row 2 of table 1 of scroll area 1 of group 1 of window "Sharing" to if value is 0 then click
            delay 1
        end tell
    end tell
    tell application "System Preferences"
         quit
    end tell