My script sets the Natural Scroll Direction of either a Mouse or Trackpad (MB trackpad or Magic Trackpad), but instead of allowing the user to choose which one is attached, I'd like to be able to programmatically execute one of the two functions based on what hardware is attached.
Is there a way to check (ie. AS or Shell Script) what hardware is attached?
For example, this is what I want to accomplish:
If: Trackpad is attached then run Trackpad function.
Else if: Mouse is attached, run Mouse function.
Else if: Mouse and
Trackpad are attached, run both
Else: throw error, no input devices
detected
I am after a robust function but have no idea where to start.
Mouse Function:
tell application "System Preferences"
reveal anchor "mouseTab" of pane id "com.apple.preference.mouse"
end tell
tell application "System Events" to tell process "System Preferences"
tell checkbox 1 of window 1 to if value is 1 then click
end tell
quit application "System Preferences"
Trackpad Function:
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.trackpad"
end tell
tell application "System Events"
tell application process "System Preferences" to tell tab group 1 of window "Trackpad"
click radio button 2
if value of checkbox 1 is 1 then
click checkbox 1
end if
end tell
end tell
quit application "System Preferences"
Thank you in advance, any help is greatly appreciated.
Best Answer
Okay, first I misread your question so I've rewritten my answer and second because the "Scroll direction: natural" preference setting is global, I've rewritten the code as well.
As coded there are two subroutines, one each for the mouse and the trackpad (internal or external) that's called based on the existence of the defined devices. That's defined as in the value for
mouse
,externalTrackpad
andinternalTrackpad
as returned from thedo shell script
commands configured to look for Apple Magic Mouse, Apple Wireless Trackpad and an Internal Trackpad. Theses commands can be changed as needed/wanted.Basically what happens is the variables are initialized and then tested by the
if
statements and acted upon accordingly as coded.If a mouse and external trackpad doesn't exist and an internal trackpad doesn't exist then a message is displayed and the script terminates.
If a mouse and external trackpad doesn't exist and an internal trackpad does exist, then the
TrackpadIsAttached
subroutine runs, the "Scroll direction: natural" checkbox is unchecked (if checked) and then the script terminates as the setting is global and there is no need to process further.If either a mouse or trackpad (internal or external) exists then either subroutine runs as appropriately coded, the "Scroll direction: natural" checkbox is unchecked (if checked) and then the script terminates as the setting is global and there is no need to process further.
Obviously I used my Apple Magic Mouse and Apple Wireless Trackpad as examples and tested against to ensure it worked as coded on my MacBook Pro. You can use whatever manufacture, make, model for an external mouse and trackpad and you'll just need to edit the script appropriately. If you need additional help with that just ask. Also I tested this under OS X 10.8.5 and it worked for me as written.