Quick Links

Applescript for Scientists Tutorials

Applescript Tutorial 1
This is the first of what will hopefully be a series of tutorials on Apple's own scripting language Applescript that I will be writing for
MacResearch.org. I should perhaps begin with a confession, "I'm not a programmer", by training I'm a chemist and I spent the majority of my career as a Medicinal Chemist. This might actually make me the ideal person to write this since I've always thought of Applescript as the programming language for the rest of us. Applescript is a scripting language that allows users to automate reptitive or complex tasks, or customise applications as such it is really useful for lttle tools or widgets that make life easier for you. In this tutorial I cover getting started, using the script editor, Applescript dictionary, folder actions and interacting with applications.
Click here to see a formatted view of this tutorial in a new window.

Applescript Tutorial 2
This is the second in the Applescript tutorials written for
MacResearch, in this tutorial I cover the use of Applescript interactions with the UNIX system, in particular the issues of Mac and UNIX paths and the use of shell scripts.
Click here to see a formatted view of this tutorial in a new window.

Applescript Tutorial 3
This is the third Applescript tutorial written for MacResearch, reading and writing files, creation and manipulation of lists, and using ChemDaw to calculate properties.
Click here to see a formatted view of this tutorial in a new window.

Applescript Tutorial 4
This is the fourth Applescript tutorial written for MacResearch, reading and writing UNIX files, dealing with missing properties, and using ChemDaw to calculate properties.
Click here to see a formatted view of this tutorial in a new window.

Applescript Tutorial 5
This is the fifth Applescript tutorial written for MacResearch, creating a sub-structure searchable chemical database.
Click here to see a formatted view of this tutorial in a new window.

Applescript Tutorial 6
This is the sixth Applescript tutorial written for MacResearch, rendering structures in a chemical database
Click here to see a formatted view of this tutorial in a new window.

Applescript Tutorial 7
This is the seventh Applescript tutorial written for MacResearch, actually it uses a web viewer within the FileMaker database to display a web page that uses the chemdraw plugin to render the structure.
Click here to see a formatted view of this tutorial in a new window.

Applescript Tutorial 8
This is the eighth Applescript tutorial written for MacResearch. Rich Apodaca has been discussing
embedding molecular information in images of molecules, such as a PNG file depicting a 2D structure. As we move to a more web-centric view of the world it is apparent that much of research information will be only available via the web, whilst images of chemical structures are usually adequate for a human viewer the chemical structure cannot be indexed and subsequently searched. In a subsequent article Rich showed a method of extracting the information as text. In this tutorial I'm going to show how to use applescript to extract the information from the PNG file and then display the structure in a couple of chemical display packages in an editable form.
Click here to see a formatted view of this tutorial in a new window.

Applescript Tutorial 9
This is the ninth Applescript tutorial written for MacResearch. In this tutorial we create an applescript for ChemBioDraw (aka ChemDraw) that embeds chemical information into the meta data of an image file.
Click here to see a formatted view of this tutorial in a new window.

Applescript Tutorial 10
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
Click here to see a formatted view of this tutorial in a new window.

Send to MOE

Send to moe A script for chemdraw, that sends a selected structure to MOE, needs to be stored in the Chemdraw folder with the other chemdraw scripts

ConvertChemDraw_to_TIFF

ChemDraw from CambridgeSoft has excellent Applescript support. I'm sure many chemists use ChemDraw to create the structures and figures for publications, and whilst ChemDraw can produce beautiful graphics some publishing companies insist on all graphics being submitted in "TIFF" format. This means that you have to open each of the ChemDraw files and then save them in "TIFF" file format. You can't only work in "TIFF" format because the image then loses all the chemical information that is available in the ChemDraw native "CDX" format. A repetative opening of files and then saving in another file format, just the task for Applescript! This folder action is available here as a zip file. It needs to be stored in "Macintosh HD:Library:Scripts:Folder Actions". Now you have the script you need to attach it to a folder. Create a folder somewhere and call it "Images", control-click (or right click) on the folder to bring up the contextual menu. By default, Folder Actions is not enabled. To turn on the Folder Actions architecture, select "Enable Folder Actions" from the contextual menu. Folder Actions will now be enabled system-wide. Now bring up the contextual menu again and select "Attach a Folder Action", navigate to and select "Convert_ChemDraw_to_TIFF". You are now already to use it. Draw a structure in ChemDraw and save it to the "Images" folder, you should find that the script is activated, it creates two folders (the first time it is used) "Original CD Files" and "TIFF Images", you should find your original ChemDraw image in "Original CD Files" and a copy of the image in TIFF format in "TIFF Images".

Convert Applescript to HTML

This Applescript is from From Concentrate Software takes the frontmost Script Editor document and converts it into an HTML document complete with all the syntax colouring and spacing. I've used it for the tutorials and found it invaluable. To use, simply toss this script into ~/Library/Scripts/Script Editor and select this script from the scripts menu in OS X. Your current script will be parsed and exported as an HTML document. It can be downloaded from here

DupCheck

This applescript droplet, dupcheck - identifies potential duplicates of a file using Spotlight metadata, simply drop a file on the icon and the search will run, it may take 30 secs for a large drive. You will then get a list of duplicates (if any) and the option to show where they are in Finder windows.

It is based on a hint from macosxhints, see
http://www.macosxhints.com/article.php?story=20061003163429425

ChemDraw Properties

This script takes the selected structure and calculates a variety of chemical properties. Full details are in the Applescript tutorial 3 (see above).

Multi ChemDraw Properties

This script takes as input a file containing SMILES strings and calculates a variety of chemical properties. Full details are in the Applescript tutorial 4 (see above).

Combining PDFs a Automator Workflow

Today I was sent a license agreement as a 6 page pdf file, I was asked to sign the document and send it back. All very well but I was in a hurry and wanted to get to work a little faster than the postal service might allow. I have a scanner so I figured I could sign a copy of the last page then scan it and convert to pdf. I could have then sent the original license pdf plus the last page by email but that did seem a little unprofessional.
So then how to combine the signed page with the rest of the pages of the license agreement?
Well opening the original pdf in Preview I could select pages 1 to 5 to print and then use the "Save as pdf" option from the "PDF" button so create a new pdf with just the first 5 pages.

save_to_pdf

Now create an Automator workflow to join the files.
Open Automator and you should see a three pane window. The first pane contains the library of applications that have automator actions, the next pane shows the actions available for the selected application. Double click on an action to add it to the workflow in the third pane. No add these actions
From the Finder library select:-
Get Specified Finder Items
Sort Finder Items (and from the pulldown menus select "name" and "ascending"
From the PDF library select:-
Combine PDF Pages
Then finally from the Preview Library selct:-
Open Images in Preview.

combine_pdf_workflow

Now save the workflow as Combine_PDF in "username/Library/Workflows/Applications/Finder/"
Now when you select several pdf files in the Finder, control-click (or right click) will bring up a menu that includes automator actions, choose combine_PDF and Preview will open showing a single document containing all files.

grab1

Note I added a sort option, this was to give some control over how the files were combined. If you now name them alphabetically then they will be combined as desired.

Print_clipboard

I was recently asked if it was possible to print text on the clipboard without the need to open another application to paste it into. One easy way to do this is to create an applescript, seems an easy thing to do but there are a couple of interesting aspects that make this worth sharing.One way to print in the background is to use the UNIX "lp " command, there is an excellent summary of UNIX printing here. However when you copy text to the clipboard if often contains invisible non-printing characters that can cause lp to fail. So first we we convert the text to plain text and remove any application specific formatting. The as text portion grabs the stylized text from whatever other junk may be surrounding it (which can be significant), as record splits out the style and text portions, and «class ktxt» grabs the plain text portion of that.The text is then written to a file the location of which is defined within the temporary items folder.lp is then used to print the file.If you want to see how you might use this have a look here.


--Need to first remove application specific formatting from clipboard
set the clipboard to «class ktxt» of ((the clipboard as text) as record)

set the_clip to the clipboard
--
Comment out if not needed
display dialog the_clip

tell application "Finder" to set the_folder to (path to temporary items folder) as text
--
If you want to keep the file use
--
tell application "Finder" to set the_desktop to (path to desktop folder) as text

set target_file to the_folder & "JUNK"
--
display dialog target_file

my write_to_file(the_clip, target_file, false)

set posix_path to POSIX path of target_file
--
display dialog posix_pathsw

set to_print to "lp " & posix_path

do shell script to_print
--
For testing
--
tell application "Terminal"
--
activate
--
do script to_print
--
end tell

on write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
--
display dialog "file_done"
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file

Comparing version numbers

I'm in the process of updating iBabel and one of the things I'm adding is the option to use some of the newer tools now available in the latest version of OpenBabel. However some users prefer not to live on the bleeding edge and want to continue to use the older version of OpenBabel so I'd like to only have the tool options available for which ever version of OpenBabel they have installed.

You can get the version number of the OpenBabel installation with the simple command

/usr/local/bin/babel -V
Open Babel 2.1.0 -- Apr 13 2007 -- 21:38:32
We can wrap this in an Applescript and use the "do shell script" command to get the returned text, simple parsing of the returned text then gives the version number.

set the_script to "/usr/local/bin/babel -V"

set the_version to (do shell script the_script)

display dialog the_version

--
Open Babel 2.1.1 -- Apr 13 2007 -- 21:38:32

set the_offset_minus to the offset of "--" in the_version
set the_offset_el to the offset of "el" in the_version


set babel_version to (characters (the_offset_el + 3) thru (the_offset_minus - 2) of the_version) as text

display dialog babel_version

Comparing version numbers however is not so straightforward since the version number could be 2.10.3 or 2.3.10, fortunately Apple have kindly provided a simple means to compare this type of numeric strings. So using "considering numeric strings".

considering numeric strings
if babel_version > "2.1" is true then --> true, if you consider each numeric string to be a single "character".
display dialog "True"
end if
end considering

The actual implementation in iBabel is this:-

considering numeric strings
--Different tools available depending on version of iBabel
if babel_version > "2.1" is true then --> true, if you consider each numeric string to be a single "character". --display dialog "2.2"
repeat with i in my tools_options_new
make new menu item at the end of menu items of menu of popup button "tools_option" of tab view item "Tools" of tab view "nstab" with properties {title:i, enabled:true}
end repeat
else if babel_version < "2.1" is true then
--display dialog "2.0"
repeat with i in my tools_options_old
make new menu item at the end of menu items of menu of popup button "tools_option" of tab view item "Tools" of tab view "nstab" with properties {title:i, enabled:true}
end repeat
end if
end considering

So depending on the version of OpenBabel I load a different list of tools options.

Print Folder Contents

I recently needed a list of all files in a particular folder, actually the folder contained sub-folders also containing files. This tedious task becomes trivial with a couple of UNIX commands and Applescript provides a convienient user interface and the glue between the UNIX commands.

Full details are here.

Lecture on Applescript and Unix

I gave a lecture at the Scientific computing meeting at Cambridge University on using Applescript to build a User interface for Unix command-line applications.
The presentation is available
here.
If anyone would like a copy of the slides let me know.