AppleScript Tips & Tricks

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

[code lang=”applescript”]
tell application “System Events”
set UI elements enabled to true
end tell

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

[code lang=”applescript”]
set value of attribute “AXValue” of text field 1 to “String”

How To Iterate Through Rows in a Table

[code lang=”applescript”]
repeat with r in rows of table 1 of window 1
log r
end repeat

Retrieve Every UI Element Available in a Window

[code lang=”applescript”]
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.

[code lang=”bash”]

function normalize_path() {
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”

# open the flas
open -a “Flash Player” “$line”

sleep 1

osascript slide_preview.scpt “$thumbnailPath”

killall “Flash Player”

exit 0

Other Applescript Code Snippets:

Helpful Applescript Articles

  • Carlos Ysunza

    good tips for the tricky GUI. I avoid as much as I can but sometimes there’s no other way, so thanks for share.
    If I can help you I gladly share with you my knowledge on AppleScript

  • rihatum

    I need to use applescript, so that we can email / copy the script to our remote users and they can execute it to setup the vpn to our work vpn server.

    I have never used applescript and google led me to a apple forum post with the following, the problem I am facing is, that I can’t seem to name the VPN connection, neither i can give it a pptp vpn server address, dns, default gateway and any custom routes.

    How can I add those fields / enteries in ?
    tell application “System Preferences”
    reveal pane “Network”
    tell application “System Events”
    tell process “System Preferences”
    tell window 1
    click button “Add Service”
    tell sheet 1
    click pop up button 1
    click menu item “VPN” of menu 1 of pop up button 1
    delay 1
    click pop up button 2
    click menu item “PPTP” of menu 1 of pop up button 2
    click button “Create”
    end tell
    click button “Apply”
    end tell
    end tell
    delay 1 — optional (just for visual feedback)
    keystroke “w” using {command down}
    end tell
    end tell

    will be grateful for your help on this

  • 4fthawaiian

    Just wanted to leave a short note of thanks for the VPN script. The only issue I had is that it would start System Pref’s every time it ran, which was a tad annoying. I came up with this way to wrap it up in a shell script to help work around this:

    1 #!/usr/bin/env bash
    3 GATEWAY=
    4 ping -c 1 ${GATEWAY} 2>&1 > /dev/null && exit # ping once and bail if it works
    5 echo VPN DOWN | mail # send me a quick mail to let me know it went down
    8 VPNLIB=`cat ~/lib/AppleScript/vpn` # read in the VPN functions
    10 osascript <<EOF
    11 — include the VPN library
    12 $VPNLIB
    14 if not vpn_status("")
    15 toggle_vpn_status("")
    16 end if
    17 EOF

    Thought I'd leave this here for anyone else who'd like to make use of it.


  • Pavel


    here is a path I get from UI browser of the window I wand to put value to:
    text field “Text” (text field 1)
    radio button “Title” (radio button -92233…)
    tab group “Title” (tab group -92233…)

    here is the code:
    set value of text field 1 of radio button “Title” of tab group “Title” of window 1 to “test”

    but I get this error:
    Can’t set text field 1…to “test”

    What do I do wrong?