Explorations in Data Science and Health

Using Applescript

I am not the type of person who is constantly looking to optimize everything. Profiles of designers and product managers often start with descriptions about their eye being caught by the most minor of inefficiency that they seek to smooth out. I have come late to that skill. Such is my dabbling with Applescript recently.

For those of you who are not aware, their exists a hidden programming language in macOS that purports to allow to extend the operating system to do any of a number things automatically. Applescript has a very cute but often confounding syntax that can occasionally resemble natural english but often in a way that is unexpected. Here is the classic "Hello World" example for Applescript:

say “hello world”

This will have your Mac say "Hello World" outloud. See it's cute. I encourage you to read the other Hello World examples at the blog post linked above. So why am I digging up this very old part of macOS? Because I recently used it to solve one of my more frustrating workflows. We use Box.Com to store and collaborate on documents at work. But, one of the frustrations of working with it are the constantly sharing of non specific links:

slack

Clicking this link will open the document in the Box online viewer, which creates remarkably good renderings of most file types, but won't allow to edit or access the document. Even more frustrating, I generally already have this document on my machine downloaded automatically to a folder on my machine through Box's Sync application. Lastly, I have no idea about the surrounding context of this document: is it a folder called "drafts"? or a folder called "CRITICAL"?

With Applescript, I created a "service" which takes in any file selected in the finder and copies path to that file on box. The script looks like this:

on run {input, parameters}

    set Txt1 to POSIX path of first item of input

    set output to {}

    set AppleScript's text item delimiters to {"/"}

    set delimited to every text item of Txt1

    set output to items 5 through length of delimited
    return output as string

end run

Apple's services which executes this script always begins with the same on run . . . end run sequence with input and parameters. Here is what the service does line by line:

  1. Create a variable Txt1 and set it to the path corresponding to the first element in the input array. Since the service expects a file as input that file will have a known path which we will manipulate.
  2. Create an output variable, blank for now
  3. Tell applescript to use / as a delimiter. Meaning when it looks at Txt1 it will break it up into parts at each / which is the delimiter in a POSIX path.
  4. We then create a new list of all the elements using this delimiter.
  5. We take items 5 through the end of the list and return it.

This creates the following really easy link to use:

slack

Which I then paste into e-mail or slack. I've also created the reverse script:

on run {input, parameters}

    set theUnixPath to input
    set theUnixPath to (POSIX path of (path to home folder)) & "Box Sync/" & theUnixPath
    set theMacPath to (POSIX file theUnixPath)
    tell application "Finder"
        set theItem to item theMacPath
        if (class of theItem) is folder then activate
        reveal theItem
    end tell
    activate application "Finder"
end run

This tells finder to open the folder where the document resides. I have rolled out these scripts across the company and they were a great first foray into Applescript.

If you want to run these scripts, you use the Automator app to create the service. Services can then be registered to shortcuts in Settings. For me, ⌘+⌥+b copies the Box path to the item, ⌘+⌥+o opens the item.