UI SCRIPTING EXAMPLES

The following script examples demonstrate the use of UI scripting.

Selecting a Menu Item
A sub-routine for selecting a menu item in an application.


on do_menu(app_name, menu_name, menu_item)
try
-- bring the target application to the front
tell application app_name
activate
end tell
tell application "System Events"
tell process app_name
tell menu bar 1
tell menu bar item menu_name
tell menu menu_name
click menu item menu_item
end tell
end tell
end tell
end tell
end tell
return true
on error error_message
return false
end try
end do_menu




Selecting a Sub-menu Item
A sub-routine for selecting a sub-menu item in an application


on do_submenu(app_name, menu_name, menu_item, submenu_item)
try
-- bring the target application to the front
tell application app_name
activate
end tell
tell application "System Events"
tell process app_name
tell menu bar 1
tell menu bar item menu_name
tell menu menu_name
tell menu item menu_item
tell menu menu_item
click menu item submenu_item
end tell
end tell
end tell
end tell
end tell
end tell
end tell
return true
on error error_message
return false
end try
end do_submenu




Navigating the Choose Item Dialog
This example demonstrates how to script the Choose Item dialog by importing an image or folder of images into iPhoto.


display dialog "Import an image file or a folder of images:" buttons ¬
{"Cancel", "Folder", "Image"} default button 3
if the button returned of the result is "Folder" then
set this_item to choose folder
else
set this_item to choose file of type {"JPEG", "TIFF"}
end if
-- convert the file reference to UNIX style
set this_path to the POSIX path of this_item
tell application "iPhoto" to activate
tell application "System Events"
tell process "iPhoto"
-- open import dialog
keystroke "I" using {command down, shift down}
-- summon path input sheet
keystroke "/" using control down
delay 1
tell window "Import Photos"
tell sheet 1
set value of text field 1 to this_path
click button 1
end tell
click button 1
end tell
end tell
end tell



Next Page
TABLE OF CONTENTS

Introduction

Activating UI Scripting - Enabling the GUI Scripting architecture

UI Element Inspector - Discovering an object's heirarchy

Example Scripts - Example scripts and routines