Section 12.6. Fake Typing


12.6. Fake Typing

Even though you turned on Sticky Keys in the last script, the script itself only impersonated mouse clicksclicking 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, tooand 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

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

You enable screen zooming by opening System Preferences Universal Access Seeing tab and clicking "Turn on Zoom."

Normally, you zoom in on the screen by pressing Option--+, and zoom out by holding Option--minus sign.

That process, however, is imprecise. Unless you can hold the keys down for exactly the same amount of time each time you press them, the magnification factor will differ each time you zoom in on your screen. You might be looking at an image of your kids' hair at 5 magnification one time you zoom in, and 15 the next.

That's where GUI Scripts can come in handy. Each time you run the following script, the screen zooms in and out a consistent amount, giving you an up-close view of your screen that's exactly the same magnification each time:

tell application "System Events"     --Part 1:     key down option     key down command     --Part 2:     key down "+"     delay 2     key up "+"     --Part 3:     delay 5     key down "-"     delay 2     key up "-"     --Part 4:     key up option     key up command end tell

Here's how the new commands work:

  • Part 1 holds down the and Option keys, a prerequisite to zooming in (-Option-+) or out (-Option-minus sign).

The key down command is an extension of the keystroke command. However, instead of simply typing the specified key, the key down command holds down the key you specifyat least until the script encounters a key up command.

  • Part 2 holds the + key down, thereby completing the -Option-+ keystrokeand zooming in on your screen. Two seconds later, the script releases the + key (using key up), stopping your screen from zooming in any further.

  • Part 3 pauses 5 seconds, letting you get a good view of your zoomed-in screen. Then the script zooms out, by holding -Option-minus sign for 2 seconds.

  • Part 4 lifts up the and Option keys, because your script is done pressing key combinations.

It is very important that every key down command be paired with a matching key up command later in your script. If you forget key up, the keys you specified will remain held downeven after your script is finished running!

That's all there is to it. Now, whenever you want to examine a blown-up version of your screen for a few seconds, simply run your script.

Frequently Asked Question
Typing Function Keys

I'm really digging the GUI-scripted typing feature, but it seems kind of limited. Is there any way I can automate typing function keys from my scripts?

Function keys, like many features in Mac OS X, offer shortcuts to various parts of your Mac world. For example, you can use the function keys (F1-F12 on most keyboards) to manage your windows with Exposé, or have function keys trigger special AppleScripts. No matter what you use function keys for, however, it would sure be useful if you could have GUI scripts press function keys for you, to save you the finger movement.

If you wanted to make a script press F9, your first instinct might be to use a command like this:

tell application "System Events"     key down "F9" end tell

As it turns out, though, that script would merely type the keys "F," then "9," in sequence. To type an actual function key, you have to know its key codeMac OS X's secret identification number for every key on your keyboard. The key code for F9 happens to be 101, so the proper script would look like this:

tell application "System Events"     key code 101 end tell

Of course, unless you're a Mac Genius, you probably don't know the key code for every function key on your Mac. Luckily, if you download a program like Full Key Codes (www.bajram.com/_downloads/?server=01&file=Full_Key_Codes.sit), you can discover the key code for just about any key on your Macfunction key or otherwise. Just keep in mind that you want the decimal key code (not the hex key code) for use in your GUI scripts.

For more addiction-satisfying GUI scripts, examine Script Menu UI Element Scripts. Or visit www.apple.com/applescript/uiscripting/03.html for a series of helpful GUI-controlling subroutines and extra scripts.




AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net