Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'm running Mac OS X 10.7.2. Is there a way to run an AppleScript or terminal command that quickly turns on or off the "mouse keys" feature under the Universal Access tab? Ideally I'm looking for a solution that doesn't involve GUI scripting the System Preferences app, although that would be slightly better than nothing.

I've explored defaults write com.apple.universalaccess useMouseKeysShortcutKeys 1 and various other forms of that statement, but to no avail.

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Here's the GUI scripting solution:

tell application "System Preferences"
    reveal anchor "Mouse" of pane id "com.apple.preference.universalaccess"
    tell application "System Events"
        tell process "System Preferences"
            tell first window
                tell first tab group
                    tell radio group 1
                        if value of radio button 1 is 1 then
                            # enabled, so disable
                            click radio button 2
                        else
                            # disabled, so enable
                            click radio button 1
                        end if
                    end tell
                end tell
            end tell
        end tell
    end tell
    quit
end tell

It will launch System Preferences if required, but doesn't show the window and quits it afterwards, so from a UI POV, it at least doesn't show the window.


You can change the persistent setting on which the System Preferences configuration is based using the following commands:

defaults write ~/Library/Preferences/com.apple.universalaccess mouseDriver -bool YES
defaults write ~/Library/Preferences/com.apple.universalaccess mouseDriver -bool NO

Unfortunately, this only changes the configuration file and does not apply the changes to the running system.


While you can programmatically press Option keys using AppleScript, like the following:

tell application "System Events"
    key down option
    key up option
end tell

The press Option five times to toggle setting is at a higher level and doesn't get triggered by this. While all regular Option key presses are ignored after you only run key down option, you can still toggle Mouse Keys.


Debugging System Preferences while changing the setting shows calls to LaunchUAServerIfNeeded and UAMouseKeysSetEnabled in the private UniversalAccessCore framework. Unless we reverse engineer that framework and write our own binaries, I don't think we'll get a proper programmatic way to do this.

share|improve this answer
    
A phenomenal answer that only got better with the edit. Well done! –  Daniel Lawson Feb 10 '12 at 23:10
    
how do you find out what the anchors are called? I guessed 'Keyboard' and 'Hearing' but I can't seem to guess what the anchor name for the Seeing tab should be. It's not Seeing, Sight, Vision.... What I'm really curious about is how to find them, without guessing. –  user54823 Aug 9 '13 at 22:02
add comment

Can't you use keystroke scripting in AppleScript for this?

Something along the lines of:

tell application "System Events"
    repeat 5 times
    //any of these should work
    key option
    key code 58
    key code option
    //I do not know if this works
    keystroke option
end tell
share|improve this answer
    
No, sadly, as far as I can tell, option is a modifier, not a scriptable keystroke. –  Daniel Lawson Feb 10 '12 at 22:31
    
@Daniel The commands would be key down option and key up option, but don't work, even with delay 0.1 between each. –  Daniel Beck Feb 10 '12 at 22:39
    
@Daniel Did you try it? –  daviesgeek Feb 10 '12 at 22:40
    
I did. Didn't trigger the switch when run. Be careful to never mismatch a pair of these when you try. AFAIK it's impossible to release a key pressed this way unless with the correct key up. –  Daniel Beck Feb 10 '12 at 22:45
    
Yes I tried and no, it doesn't work. :-( –  Daniel Lawson Feb 10 '12 at 22:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.