AppleScript GUI scripting problem.

Discussion in 'Mac Programming' started by mduser63, Oct 13, 2005.

  1. macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #1
    I'm trying to create a script that will automatically change the mouse tracking speed for me, as I switch between mice (Mighty Mouse and Logitech Notebook wireless mouse) every day, and it's annoying to change the tracking speed manually every time I switch mice. Anyway, I've been wanting to learn how to use GUI scripting in AppleScript and figured this would be a good chance to do that. I've run into a problem. I've got the following code so far:

    Code:
    tell application "Finder"
    	open application file "System Preferences" of folder "Applications" of startup disk
    end tell
    tell application "System Events"
    	tell process "System Preferences"
    		tell window "System Preferences"
    			tell scroll area 1
    				click button "Keyboard & Mouse"
    			end tell
    		end tell
    	end tell
    end tell
    System Preferences opens fine, but then I get the following error:
    Code:
    System Events got an error: NSReceiverEvaluationScriptError: 4
    and the line "click button "Keyboard & Mouse"" is highlighted. Can anybody tell what I'm doing wrong? I've got "access for assistive devices" checked in System Preferences->Universal Access.
     
  2. Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #2
    From memory, that method of opening a preference pane worked in earlier versions of Mac OS X, but doesn't anymore. Instead, you have to tell System Events to click the appropriate menu item, for example:

    Code:
    click menu item "Keyboard & Mouse" of menu "View" of menu bar 1
    Here's a complete script that should set the mouse tracking speed. Set the value of trackingValue to the speed you want.

    Code:
    (*Tracking value is the desired amount of tracking
    It should be an integer from 0 to 9*)
    set trackingValue to 5
    
    --Open and activate System Preferences
    tell application "System Preferences" to activate
    
    --Attempt to change settings using System Events
    tell application "System Events"
    	tell process "System Preferences"
    		try
    			--Open the "Keyboard & Mouse" pane
    			click menu item "Keyboard & Mouse" of menu "View" of menu bar 1
    			
    			--Now set the tracking value. The delays prevent actions being sent before previous ones have been completed
    			delay 2
    			tell tab group of window "Keyboard & Mouse"
    				click radio button "Mouse"
    				delay 2
    				set value of slider 1 to trackingValue
    			end tell
    		on error theError
    			--An error occured
    			display dialog ("Sorry, an error occured while altering Keyboard and Mouse settings:" & return & theError) buttons "OK" default button "OK"
    		end try
    	end tell
    end tell
     
  3. thread starter macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #3
    Thanks a lot HexMonkey, that works great!
     

Share This Page