Skip to content

Instantly share code, notes, and snippets.

@disulfidebond
Last active March 30, 2019 20:25
Show Gist options
  • Save disulfidebond/e3410ee182e58d2b82549f95dada1ab0 to your computer and use it in GitHub Desktop.
Save disulfidebond/e3410ee182e58d2b82549f95dada1ab0 to your computer and use it in GitHub Desktop.
Applescript automation with NCBI

Overview

Applescript has a long and somewhat storied history with Apple. It dates back to OS 'Classic', and has been intermittently updated and forgotten over the decades. Although elements of AppleScript can be seen in Swift, and Objective-C to a degree, AppleScript shares very little in common syntactically with either.

Particularly with the advent of Swift, AppleScript has fallen to the wayside, although it is still extremely useful in certain situations, such as automation, or an interface with Bash and the Apple GUI.

Basics

The basic usage via Bash (or Terminal) is:

    osascript OPTION [COMMAND || SCRIPTNAME]
    # example that opens Safari, the -e option means execute
    osascript -e 'tell application "Safari" to activate' 
    # example that runs the AppleScript script 'openSafari.scpt'
    osascript openSafari.scpt 

Most versions of MacOSX have a ScriptEditor application that has varying degrees of usefulness or frustration, depending on the OS version. Note that this Applications is separate from XCode completely. If you are building a complicated AppleScript script, you definitely should use ScriptEditor.

Here is an example of the script mentioned above, 'openSafari.scpt', that opens Safari

    to safariVisitWebPage(theWebPage)
    tell application "Safari"
    activate
    set URL of document 1 to theWebPage
    end tell
    end safariVisitWebPage
    safariVisitWebPage("https://www.ncbi.nlm.nih.gov/pubmed/30530595")

Here are a few preliminary pointers:

  • Tabs are not required, and usually are ignored. However, tabs or tab spacing is exceptionally useful for organizing your code.

  • Comments in AppleScript use a double-dash, i.e. anything after '--' will be ignored.

  • Functions are allowed in AppleScript, and must always follow the syntax:

     to FUNCTIONNAME
     -- code
     end FUNCTIONNAME
    
  • Variables are allowed, but have a few quirks (see below)

  • Blocks of code that carry out an action are usually begun with tell and end with end tell

  • Apple Software usually requires minimal AppleScript code

  • Most non-Apple software is still usable with AppleScript, but may require additional or convoluted steps. This is not an acrimonious action by Apple, per se, but rather involves how Events in AppleScript, Objective-C, and Swift are handled.

Here is line by line description of the above code.

  • This first line begins the function safariVisitWebPage

      to safariVisitWebPage(theWebPage)
    
  • The next 2 lines directs AppleScript to the application Safari, and then set the focus (activate) it

      tell application "Safari"
        activate
    
  • Then, AppleScript will set the URL of Safari to the variable theWebPage

        set URL of document 1 to theWebPage
    
  • The tell and the function safariVisitWebPage must be closed, similar to a closing bracket:

        end tell
      end safariVisitWebPage
    
  • Finally, this line of code calls the defined function, and executes it:

      safariVisitWebPage("https://www.ncbi.nlm.nih.gov/pubmed/30530595")
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment