Publishers of technology books, eBooks, and videos for creative people

Home > Blogs > Using AppleScript to enable GUI Scripting, Five AppleScript Tips in Five Days

Using AppleScript to enable GUI Scripting, Five AppleScript Tips in Five Days

You can use AppleScript to control applications on a Mac even if they aren't scriptable by design. You do this by using GUI Scripting, an AppleScript technology that lets you control any application through its graphical user interface, such as clicking buttons, choosing menu commands, and sending keystrokes to text fields.

The problem: You must first enable GUI Scripting by turning on the "Enable GUI Scripting" setting in AppleScript Utility or the "Enable access for assistive devices" setting in the Universal Access pane of System Preferences. It's a pain to have to tell users of your scripts to do this manually when, after all, AppleScript is an automation tool.

The solution: Write your script so it turns on these settings automatically. The script will present an authentication dialog where the current user's name and password may be entered, then the script continues running and executes any GUI Scripting commands that it contains. You no longer have to explain to your users where to find these settings and how to turn them on!

Simply include this AppleScript subroutine handler in your script and call it before issuing any GUI Scripting commands. In the subroutine call, set the 'switch' parameter to true or false to turn GUI Scripting on or off. Test the subroutine's return value to learn whether GUI Scripting is now enabled.

   on enabledGUIScripting(switch)
      tell application "System Events"
         activate
         set UI elements enabled to switch
         return UI elements enabled
      end tell
   end enabledGUIScripting

You can click this link to open the script in the Script Editor window: Script 28.4

This is Script 28.4 in our book Apple Training Series: AppleScript 1-2-3. Read Chapter 28 for more useful information about GUI Scripting.

To learn more about AppleScript, such as how to script applications that are not scriptable by design, return to this blog each day this week for new tips from our book Apple Training Series: AppleScript 1-2-3.