Getting Started with AppleScript

As a consultant, I do my best to be as efficient as possible. I want to provide the greatest value I can, and one of the ways of doing this is by cutting down the repetition in my workflow. If I find myself doing a task frequently, I try to shorten the time it takes to do it, or to automate it completely. Recently, I have been exploring AppleScript, which is a very powerful tool available to anyone running OS X.

Why AppleScript?

I am by no means an AppleScript expert–I would like to take the time to learn the intricacies of the language, but I just haven’t had the chance yet. However, the beauty of AppleScript is that it has a very low learning curve and a very readable syntax, so you can learn some basics quickly. There are many examples online, one of my favorites being a Spotify command line interface (This one with pure AppleScript, and this one with both Bash and AppleScript).

Scripting languages are an integral part of a developer’s toolbox. While there is no language that can work for every possible scenario, some languages are great in general scenarios, and others excel for a specific purpose. The use of AppleScript is limited to OS X development environments, but it excels within them.

Examples

I’m the kind of person who usually has a lot of tabs open, and sometimes, it can be hard to keep track of them.

Rather than scrolling through my open tabs to find the one I know I have somewhere, I rely on a small AppleScript:

#!/usr/bin/env osascript

on run argv
  tell application "Google Chrome"
    set i to 0
    set windowList to every window
    repeat with theWindow in windowList
      set tabList to every tab in theWindow
      repeat with t in tabList
        set i to i + 1
        if item 1 of argv is in the title of t then
          set active tab index of theWindow to i
          activate
        end if
      end repeat
    end repeat
  end tell
end run


When I run the command chrome inb, it will go to the first tab it finds containing the text “inb”–which in my case is usually Google Inbox. I generally have my terminal in the foreground, so it almost always takes less time to type that in than to switch windows and scroll through my open tabs to find the one I’m looking for.

Some other small snippets of AppleScript that I find myself using are:

tell application "Finder" to eject (every disk whose ejectable is true) -- ejects all open disks, such as a portable hard drive
tell app "Finder" to POSIX path of (insertion location as alias) -- gets the path of the foremost finder window
set volume 5 -- sets the system volume to 5
100 as degrees fahrenheit as degrees celsius -- converts 100 degrees fahrenheit to celsius

Now, by themselves, some of these scripts aren’t exactly the most convenient. Opening Script Editor (the traditional way of running AppleScripts) and setting the volume isn’t going to be any faster than using the system tray icon or using the volume keys on the keyboard. However, by combining these scripts with the command line, some serious time can be saved.

Ways to Run AppleScript

  1. The traditional way to run AppleScript is through the built-in application “Script Editor.” This is the best place to write scripts, but not always the best way to run them. When writing AppleScript in the Script Editor, you have access to many code snippets and dictionaries. However, it is inconvenient to open the application and find the file to run every time.
  2. The command osascript is another good way to run AppleScript snippets. This can be done by:
    • osascript /path/to/file
    • osascript -e 'some AppleScript goes here'
  3. Prepending the shebang #!/usr/bin/env osascript to a file containing AppleScript tells the program loader to run it with osascript instead. To make the file executable, run the command chmod u+x /path/to/file. I would also recommend putting these executable files in a folder, and then adding that folder to your PATH (mine is located at ~/bin) so they can be run at any time.

In my workflow, I have a combination of larger AppleScript files in my ~/bin, and smaller commands stored as aliases. It’s quicker for me to add an alias, so I will use this method for smaller scripts. Here is a commonly used snippet of AppleScript I found online, and saved as an alias for easy reuse:

alias eject="osascript -e 'tell application \"Finder\" to eject (every disk whose ejectable is true)'"

Sometimes I don’t limit it to an alias, and I’ll create functions as well:

function vol () {
    /usr/bin/osascript -e "set volume ${1}"
}

I put this in a function so I can use the ARGV environment variable inside the AppleScript. This could be rewritten in pure AppleScript as:

on run argv
  set volume item 1 of argv
end run


I don’t have a particularly good reason why I use a combination of aliases, functions, and executable scripts. I find it easier to add an alias or a function, and that’s what I do in a pinch, when I don’t want to dive too deep into AppleScript.

Conclusion

Although I’m still learning AppleScript, I’m discovering that it can be a powerful tool for everyday use. I keep finding ways to use it daily, beyond the examples I provided. If used correctly, it can be a tremendous productivity booster and it can help cut down on repetitive tasks. I would encourage you to give it a try if you haven’t already.

Conversation
  • Kelly says:

    👍🏼

  • Comments are closed.