Macs in Chemistry

Insanely great science

Applescript Tutorial 10

A simple Droplet that uses a UNIX command line application.

At my presentation on Applescript and UNIX I was asked how to create a droplet that accessed a UNIX application so I thought it might be useful to give a very simple example.

This is a simple file conversion tool, drop a file(s) containing chemical structures and it will convert them all into SMILES format, and save them in the same folder as the original file. It requires OpenBabel to do the actual file conversion and the best way to get the current version is to install ChemSpotlight. Droplets use the "open" handler, when the user drags and drops filesystem items onto the script, it will be launched and the open handler will be invoked and a list of aliases will be set with the open handler's variable name. We can then work through the list getting each file path, identifying the enclosing folder, then converting to POSIX paths and constructing the shell script. the "do shell script" then runs the command. The script needs to be saved as an application.


and can be identified as a droplet by the script icon with an arrow on it.


Alternatively you can always click "Get Info and paste a custom icon


Full script text

on open these_items
    repeat with i from 1 to the count of these_items
       set the_File to item i of these_items

        set the_Folder to GetParentPath(the_File)

        set the_posix_file to quoted form of POSIX path of the_File
        set the_posix_folder to POSIX path of the_Folder
        set the posix_output_file to quoted form of (the_posix_folder & "output_" & i & ".smi")
        set the ob_script to "/usr/local/bin/babel " & the_posix_file & " -osmi " & posix_output_file

        do shell script ob_script

   end repeat
    display dialog "All Done"

end open

on GetParentPath(the_File)
   tell application "Finder" to return container of the_File as text
    --so we can save the output in the same folder
end GetParentPath