DEV Community

Cover image for Automate Anything Easily With Applescript
Pranith Hengavalli
Pranith Hengavalli

Posted on

Automate Anything Easily With Applescript

If there's a series of actions you find yourself doing really often, it'll probably save a lot of time in the long term to write a script to do it. AppleScript lets you automate just about anything on your Mac, from file I/O to interactive prompts and filling in web browsers. It can even automatically reply to your messages for you!

Unlike regular programming languages, its deep integration with OSX lets you create rich GUIs and complex actions that would require a lot more effort to do a more universal language like Python.

Recently I wrote a script that takes the name of my git branch and automatically builds iOS and Android variants, and sends me a notification on Slack when it's all done. This would otherwise involve a lot of manual effort since the builds are started on two separate Jenkins web dashboards. This is something I usually do a few times a day, and I probably save a few minutes cumulatively thanks to the script. 😄

To start writing a script, open Script Editor on your Mac.

In many cases, you can generate an AppleScript by just pressing the record button and doing the sequence of actions once. The script editor generates code for each action as you perform it.


One of the cases where AppleScript doesn't work as well is when interacting with the content on web pages. For this I like to execute Javascript directly on the page, which I think is a more powerful approach than detecting elements in the GUI.


The following snippet defines a function called clickClassName that clicks on the selected element on the page, in this case the 3rd button.

to clickClassName(theClassName, elementnum)
  tell application "Safari"
    do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
  end tell
end clickClassName

clickClassName("button", 3)

User input

You can accept user input in your scripts, and you get these pretty dialog boxes that come with various input type options. This is useful to pick options at the time of running the script.

display dialog 
    "What website do you want to see?" buttons {"Dev.to", "StackOverflow", "Hacker News"}
set theSite to button returned of result
display dialog theSite

Persistent Data

Properties are saved between launches, and can be used to remember your last input.

property variable : 0
display dialog variable
set variable to 100

Will show 0 on the first run and 100 on the second run.


A surprising number of applications support automation. Go to File > Open Dictionary to see a list of installed applications that support AppleScript control, as well as documentation about supported commands.

Finally, you'll need to save it as an application so it won't open the script editor each time you try to run it. Go to File > Export > Set the file format to 'Application' > Save.


The best scripts are the ones you write for yourself, but here are some useful scripts to get you started:
https://www.computerworld.com/article/2524645/10-applescripts-to-make-you-love-your-mac--even-more-.html
https://www.smashingmagazine.com/2009/05/mac-hacks-17-applescripts-to-make-your-life-easier/
https://drago86.wixsite.com/scripting/scripts

Top comments (0)