ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

AppleScripts for Graphical User Interface (GUI)

Updated on October 20, 2016

If you have experienced the frustration of trying to write AppleScripts for an application that does not have AppleScript support, this is the place to find a way around that problem. In fact, there may be times when you''ll want to use GUI to 'fill in the gaps' with applications that have AppleScript support but may be lacking in an area you'd like to explore.

Universal Access Preferences: Enabling GUI Scripting (pre- Mountain Lion)

From the Apple Menu, Choose 'System Preferences...', then click on 'Universal Access'

To Enable GUI Scripting, be sure that the checkbox entitled 'Enable Access for Assistive Devices' is checked.

Adobe Reader: Scripting the 'Unscriptable'

While Adobe Reader cannot be scripted directly since it does not have a dictionary for AppleScript, it can be scripted using the System Events App. You'll see below that not only can you script reader, but you can do some things scripting through System Events that you wouldn't be able to do at all using AppleScript alone.

Notice that I get the properties of the front window so that I can get the title property, which is the name of the window. Also notice that there is a tell Finder block, because Reader doesn't understand what to do with regular AppleScript

tell application "Adobe Reader" to activate

tell application "System Events"
	try
		tell process "Adobe Reader"
			set saveDelimiters to text item delimiters of AppleScript
			set windProperties to (properties of window 1)
			set currentWindow to title of windProperties
			tell application "Finder"
				activate
				set text item delimiters of AppleScript to "."
				set currentWindow to (text item 1 of currentWindow & " 2") as string
				display dialog "Save this PDF as text as:" default answer currentWindow with icon 1 buttons {"OK"} default button 1
				set currentWindow to text returned of the result
				set text item delimiters of AppleScript to saveDelimiters
			end tell
			tell application "Adobe Reader" to activate
			click menu item "Text..." of menu 1 of menu item "Save As" of menu 1 of menu bar item "File" of menu bar 1
			delay 2
			tell window "Save"
				repeat 2 times
					click text field 1 -- a double-click to select the text
				end repeat
				keystroke currentWindow
				delay 3
				click pop up button 1 --choose a destination folder 		
				delay 5
				tell checkbox 1 to if value is 1 then click --show extension
				delay 3
				click button "Save"
			end tell
		end tell
	on error theError number theNumber
		display alert "Error number " & theNumber message theError as warning
	end try
end tell

A GUI Script for Universal Access

This is a script that pins down everything (well, almost everything) you would need to know to automate actions with the Universal Access pane of System Preferences. This was scripted with Snow Leopard 10.6.8 in mind, but provides the basis (with a little tinkering) to script Universal Access in other versions of the system as well.

I attempted to order the commands in a logical manner, but you may want to change the order or omit certain commands to suit your particular purposes.

The script:

tell application "System Events"
	if UI elements enabled is false then
		tell application "System Preferences"
			activate
			set current pane to pane id "com.apple.preference.universalaccess"
			display alert "GUI Error" message "Access for assistive devices must be enabled to run this script. To continue, click the OK button and enter a security password (if required) then run the script again." as warning
		end tell
		set UI elements enabled to true
		if UI elements enabled is false then return "user cancelled"
		delay 1
	end if
end tell
tell application "System Preferences"
	launch
	try
		set the current pane to pane id "com.apple.preference.universalaccess"
		reveal anchor 1 of current pane
		set mainRadioChoices to {"Seeing", "Hearing", "Keyboard", "Mouse & Trackpad"}
		set universalHelp to (choose from list mainRadioChoices with prompt "Get assistance with:" with title "Universal Access Prefs" default items "Seeing" OK button name "Set") as string
		tell application "System Events"
			tell process "System Preferences"
				if universalHelp = "Seeing" then
					click radio button "Seeing" of tab group 1 of window 1
					delay 2
					click radio button "On" of radio group 2 of tab group 1 of window 1 --Turn VoiceOver on
					delay 2
					click radio button "On" of radio group 3 of tab group 1 of window 1 --Turn Zoom on
					delay 2
					click radio button "White on Black" of radio group 1 of tab group 1 of window 1
					delay 2
					tell checkbox 1 of tab group 1 of window 1 to if value is 0 then click --Turn on 'Use grayscale'
				else if universalHelp = "Hearing" then
					click radio button "Hearing" of tab group 1 of window 1
					delay 2
					tell checkbox "Flash the screen when an alert sound occurs" of tab group 1 of window 1 to if value is 0 then click
					delay 2
					repeat 2 times
						click button "Flash Screen" of tab group 1 of window 1
						delay 1
					end repeat
					delay 2
					tell checkbox "Play stereo audio as mono" of tab group 1 of window 1 to if value is 0 then click
					delay 2
					click button "Adjust Volume… " of tab group 1 of window 1
				else if universalHelp = "Keyboard" then
					click radio button "Keyboard" of tab group 1 of window 1
					delay 2
					click radio button "On" of radio group 1 of tab group 1 of window 1 --Sticky Keys
					delay 2
					tell checkbox "Press the Shift key five times to turn Sticky Keys on or off" of tab group 1 of window 1 to if value is 1 then click
					delay 2
					tell checkbox "Beep when a modifier key is set" of tab group 1 of window 1 to if value is 0 then click
					delay 2
					tell checkbox "Display pressed keys on screen" of tab group 1 of window 1 to if value is 0 then click
					delay 2
					click radio button "On" of radio group 2 of tab group 1 of window 1 --Slow Keys
					delay 2
					tell checkbox "Use click key sounds" of tab group 1 of window 1 to if value is 0 then click
					delay 2
					click button "Set Key Repeat…" of tab group 1 of window 1
				else if universalHelp = "Mouse & Trackpad" then
					click radio button "Mouse & Trackpad" of tab group 1 of window 1
					delay 2
					click radio button "On" of radio group 1 of tab group 1 of window 1 --Mouse Keys
					delay 2
					--Remember to hit the option key 5 times to release:
					tell checkbox "Press the Option key five times to turn Mouse Keys on or off" of tab group 1 of window 1 to if value is 0 then click
					delay 2
					tell checkbox "Ignore built-in trackpad when Mouse Keys is on" of tab group 1 of window 1 to if value is 0 then click
					delay 2
					tell checkbox "Ignore built-in trackpad when mouse or wireless trackpad is present" of tab group 1 of window 1 to if value is 0 then click
					click button "Open Keyboard Preferences..." of tab group 1 of window 1
				end if
			end tell
		end tell
	on error theError number theNumber
		display alert "Error number" & theNumber message theError
	end try
end tell

Note lines such as this:

tell checkbox "Flash the screen when an alert sound occurs" of tab group 1 of window 1 to if value is 0 then click

A value of 0 means that the checkbox is unchecked (off or false) and 1 means it is checked (on or true)

Folder Actions Setup
Folder Actions Setup | Source

A Script Using Folder Actions

There are times when you want to perform an action on a file as it is being moved from one place to another. Using the scripts below, you can open an image in Preview that needs to be rotated to improve its appearance when it is opened later.

The first snippet (removing folder items from targetFolder after losing removedItem) would be attached to the source folder from which the image is supposed to be moved. The second (adding folder items to targetFolder after receiving addedItem), sets in motion the editing of the image after it has been moved to the target folder and therefore must be attached to the target (destination folder).

In addition to attaching the AppleScripts to the appropriate folders, you must enable folder actions for the folders cited above. To enable folder actions, go to: http://www.macworld.com/article/1167157/automate_tasks_with_folder_actions.html

This is the script to be attached to the source folder (the folder containing the image to be moved:)

on removing folder items from targetFolder after losing removedItem
	set removedItem to (get item 1 of removedItem)
	tell application "Finder" to open file removedItem
end removing folder items from

And this is the script to be attached to the folder to which the image will be moved:

on adding folder items to targetFolder after receiving addedItem
	tell application "System Events"
		if UI elements enabled is false then
			tell application "System Preferences"
				activate
				set current pane to pane id "com.apple.preference.universalaccess"
				display dialog "This script requires access for assistive devices be enabled." & return & return & "To continue, click the OK button and enter an administrative password in the forthcoming security dialog." with icon 1
			end tell
			set UI elements enabled to true
			if UI elements enabled is false then return "user cancelled"
			delay 1
		end if
	end tell
	
	tell application "Preview"
		activate
		set rotateBoolean to button returned of (display alert "Image Rotation" message "Rotate currently selected image ?" buttons {"Left", "Right", "No"} default button 3 as warning)
		if rotateBoolean does not contain "No" then
			tell application "System Events"
				tell process "Preview"
					click menu item "Select All" of menu "Edit" of menu bar 1
					if rotateBoolean contains "Left" then
						click menu item "Rotate Left" of menu "Tools" of menu bar 1
					else if rotateBoolean contains "Right" then
						click menu item "Rotate Right" of menu "Tools" of menu bar 1
					end if
					set saveBoolean to button returned of (display alert "Save Changes" message "Keep changes or revert to saved ?" buttons {"Save", "Revert"} default button 2 as warning)
					if saveBoolean contains "Revert" then
						click menu item "Revert to Saved…" of menu "File" of menu bar 1
					else if saveBoolean contains "Save" then
						click menu item "Save" of menu "File" of menu bar 1
					end if
				end tell
			end tell
		end if
	end tell
end adding folder items to

Find and Reveal Person in Address Book

Using GUI in Address Book Search

The first part of this script gets all of the entries in Address Book as a list and displays them in a 'choose from list'. The result is placed in the variable targetPerson for use in the second part (the search) of the script which uses a little GUI. Before execution of the search part of the script there is a test to see that GUI Scripting has been enabled in System Preferences.

tell application "Address Book"
activate
set thePersons to (get name of every person) as list
set targetPerson to (choose from list thePersons with prompt "Choose an entry to locate from the list below:") as string
tell application "System Events"
if not (UI elements enabled) then
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.universalaccess"
display dialog "To proceed, you must enable access for assistive devices." & return & return & "Click 'OK' and enter the security password." with icon 0
end tell
set UI elements enabled to true
if not (UI elements enabled) then return
delay 1
end if
tell application "Address Book" to activate
key code 0 using command down
keystroke targetPerson
end tell
end tell

As a reminder, this script requires that universal access be enabled and, therefore runs a check for that before executing the main function of the script, which relies on GUI.

The main part of the script:

tell application "Address Book" to activate
key code 0 using command down
keystroke targetPerson
end tell

uses a key code to select the text (if any), that may already be in the search field, and then pastes (or types actually) the contents of the variable targetPerson into the field, which causes Address Book to begin searching for an entry that matches that text. It must be nested within the System Events block; otherwise it will not work, because the GUI must be directed to System Events before it is directed to Address Book.

Getting UI Elements for GUI Scripting

With the 'Universal Access' pane of System Preferences open, use this simple script to get info on its elements. I can't stress enough just how useful this simple script is in getting good syntax for writing scripts for GUI. The script that follows shows how to use some of the syntax from this.

tell application "System Events"
tell process "System Preferences"
get entire contents of window 1
end tell
end tell

This yields a rather lengthy list of elements. While some are not very useful (static text, for example), some of the element descriptions can be quite useful in writing scripts. Here are some notable ones from the Universal Access Pane:

The tabs (technically radio buttons) at the top of the window:

radio button "Seeing" of tab group 1 of window "Universal Access"
radio button "Hearing" of tab group 1 of window "Universal Access"
radio button "Keyboard" of tab group 1 of window "Universal Access"
radio button "Mouse & Trackpad" of tab group 1 [of window "Universal Access"]

Some buttons:

button "Open VoiceOver Utility..." of tab group 1
button "Options..." of tab group 1

A slider:

slider "Enhance contrast:" of tab group 1 of window "Universal Access"
value indicator 1 of slider "Enhance contrast:" of tab group 1

Some other radio buttons:

radio button "Black on white" of radio group 1 of tab group 1
radio button "White on black" of radio group 1 of tab group 1
radio button "On" of radio group 3 of tab group 1
radio button "Off" of radio group 3 of tab group 1 of window "Universal Access" of application process "System Preferences" of application "System Events"

Some checkboxes:

checkbox "Enable access for assistive devices"
checkbox "Show Universal Access status in the menu bar"

And some other stuff:

tool bar 1 of window "Universal Access"
group 1 of tool bar 1 of window "Universal Access"
text field 1 of group 3 of tool bar 1 of window "Universal Access"
button 1 of text field 1 of group 3 of tool bar 1 of window "Universal Access"

I haven't investigated the scriptability of all of these elements, but here's a script I wrote to give you an idea of what you can do with the some of the info. Run this in the Script Editor, I put in some delays to make it a sort of a 'show'

tell application "System Preferences"
	activate
	delay 3
	set current pane to pane "com.apple.preference.universalaccess"
	tell application "System Events"
		tell process "System Preferences"
			tell window "Universal Access"
				click radio button "Seeing" of tab group 1
				delay 1
				click radio button "On" of radio group 3 of tab group 1 --Initiate Zoom
				delay 1
				click button "Options..." of tab group 1
				delay 2
				click button "Done" of sheet 1
				delay 2
				click radio button "White on Black" of radio group 1 of tab group 1
				delay 2
				click radio button "Off" of radio group 3 of tab group 1 --Close Zoom
				delay 2
				click radio button "Black on White" of radio group 1 of tab group 1
			end tell
		end tell
	end tell
end tell

Just a little note for clarity. When you run the first script, this is appended to each element:

of application process "System Preferences" of application "System Events"

However, when enclosed in the proper tell blocks, it is superfluous.

Most of this is outlined so you can easily understand it without any further explanation. There is one thing that I want to point out here though. Take note of the line: click button "Done" of sheet 1. This closes a dialog that was initiated by clicking on the 'Options...' button. You use the sheet 1 syntax any time that you are addressing a dialog and, usually you can refer to the button to close the 'sheet' by name.

Copy this into Script Editor and save as Application:

on run
tell application "AppleScript Editor" to activate
delay 3
tell application "System Events"
tell process "AppleScript Editor"
click menu item "New" of menu "File" of menu bar 1
delay 2
tell window 1
click button "Record" of tool bar 1
end tell
end tell
end tell
end run
Preview
Preview | Source

Copy Preview Image to FileMaker Database

FileMaker Pro and GUI

This is another one of those scripts which uses GUI to get around deficiencies in the scriptability of an application. In this particular case, it gets around not only the deficiencies inherent in FileMaker, but also the fact that Preview isn't scriptable at all:

tell application "FileMaker Pro"
activate
try
set saveDelimiters to text item delimiters of AppleScript
set imageFile to (choose file with prompt "Choose an image to import:")
set text item delimiters of AppleScript to ":"
set theName to text item -1 of (imageFile as string)
set text item delimiters of AppleScript to saveDelimiters
set imageFile to (imageFile as alias)
tell application "Finder" to open imageFile
tell application "System Events"
tell process "Preview"
tell menu bar 1
tell menu "Edit"
click menu item "Select All"
click menu item "Copy"
end tell
tell menu "File"
click menu item "Close Window"
end tell
end tell
end tell
end tell
go to (create record)
set data of cell "GraphicName" of current record to theName
go to cell "GraphicField" of current record
paste
on error errorMsg
display dialog errorMsg
end try
end tell

This script activates System Events to use GUI on Preview. The script opens an image file, selects and copies the image and closes the image file.

The second part of the script returns to FileMaker, creates a new record, enters the short name of the image file which is stored in the variable theName into the cell "GraphicName" and pastes the image file (from the clipboard) into the image container named "GraphicField".

GUI Script to Print with FileMaker Pro Database

Getting Around Quirky Nature of FileMaker with AppleScript

If you have written enough scripts in FileMaker which combine FileMaker scripts with AppleScript, you know that it can be very enerving. The FileMaker scripting alone, can be frustrating. If you are an experienced AppleScripter, though you wouldn't expect the AppleScript part to be nearly as difficult--right ?

So you've worked out all of the glitches with FileMaker and you've run your AppleScripts successfully in Script Editor, then you put it all together and run it through FileMaker and the AppleScript does not work, even though you have checked FileMaker's dictionary and verified that the terminology you have used is there and that you have assembled the syntax correctly etc.

So what do you do ?

Below, I have written a script that 'gets around' just one frustrating situation of this sort. You see, invoking AppleScript menu commands in FileMaker (do menu item, etc), although they should be possible, do not work. Why is this, when the terminology is supposedly supported in its AppleScript dictionary ? Who knows ?

With that said, the script below, written for use with FileMaker Pro, also works with AppleWorks 6, TextEdit, and Microsoft Word (with some modification):

tell application "System Events"
try
if UI elements enabled then
tell application "FileMaker Pro" to activate
tell process "FileMaker Pro"
tell menu bar 1
tell menu 3 --File menu
click menu item "Print..." -- or click menu item 14
tell window 1
keystroke return --activate 'Print' button
end tell
end tell
end tell
end tell
else
tell application "System Preferences"
activate
display alert "Universal Access error number -1719" message "UI element scripting is not enabled. Check \"Enable access for assistive devices\" to continue." as critical
reveal pane "com.apple.preference.universalaccess"
error
end tell
end if
on error
set UI elements enabled to true --prompts for password if one is set
end try
end tell
(*--> result: tell application "System Events"
get UI elements enabled
--> true
end tell
tell application "FileMaker Pro"
activate
end tell
tell application "System Events"
click menu item "Print..." of menu 3 of menu bar 1 of process "FileMaker Pro"
--> menu item "Print..." of menu "File" of menu bar item "File" of menu bar 1 of application process "FileMaker Pro"
keystroke "
" --> return
end tell*)

Creating a New AppleWorks Document Using GUI

Copy Contents of a Document and Paste into New Doc

This script shows how to go about using GUI to perform menu and sub-menu tasks with AppleWorks 6 to copy the content of the frontmost window to the clipboard and to paste it into a new document.

This script presumes both that Assistive Devices is enabled and that AppleWorks is currently running and has an open text document:

try
tell application "AppleWorks 6"
activate
end tell
(*There are 3 main sub-scripts contained within the 'tell menu bar 1' part of the script below*)
tell application "System Events"
tell process "AppleWorks 6"
tell menu bar 1
--This selects and copies the contents of the frontmost document to the clipboard
tell menu bar item "Edit"
tell menu "Edit"
click menu item "Select All"
click menu item "Copy"
end tell
end tell
--This creates a new text document
tell menu bar item "File"
tell menu "File"
tell menu item "New"
tell menu "New"
click menu item "Word Processing"
end tell
end tell
end tell
end tell
--Finally, this pastes the contents of the clipboard into the new document
tell menu bar item "Edit"
tell menu "Edit"
click menu item "Paste"
end tell
end tell
end tell
end tell
end tell
return true
on error theError
return false
end try

Please leave comments or suggestions for new articles at the bottom of the page. For more on AppleWorks 6, go to: Free AppleScripts for AppleWorks 6

MacOS System Prefs
MacOS System Prefs | Source

GUI Scripting for System Preferences Panes and Apps

By directing script statements to the 'System Events' application, you can get the system to perform keyboard and other tasks as if you were actually manually performing them yourself. It is not only a great way to automate tasks, but it is also an effective measure for applications that are otherwise 'unscriptable'. This is referred to as General User Interface Scripting (or 'GUI' Scripting). Here I will give you two examples of this, one directed to System Preferences, and another example directed to AppleWorks 6 to perform menu tasks that can also be applied to other applications that, while they are scriptable to an extent, do have some limitations.

First, one that deals with the Universal Access preferences pane of System Preferences:

tell application "System Events"
(*If the checkbox (in Universal Access Preferences) with the prompt "Enable access for assistive devices" is not enabled, then the user is prompted to click the box to enable it.*)
if not (UI elements enabled) then
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.universalaccess"
display dialog "To proceed, you must enable access for assistive devices." & return & return & "Click 'OK' and enter the security password." with icon 0
end tell
(*If the user clicks 'OK', enters the correct security password and then enables the checkbox, then assistive devices is enabled:*)
set UI elements enabled to true
(*If the user clicks 'Cancel', enters an invalid security password or does not enable the checkbox, the script is aborted without any action being taken*)
if not (UI elements enabled) then return
delay 1
end if
end tell

Now the second, which performs menu tasks in AppleWorks 6 that checks first to determine that Access for Assistive Devices is enabled and, if not, it will pass the script on to Universal Access to notify the user that Assistive Devices needs to be enabled to perform the script:

try
tell application "AppleWorks 6"
activate
end tell
tell application "System Events"
if not (UI elements enabled) then
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.universalaccess"
display dialog "To proceed, you must enable access for assistive devices." & return & return & "Click 'OK' and enter the security password." with icon 0
end tell
end if
tell process "AppleWorks 6"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
click menu item "Print..."
end tell
end tell
end tell
end tell
end tell
return true
on error errorMsg
display dialog errorMsg
end try

In the previous script, you could substitute any number of menu commands (most will work without any further scripting). With a little more knowledge of GUI, you could also script to send commands to specific panes that come up when you choose menu items requiring further user input.

Once you get the hang of it, you will have a sort of intuition on how to delve into GUI's for your specific requirements.

Comments and Questions on AppleScript GUI

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article