PDA

View Full Version : AppleScript GUI scripting problem.




mduser63
Oct 13, 2005, 06:38 PM
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:

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: 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.



HexMonkey
Oct 14, 2005, 12:49 AM
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:

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.

(*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

mduser63
Oct 14, 2005, 09:29 AM
Thanks a lot HexMonkey, that works great!