blog advertising is good for you


blog advertising is good for you
User login

GUI Automation with AppleScript

System Events, introduced with Mac OS X 10.3, is what assistive devices use in Mac OS X. It also allows AppleScript to control GUI applications directly through UI Scripting.

With the release of Mac OS X 10.4, Apple includes the ability to adjust the behavior of the modifier keys on the keyboard in the Keyboard & Mouse system preferences. This becomes useful if you use a PC-style keyboard with your Mac, like I do with my iBook.

We’re combining the two to do something useful for switching between a PC style keyboard and a Mac style keyboard.

The reason this is useful on the PC-style keyboards is that the placement of the equivalent of the Command and Option keys (Windows and Alt, respectively) have their placement switched. It can be quite annoying to have to change your keystrokes on a frequent basis or having to remember to go into System Preferences and change the keyboard behavior.

Enter AppleScript, and System Events. In this case, I am using UI Scripting to automate the changing of the Keyboard & Mouse System Preferences. (Yes, I could hunt around for a while and determine which property list file and which settings to manipulate, but this was more fun for now.)

To get started with UI Scripting, you can review Apple’s documentation on the subject.

Be sure to download the UI Element Inspector, as this will allow you to see GUI elements. There are a few gotchas and pitfalls, as pwith any scripting language, but these seem more based on how the developer decided to code their UI.

Additionally, you will need to activate the UI Scripting ability. The instructions are also linked from the GUI Scripting page given above.

Play around with the UI Element inspector, as this will be your primary tool in discovering how to automate your applications.

There are a couple of things to note - menus brought up by Pop Up buttons are not accessible in your AppleScript until you click the button, so you must click (via AppleScript) the pop up button, then click the item you want.

Here is the AppleScript, it will only work with 10.4 because of the Keyboard & Mouse preferences. But the UI Scripting concepts can be used with any 10.3 or higher system. Paste this into Script Editor make sure it compiles before saving this as an application or application bundle. The only problems will likely be formatting, so make sure each “click” line is on a single, non-wrapped line.

-- UI Scripting Demonstration
--
-- Modify Keyboard System Preferences to swap the Option (Alt) and Command (Windows) keys
-- so their placement on the keyboard matches that of the standard Macintosh keyboard. This
-- will also allow us to change the preferences back so the keys go back to normal
-- when the PC keyboard is disconnected.

on setKeyboard(keyboardType)
  tell application "System Events"
    get properties
    tell process "System Preferences"
      -- click "Modifier Keys..." at bottom of Keyboard tab
      click button "Modifier Keys…" of tab group 1 of window "Keyboard & Mouse"

      if keyboardType is equal to "Macintosh" then
        -- click "Restore Defaults" on the sheet
        click button "Restore Defaults" of sheet 1 of window "Keyboard & Mouse"

      else if keyboardType is equal to "PC" then
         -- click the pop up button menu "Option", this menu does not exist until it is clicked in the GUI
         click pop up button 3 of sheet 1 of window "Keyboard & Mouse"
         -- click "Command" of the pop up menu
         click menu item 4 of menu 1 of pop up button 3 of sheet 1 of window "Keyboard & Mouse"

         -- delay briefly
         delay 1

        -- click the pop up button menu "Command", this menu does not exist until it is clicked in the GUI
        click pop up button 4 of sheet 1 of window "Keyboard & Mouse"
        -- click "Option" of the pop up menu
        click menu item 3 of menu 1 of pop up button 4 of sheet 1 of window "Keyboard & Mouse"
      end if

      -- click "OK" to dismiss the sheet
      click button "OK" of sheet 1 of window "Keyboard & Mouse"
    end tell
  end tell
end setKeyboard

-- main script; preset desiredKeyboard to "Macintosh"
set desiredKeyboard to "Macintosh"

-- get users input; we're waiting until selection
display dialog "Which keyboard type?"
  buttons {"Macintosh", "PC"}
  default button "Macintosh"

-- set desiredKeyboard to "PC" if they picked "PC"
if button returned of result = "PC" then
  set desiredKeyboard to "PC"
end if

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

-- call subroutine with desired keyboard type
setKeyboard(desiredKeyboard)

-- quit from system preferences
tell application "System Preferences"
  quit
end tell

Edited: 2005-04-30 11:30 CDT for clarification

Average rating
(1 vote)
About unixgeek

Brilliant Idea. I recently switched back to Mac after way too many years on PC and I’ve been driven crazy moving back and forth to my old PC Keyboard.

That said—and I will admit that while my scripting is getting better it ain’t great—I can’t seem to get your script to compile. The compiler is getting stuck on

buttons {“Macintosh”, “PC”} default button “Macintosh”

I spent half of last night scrambling through books with no luck. And I’ve decided that I’ve probably missed some check box deep in Prefs which is prohibiting me from finishing this.

Anyway, if you have any ideas I’d love to hear them. (and don’t feel obligated to spend a lot of time here. I will solve it eventually and my scripting will get better for the effort.)

Other facts: When I run the script I get as far as a dialog box reading “Which keyboard type?” with buttons “OK” and “Cancel”.

I am running a 12-inch 1.5GHz powerbook with OS X 10.4.

Thanks for the all of your efforts

unixgeek's picture

The compiler is getting stuck on those lines because they should be on all one line, but I goofed when typing them up on to the website. So, it should look like this:

display dialog “Which keyboard type?” buttons {“Macintosh”, “PC”} default button “Macintosh”

Hope this helps,


unxgeek@unxgeek.us
“Smile,” they said, “it could be worse.”
So I did, and it was.

That worked perfectly. I really appreciate the help.

You managed to solve with a simple script something that has been driving me crazy for a month. I am definitely inspired to learn more.

Steve

belt forklift seat belt forklift seat,cinema 1 pen centre cinema 1 pen centre,desert star cinema in lake deltonwi desert star cinema in lake deltonwi,reformation church rochester ny reformation church rochester ny,red sox showcase cinema red sox showcase cinema,forklifts specs worksheet forklifts specs worksheet,definition of negligence as used in victorian ohamp definition of negligence as used in victorian ohamp,bureau of criminal identification and investigation bureau of criminal identification and investigation,1950 chrysler engine identification 1950 chrysler engine identification,tort of negligence for not wearing a motorcycle helmet tort of negligence for not wearing a motorcycle helmet,

This script looks super-promising (I’ve been doing what the script does by hand for two weeks now). But, I can’t get it to work. I fixed the newline issue, but now, once I click on a keyboard type, I get

System Events got an error: NSReceiverEvaluationScriptError: 4

Anyone else seeing this?

Thanks!

I had the same problem, but the script worked just fine, when (for some other reasons) changed the system-language from German to English.

So I changed “Modifier Keys…” to “Sondertasten…” and “Keyboard & Mouse” to “Tastatur & Maus” throughout the whole script, and now it works flawlessly. If you are using a localized version of the OS, this might be solution for you, too?

Strangely enough it wasn’t necessary to change “System Preferences” and “System Events”. Does anybody know why? I think it’s strange, that this issue seems to be partly dependent on the localized names, while the rest of AppleScript isn’t…?

@unixgeek: Thanks a lot for this script. I didn’t know yet that uiscripting was possible and switching by hand was definitely annoying.

Got the exact same problem. Did japnhar’s advice help you? It didn’t do anything for me, as I’m already running my OS in English (though my Formats are set to Danish).

Any help would be appriciated, as I’m switching the key-layout several times every day.

Adam Knight's picture

Note the comment below about turning on Assistive Devices in Universal Access.

cp

Things are working flawlessly now – thanks a million!

/Steen

After copying and pasting the script, the “…” after “Modifier Keys” (in the highlighted offending line) you may notice is not an ellipsis.

After replacing the other characters with option+”;”, the script worked for me.

Great script!

unixgeek's picture

UI Scripting does not work unless you enable assistive devices in the Universal Access System Preferences. See this link for instructions, if you need them.

– unxgeek@unxgeek.us “Smile,” they said, “it could be worse.” So I did, and it was.

I made minor alterations to the script provided, to create two versions:

version a) always switches keyboard to Macintosh
version b) always switches keyboard to PC

I then used Script Editor to compile each script version as a seperate application (“Set Keyboard to Macintosh.app” and “Set Keyboard to PC.app”).

I then used Peripheral Vision’s automation feature to automatically run “Set Keyboard to PC.app” when the Dell USB keyboard I use at work is plugged into my iBook.

When the keyboard is unplugged, Peripheral Vision automatically runs the “Set Keyboard to Macintosh.app” program.

Awesome. Thanks to unixgeek for posting the original script.

Peripheral Vision is a great application and can be found here:

http://www.grantedsw.com/p-vision/

I am in no way affiliated with Peripheral Vision, other than being a happy user.

Cheers,

ct77

If you have an older PowerBook or iBook, then you have an ADB built-in keyboard. In that case, here’s a way of setting it up so that both the external and built-in keyboards have the command and option keys correctly arranged.

I would like to distribute a modified copy of this script under a GPL license. I would like to make sure this is OK with you.

Thanks!

Adam Knight's picture

Most users have a contact page, as does the author of this piece.

Copy-and-paste friendy version of the code is available here:

http://pastie.caboo.se/89067

Thanks for the code UnixGeek

Hi

1.How do I know if a third party app on mac is scriptable through AppleScripts?
2.Also how can I do dialog handling for the third party applications?

Thanks in advance
Sarthak

Post new comment
The content of this field is kept private and will not be shown publicly.
6 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.