13.10. GUI Scripting

GUI Scripting gives you the ability to script almost any application on the Mac. It does this by mimicking the actions of clicking the mouse, selecting menu items, and typing keystrokes. The next Try It Out gives you a quick look at how to use this technique for scripting applications.

13.10.1.

13.10.1.1. Try It Out: Introduction to GUI Scripting

This program shows how you can copy the text inside your TextEdit window to your application without knowing the specific AppleScript command to do this. You saw how to use the clipboard commands earlier in this chapter to accomplish this; these steps provide another approach.

  1. Select System Preferences from the Apple Menu.

  2. In the System Preferences window, click the icon labeled Universal Access.

  3. Check Enable Access for Assistive Devices. Enter your administrative password when prompted.

  4. Close the System Preferences window.

  5. Launch the TextEdit application.

  6. Type some lines of text into the TextEdit Window.

  7. Type the following program into Script Editor:

    -- Copy the text from TextEdit's window to the clipboard
    
    tell application "System Events"
       tell application process "TextEdit"
            set frontmost to true
            click menu item "Select All" of menu "Edit" of menu bar item "Edit" ¬
                    of menu bar 1
            click menu item "Copy" of menu "Edit" of menu bar item "Edit" of menu bar 1
        end tell
    end tell
    
    delay 1
    the clipboard
  8. Click the Result tab and run the program. The text from the web page you loaded should be displayed in the Result pane.

13.10.1.2. ...

Get Beginning AppleScript® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.