PDA

View Full Version : [Resolved] AppleScript help




Habbfreaky
Jul 13, 2013, 07:50 AM
Hello,

This is my first post on the MacRumors forum, and my first attempt with AppleScript.

I'd like to change the scrolling direction of my mouse wheel, with the help of an AppleScript. Since it isn't a MagicMouse, the checkbox in System Preferences is labeled "Scroll Direction: natural". I want to check or uncheck that checkbox with the AppleScript.

What I have now, is the following. I wrote this after checking several other posts on this forum, but I could not find exactly my problem.


tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.mouse"
end tell

tell application "System Events"
tell application process "System Preferences"
tell tab group 1 of window "Mouse"
click checkbox "Scroll Direction: natural"
end tell
end tell
end tell


I'd love to use it, but it gives the error that window \"Mouse"\ cannot be accessed.

Can anyone help me out?
Thank you.

Problem solved: the correct code is (thanks HexMonkey):


tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.mouse"
end tell

tell application "System Events"
tell application process "System Preferences"
click checkbox "Scroll direction: natural" of window "Mouse"
end tell
end tell


For other languages than English, the name of the checkbox and window after that, should be changed to as in System Preferences.



HexMonkey
Jul 13, 2013, 08:07 AM
There are no tab groups in the Mouse preference pane, which is why it gives the error. You can instead access the checkbox directly (as it is not nested inside some other view), ie using the following for the second part of the script:

tell application "System Events"
tell application process "System Preferences"
click checkbox "Scroll direction: natural" of window "Mouse"
end tell
end tell

Habbfreaky
Jul 13, 2013, 08:38 AM
There are no tab groups in the Mouse preference pane, which is why it gives the error. You can instead access the checkbox directly (as it is not nested inside some other view), ie using the following for the second part of the script:

tell application "System Events"
tell application process "System Preferences"
click checkbox "Scroll direction: natural" of window "Mouse"
end tell
end tell

When I use that, it gives me the error:
System Events got an error: Can’t get window "Mouse" of application process "System Preferences".

This is my code with your change:
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.mouse"
end tell

tell application "System Events"
tell application process "System Preferences"
click checkbox "Scroll direction: natural" of window "Mouse"
end tell
end tell


I don't know why that's coming up as an error.


EDIT: My default system language is not English, as I am not English. When I changed that, it worked perfectly. But, what should I change to make it work in my language?

HexMonkey
Jul 13, 2013, 08:43 AM
Hmm, it works for me (on 10.8). I know years ago I had some issues with that kind of script when one command would be executed before another one completed, which could explain it - for example, if it hadn't finished loading the Mouse preference pane before it tried to modify its content.

Does it give the error if you run the script when the Mouse pane is already open? What about when it's not open but you put "delay 1" before the "click checkbox" command?

Edit: Just saw your edit. Change the window title in the "click checkbox" line to whatever the title of the window is when the Mouse preference pane is active. You might also need to change the checkbox title to the text displayed in your language in the preference pane (I'm not sure if it's localised or not for accessing it via AppleScript).

Habbfreaky
Jul 13, 2013, 08:45 AM
Hmm, it works for me (on 10.8). I know years ago I had some issues with that kind of script when one command would be executed before another one completed, which could explain it - for example, if it hadn't finished loading the Mouse preference pane before it tried to modify its content.

Does it give the error if you run the script when the Mouse pane is already open? What about when it's not open but you put "delay 1" before the "click checkbox" command?

I figured it out. There wasn't any script running, the difference in language was the problem. I changed the name of the checkbox and window, and it works. It is in fact localised, yes. Thank you!

(Problem solved, for any references, I updated the first post with the solution.)