Applescript, so close and yet so far

[2 February 2008]

There are lots of big things on my mind lately: papers due and overdue and long overdue, submissions deadlines coming up, and a long long list of things to fix in the XSD 1.1 spec.

But there are some little things that refuse to stop taking up time and energy.

Years ago, tired of the hassles of trying to synchronize desktop and laptop, I followed the example of my friend Willard McCarty and started using my laptop as my only machine. This has worked pretty well on the whole, though it has saddled me with heavier laptops than some of my friends carry and given me less disk space than I could have had on desktop machines bought for the same price.

But a key part of making this work is having an external keyboard to use at my desk. I use a wave-shaped keyboard from Logitech at my desk, and to make things work as I expect, I use the Mac System Preferences interface to switch the Option and Command keys when I’m using the external keyboard.

Unfortunately, when I’m using the Powerbook’s own keyboard, this system preference must be undone. And then when I return to my desk, I have to switch the keys again.

Changing the relevant keyboard settings takes seven or eight mouse clicks. That gets old. I’d like to automate it; can Applescript help? Yes, it can: the samples include at least one example of scripting a change to the system preferences.

So I spent some time the other day trying to script my task: one script to launch System Preferences, choose Keyboard and Mouse, choose Modifier Keys, switch Command and Option, choose OK, and quit; another to go the other way.

The documentation makes fairly clear that I need to know the names for buttons and subpanes and so on provided by the application, so I can tell Applescript which things to activate. But I seem to be missing a step; I can’t find anything that tells me what names System Preferences gives to its panes. There’s an Open Dictionary option in the Script editor, but the dictionary for System Preferences only tells me that it defines things called panes. It doesn’t tell me — or am I just missing something here? — what IDs those panes have, or how to find out.

At the moment, this task is out of time and is going back to the bottom of the to-do list. But every time I take my machine away from my desk, or bring it back, I’m reminded that I haven’t solved this one yet.

5 thoughts on “Applescript, so close and yet so far

  1. Thank you, Karl. That script should be helpful.

    I do still have two problems. First, the script doesn’t seem to work on my system; it fails with the enlightening error message “System Events got an error: NSReceiverEvaluationScriptError: 4”. But I can see that it does at least manage to open the System Preferences window, and I may be able to figure out what’s going wrong my looking carefully at the script and at the windows it’s trying to manipulate. Unfortunately, the evidence appears to be that it’s failing when trying to select a particular UI widget. If it’s using the wrong name, then I’ll need to find out what name the system is expecting.

    Which brings me (back) to my other problem: how to find out what names an application assigns to objects in its abstract model or in its user interface. (It’s possible that the dictionary for System Preferences doesn’t have the names I’m expecting because System Preferences is not written to be scriptable; that would explain why the script you point to seems to be manipulating the UI instead of calling directly for a particular action. But … System Preferences not scriptable? Seems implausible.

    To find out, I guess I’m going to have to learn more Applescript, more systematically. Which is why (sigh) this project won’t happen soon.

  2. An update on this topic. I did find some time, in an airport somewhere, to look carefully at the script Karl found on the Web. The problem it was having turned out to be a character-set problem, and once that was fixed it worked as advertised. Thank you, Karl!

    Of course, having a working script reduces the priority for learning Applescript, which in a way is a kind of shame. But moving my machine away from my desk is now less painful than before.

  3. >>>I can’t find anything that tells me what names System Preferences gives to its panes.

    Have you found anything about naming Pref Panes?
    I think it is much more slower to use UI scripting, than changing preference using pref. pane parameter scripting.

    Please, answer to my mail. Cause there is no possibility to subscribe to your comment 🙁

  4. Hey, Leopard has this option built in to System Prefs -> Modifier keys. You can set them for each different keyboard you use e.g. switch alt/cmd for external pc keyboard, but not for laptop

Comments are closed.