Skip to content

AppleScript Tips & Tricks

Tags: applescript, macos • Categories: Software

Table of Contents

I’ve done a fair amount of applescript work (mostly automation & UI scripting related projects) over the last couple months and – although very frustrating – applescript can allow you to achieve some incredible automation tasks. I’ve compiled a disjointed lists of tips, tricks, and source code that some trying to wade through applescript (specifically ui scripting which can be especially tricky) might find useful.

Enable Access for Assistive Devices

tell application "System Events"
set UI elements enabled to true
end tell

When “set value of text field” Won’t Work

set value of attribute "AXValue" of text field 1 to "String"

How To Iterate Through Rows in a Table

repeat with r in rows of table 1 of window 1
log r
end repeat

Retrieve Every UI Element Available in a Window

tell application "System Events"
tell process "Process Name"
set visible to true
return every UI element of front window
return name of every UI element of front window
end tell
end tell

Get a List of the Children of a UI Element

return value of attribute "AXChildren" of UI element 1

Create / Update / Connect Network Preferences VPN Services

Although since OS X 10.5 we have been given better applescript access to network preference settings, it still isn’t possible to create / update services / interfaces through applescript. With some tricky UI scripting and the help of the UI Element Inspector (or the advanced UI Browser) it is possible to create and update VPN service information. I also wrote a couple functions that allow you to check the existence of a VPN service and the connection status. You can check out the source code here. Note that although the code is VPN service specific it wouldn’t be terribly hard to modify the code for use in automating the creation of aiport, ethernet, etc services.

Take a Screenshot of a SWF

This is a bit more complex that one would expect. Because the Flash Player isn’t built using cocoa it doesn’t support alot of native applescript methods and getting a screenshot of the actual content (not containing the title bar!) of the swf is actually pretty challenging. The script will take a screenshot of only the content of the actual swf. This is useful if you have a flash app that has loadable swf components that the user should be able to preview through a thumbnail. Coupling the script with the following bash script allows you to easily generate thumbnails for all the swfs in the specified directory.

#!/bin/bash

function normalize_path() {
eval "NORMALIZED_PATH=$1"
NORMALIZED_PATH=`php -r "echo realpath('$NORMALIZED_PATH');"`
}

find ../path/to/swfdirectory -name "*.swf" | while read line; do
# remove the relative reference
normalize_path "$line"
line=$NORMALIZED_PATH

# open the flas
open -a "Flash Player" "$line"

sleep 1

thumbnailPath=${line/.swf/.jpg}
osascript slide_preview.scpt "$thumbnailPath"

killall "Flash Player"
done

exit 0

Other Applescript Code Snippets:

Helpful Applescript Articles