Pinterest Stumbleupon Whatsapp

how to use applescriptIf you’re a fan of using Mac OSX Applescript, you’ve probably noticed some limitations. Namely, that not all programs can be manipulated directly with Applescript. You can, however, use Applescript‘s User Interface (UI) scripting to fill the gaps if you know how.

It’s possible to use Applescript to control the user interface of just about any program on your Mac. Sometimes it’s a little difficult, but it is indeed able to be done. I’ll show you the basics of how to do this yourself and take you through a simple script using these techniques.

Brush Up On Basic Applescript First

If you’re new to Applescript or haven’t looked at it in a while, brush up Learn to Automate Your Mac with AppleScript [Part 1: Introduction] Learn to Automate Your Mac with AppleScript [Part 1: Introduction] Read More on your Applescript knowledge 6 Beginner Resources For Learning Mac Programming 6 Beginner Resources For Learning Mac Programming Read More before reading this guide.

I’ll add screenshots which cover as much as possible, but this article is about the finer details, not about basic Applescript (which we’ve already covered).

Enable UI Scripting

Before you can use UI scripting, go to System Preferences, Universal Access and check the box to enable access for assistive devices.


how to use applescript

Using Key Strokes

The simplest way to interact with the UI of a program with Applescript is to simulate the entering of keystrokes, as if you’re using Mac shortcuts and typing commands.

For instance, in TextEdit you might want to do a find-and-replace search. You probably already know that you can type CMD-F for Find/Replace, type in your search term, press tab to move to the next field, type in the replacement term and then hit “Replace All” to replace the text.

applescript tips tricks

In Applescript, you can also tell the user interface to type these commands. You would communicate CMD-F by the command keystroke “f” using {command down} with Applescript. Also note, the way to execute CMD-SHIFT-S is keystroke “s” using {command down, shift down}. You can also type into the UI with Applescript by using keystroke “mytext”, where your text is entered between the inverted commas. Finally, you can type some simple key strokes such as TAB like this: keystroke tab.

applescript tips

Before using the keystroke command, you need to ensure the process class is being invoked (and don’t forget to end those tell commands later).

tell application “System Events”
tell process “TextEdit”

See the screenshot for the full commands for this basic find/replace action in sequence.

applescript tips

Clicking Buttons

When your program has a number of buttons to choose from, normally you can use a simple Applescript command to control them. In the case of the find/replace you might want to click “Replace All”. The command for this is click button “Replace All”.

More Complex GUI Manipulation

As many Mac programs weren’t designed to be handled by Applescript with UI Scripting, sometimes you have to hunt around to find what you’re looking for. To do this, use the free program called UIElementInspector.

Once this is running, you can inspect any UI element you wish. The UI element inspector will continuously show you the details of any element you’re hovering over.

Look at this example and you can see the hierarchy. The pop-up menu is part of a group, which is part of another group, which is part of a sheet, which is part of a window. Note it’s also described as a pop up button, rather than a menu.

applescript tips

Now see how this is represented in the script:

tell group 1 of group 1 of sheet 1 of window 1
tell pop up button 1

As you can see, in order to get the script to work you need to add a little common sense and a little bit of trial and error.

how to use applescript

In this case, I wanted to save the document as RTF. I used a keystroke command to open the “Save As” window. This hovers above the file’s “Example” window, but the UI element inspector seems to ignore this extra window. Nevertheless, in order to execute the script, you need to call window 1 to action, not window “Example”.

You also need to remember to include every little click you would normally make. In this case, you need to remember to click on the pop-up menu before you can instruct that menu to choose a menu item.

It takes a little work to ascertain exactly what you need to write into the script for the UI you’re trying to use, but with patience and a little guesswork you can manage to do so. I hope that by opening your eyes to this possibility you’re able to create more useful Applescripts for use alone or in Automator.

What are your favourite ways to use Applescript? Could you make those scripts better with some UI scripting? Let us know in the comments!

Leave a Reply

Your email address will not be published. Required fields are marked *