3

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.

1

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 and internalTrackpad as returned from the do 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.

on MouseIsAttached()
    tell application "System Preferences"
        activate
        set current pane to pane "com.apple.preference.mouse"
    end tell
    tell application "System Events" to tell process "System Preferences"
        tell radio button 1 of tab group 1 of window 1 to if value is 0 then click
        tell checkbox 1 of tab group 1 of window 1 to if value is 1 then click
    end tell
    tell application "System Preferences" to quit
end MouseIsAttached

on TrackpadIsAttached()
    tell application "System Preferences"
        activate
        set current pane to pane "com.apple.preference.trackpad"
    end tell
    tell application "System Events" to tell application process "System Preferences"
        tell radio button 2 of tab group 1 of window 1 to if value is 0 then click
        tell checkbox 1 of tab group 1 of window 1 to if value is 1 then click
    end tell
    tell application "System Preferences" to quit
end TrackpadIsAttached

tell current application
    set mouse to (do shell script "system_profiler SPBluetoothDataType | awk '{ FS = \": \" } ; /Apple Magic Mouse/ { print $2 }'")
    set externalTrackpad to (do shell script "system_profiler SPBluetoothDataType | awk '{ FS = \": \" } ; /Apple Wireless Trackpad/ { print $2 }'")
    set internalTrackpad to (do shell script "system_profiler SPUSBDataType | awk '/Trackpad:/ { print \"Internal Trackpad\" }'")

    if mouse is equal to "" and externalTrackpad is equal to "" then
        if internalTrackpad is equal to "Internal Trackpad" then
            my TrackpadIsAttached()
            return
        else
            display dialog "The Mouse and Trackpad are not connected." buttons {"OK"} default button 1
            return
        end if
    end if

    if mouse is equal to "Apple Magic Mouse" then
        my MouseIsAttached()
        return
    end if

    if externalTrackpad is equal to "Apple Wireless Trackpad" then
        my TrackpadIsAttached()
        return
    end if
end tell

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.

0

You really can't access HID devices (keyboards, mice, etc) without getting into actual software development.

However, as a "hack" you can query the system_profiler to see what's connected if you already know what you have connected.

For instance, to see if my Logitech mouse is connected, you can issue the command system_profiler SPUSBDataType | grep -i logitech

and it will output

Vendor ID: 0x046d  (Logitech Inc.)
Manufacturer: Logitech

So, to specify your specific device, you can modify the command to: system_profiler SPUSBDataType | grep -i 0x046d

So a simple bash script to check for the mouse being connected would be

#!/bin/bash

mouse="0x046d"
test=`system_profiler SPUSBDataType | grep $mouse | cut -d : -f 2 | cut -d "(" -f 1`

if [ $mouse == $test  ]
  then 
  echo "Mouse Connected"
else
  echo "No Mouse Connected"
fi

exit 0

You would do something similar for the trackpad

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .