Control System Prefs with AppleScript

I use TeamViewer to control the PC in the Hut, because it’s free, and it often works quite well.  The thing that drives me most nuts about it is how to right-click remotely.  I’m working from a Mac Book Pro with a trackpad.  I suppose I could actually use a two button mouse, but I haven’t used a mouse with a MBP forever.  I’m not even sure I have a two-button mouse… The solution is to go to the System Prefs Trackpad panel and set the Secondary Click checkbox for two-finger click.  trackpad-panel

That actually works great for TeamViewer remote sessions.  But it drives me nuts in MacLand, because I seem to use two hands on the trackpad and am constantly getting unwanted context menus.  So I wanted a handy way to turn on or off the two finger click trick programatically, which is to say, with AppleScript.

So how do you get your hooks into the Systems Preferences elements?

The key is to use System Events to get and set the window’s UI elements.  Here’s a simple script that collects the UI elements for the frontmost window of the application you want to peek at, in this case System Preferences:

tell application "System Preferences"
    --bring System Prefs to front
    activate
end tell

tell application "System Events"
    tell process "System Preferences"
        set windowsProperties to entire contents of front window
    end tell
end tell

return windowsProperties

The results window displays a lengthy list of elements that you’ll have to noodle over to figure out what you want.  In this case, I had opened System Preferences and clicked  to open the Trackpad panel, as in the image at top.  Here’s what the ScriptDebugger window looked like:  results of ui inspector

 

Here’s the pasted value (pasted into ScriptDebugger) from the results for “checkbox 2 of tab group 1 of window “Trackpad” of application process “System Preferences”:

tell application "System Events.app"
    tell application process "System Preferences"
        tell window "Trackpad"
            tell tab group 1
                tell checkbox 2
                    -- your code goes here
                end tell
            end tell
        end tell
    end tell
end tell

This nested tell set is clunky, but lets you easily understand exactly what’s going on, and you can just paste it into a script to get or set a value to a particular UI element.  I had to horse around to check or un-check the  secondary click checkbox.  “Normal” checkboxes have a value of “0” for unchecked and “1” for checked.  The script would let me get the value, “0” or “1”, but not let me set the value.  However, it did let me treat the checkbox like a button, and “click” it.  Go figure. That would be a problem if I really needed to set a particular value, but in this case, I’m happy to let it toggle:

--toggles "2 finger click" for secondary click on or off via trackpad pane of system Prefs
--needs "does trackpad panel exist?" 
--JJ McClintock 20130411

tell application "System Preferences"
    activate
end tell
tell application "System Events"
    tell application process "System Preferences"
        tell window "System Preferences"
            tell scroll area 1
                --hopefully this will catch issues if no trackpad panel exists
                try
                    click button "Trackpad"
                    tell application "System Events"
                        tell application process "System Preferences"
                            tell window "Trackpad"
                                tell tab group 1
                                    ----set value doesn't seem to work
                                    --set value of checkbox 2 to 1
                                    ----get works fine
                                    --get value of checkbox 2
                                    --click works as toggle
                                    click checkbox 2
                                end tell
                            end tell
                        end tell
                    end tell
                end try
            end tell
        end tell
    end tell
end tell
--we want to quit System Prefs when we're done
tell application "System Preferences"
    quit
end tell

I saved the “toggle 2 finger secondary click” script as an application bundle and placed it in my script menu folder. Now I can turn on 2 finger click when I begin a TeamViewer session and turn it off the same way when I’m done.

Leave a Reply