O'Reilly logo

AppleScript: The Missing Manual by Adam Goldstein

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Fake Typing

Even though you turned on Sticky Keys in the last script, the script itself only impersonated mouse clicks—clicking a menu item and then clicking a few radio buttons. As you know, however, a mouse is only half of your computer's input.

The other half, of course, is the keyboard. If you want to do any serious work with GUI Scripting, you have to emulate keystrokes as well. Thanks to the keystroke command, this task is quite easy:

tell application "System Events"
    keystroke "Q"
end tell

When you run that script, you should notice the letter Q magically typed into your Script Editor window. Behind the scenes, your script convinces Mac OS X that you just typed the letter Q on your keyboard.

Of course, you probably want to type keystrokes in other programs, too—and thankfully, that's just as convenient:

               tell application "TextEdit"
    activate
               end tell
               --Now the keystroke "Q" will appear in TextEdit
tell application "System Events"
    keystroke "Q"
end tell

You can even specify more than one key to press, in sequence:

tell application "TextEdit"
    activate
end tell
tell application "System Events"
    keystroke "Quack" --This types "Q", "u", "a", "c", "k"
end tell

Zooming In and Out

In the script on Section 12.5, you turned on Mac OS X's Sticky Keys feature. Even more useful to everyday Mac users, though, is Mac OS X's screen zooming feature, which lets you focus in on a small section of your screen for detailed graphics work.

Note

You enable screen zooming by opening System Preferences Universal ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required