wch1zpink wch1zpink - 2 months ago 14
AppleScript Question

AppleScript - help adding conditionals to a functioning script. If check box is already selected, then quit application

I recently had a stroke and now I have a limited use of my hands, making it difficult using my mouse or trackpad and keyboard. Because of this I have been creating AppleScripts to be activated by dictation commands to be able to control pretty much anything I do on the computer. I am very new to this and I've only been writing AppleScripts for one week. I have been researching and reading help documents and practicing until all hours of the morning LOL but I could use a little help on this one. This is what I'm trying to accomplish.. I already 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.. What I would like to do is actually 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? Here is the script I am working with

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

Answer

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
Comments