How to map extra mouse buttons in only some apps

Dec 23, '05 05:12:00AM

Contributed by: mtimmsj

I use a multi-button mouse, and I wanted to put some of the exta buttons to good use. My initial thought was to assgn the middle mouse button to Exposé's All Windows functionality (usually assigned to the F9 key) in the Dashboard & Exposé System Preferences panel. However, I use Apple's X11, Terminal, and several other applications that have good uses for the middle mouse button.

So what I really wanted was a way to assign the middle mouse button to F9 on most applications and leave it the default on some others. ControllerMate allows you to assign a rule to specific applications or all applications, but they don't have the built-in functionality to exclude applications from a specific rule.

My solution to this minor dilemma was to create a ControllerMate rule that calls an AppleScript that returns a value of 1 or 0. If the script returns a 1, then the default use of the middle mouse button should be allowed (i.e. don't send F9). If the script returns a 0, then F9 should be sent.

The script is as follows:

on run
  tell application "System Events"
    set myApp to name of first application process ¬
     where frontmost is true
    --- Uncomment the next two lines to see the name of the frontmost 
    --- application; it is written to the system log.
    ---do shell script ¬
    ---"logger -t clickscript "Application was '" & myApp & "'.""
    if myApp is "x11" or myApp is "Terminal" then
      return 1
    end if
    return 0
  end tell
end run
The ControllerMate rule contains the following building blocks:

These are tied together as seen in the above screenshot. So when the mouse button is clicked, its building block transitions to ON, and the script is run. If the script returns 0, the value selector turns ON. If the button is still pressed (since the script can run faster than one can let go of the button, the button will most certainly still be pressed), the And building block turns ON and causes the F9 keystroke to be sent.

If the script returns 1, the Value Select building block doesn't turn ON, and the F9 keystroke is not sent, instead the default use of the middle mouse button takes place for that application.

Here is a little more complex example I did that uses the same technique to assign the Exposé functionality to display the desktop (F11) to a mouse button for most applications, but then for my browsers, it assigns this button to do the key sequence to go back to the previous page (command-[).


The script is nearly identical to the one above, but it returns 1 if the frontmost application is one of my browsers.

Comments (3)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20051220161337426