Macs in Chemistry

Insanely great science

 

Script to email Chemical Structures

A reader commented that they often emailed ChemBioDraw .cdx structures to colleagues who it turned out did not have a copy of ChemBioDraw to view them, whilst it is possible to convert the cdx file to other formats using OpenBabel they usually just ended up exchanging an image. They asked if there was a better solution and this is an ideal task for Applescript. The script assumes the structure you want to email is open in ChemBioDraw, it will create an email using Apple Mail containing the structure as a SMILES string, and embed the .cdx file together with an image in PNG format.

The following applescript first creates two temporary files in the temporary items folder, (if you want to keep the files then you might want to change the path and store them elsewhere) using a standard write to file subroutine, and then create alias’ to these files. The next part uses ChemBioDraw to generate the SMILES string and then save the file in both cdx and PNG format to the files in the temporary items folder.

tell application "CS ChemBioDraw Ultra"
  activate
    try
      if not (enabled of menu item "Copy") then
          do menu item "Select All" of menu "Edit"

            set theSMILES to SMILES of selection
      else
          
            set theSMILES to SMILES of selection
      end if

  end try

    save first document in (pngAttachment) as "PNG"
    save first document in (cdxAttachment) as "ChemDraw"


end tell
Finally “Mail” is activated and a new message created with
set theMessage to make new outgoing message with properties {visible:true, subject:"Molecular Structure", content:"Structure in cdx, png format and SMILES
    " & theSMILES}

In the code above the value for the visible property of the outgoing message is set to true. The reason for this is that, by default, Mail will create new outgoing messages without displaying them.

Finally the two files are embedded into the email message, if you are sending the files regularly to the same recipient you could also add a line like this to the script.


  tell theMessage
      make new to recipient at end of to recipients with properties {name:"John Doe", address:"jdoe@mac.com"}
  end tell

ChemBioDraw supports a wide variety of different file formats, shown in the table below, and of course the script can be modified to include alternative file types.

Format Import Export
ChemDraw XML (CDXML) YES YES
ChemDraw 3.5 (CHM) YES YES
ChemDraw 2.0 and ChemDraw 2.1 (CHM) YES NO
ChemDraw Template (CTP)a YES YES
ChemDraw Template Style Sheet (CTS)a YES YES
ChemDraw Stationary Pads (CDS) YES YES
Connection Table (CT)a YES YES
Chemical Markup Language (CML)a YES YES
Encapsulated PostScript (EPS) NO YES
Graphic Image Format (GIF) YES YES
ISIS (SKC, TGF, RXN)a YES YES
JCAMP (JDX, DX)a YES NO
JPEG (JPG, JPEG) YES NO
MDL V3000 MolFile (MOL) YES YES
MDL MolFile (MOL)a YES YES
MDL RGFile (RGF)a YES YES
Accelrys MolFile (MSM) YES YES
PICT YES YES
Portable Network Graphics (PNG) YES YES
Standard Molecular Data (SMD)a YES YES
Structure-Data file (SD)a YES YES
Galactic Industries (SPC)a YES NO
TIFF file (TIF) YES YES

a Available in ChemBioDraw Ultra 12.0, ChemDraw Ultra 12.0, and ChemDraw Pro 12.0 only

The Script



--property target_path : path to desktop as string --if you want to keep original files
property target_path : path to temporary items as string

set png_file to target_path & "temp.png"
set cdx_file to target_path & "temp.cdx"

set myinfo to ""
my write_to_file(myinfo, png_file, false)
set pngAttachment to png_file as alias

set myinfo to ""
my write_to_file(myinfo, cdx_file, false)
set cdxAttachment to cdx_file as alias


tell application "CS ChemBioDraw Ultra"
  activate
    try
      if not (enabled of menu item "Copy") then
          do menu item "Select All" of menu "Edit"

            set theSMILES to SMILES of selection
      else
          
            set theSMILES to SMILES of selection
      end if

  end try

    save first document in (pngAttachment) as "PNG"
    save first document in (cdxAttachment) as "ChemDraw"


end tell



tell application "Mail"
  
    set theMessage to make new outgoing message with properties {visible:true, subject:"Molecular Structure", content:"Structure in cdx, png format and SMILES
    " & theSMILES}
    tell content of theMessage
      make new attachment with properties {file name:pngAttachment} at after last paragraph
        make new attachment with properties {file name:cdxAttachment} at after last paragraph
  end tell
end tell


--this is just a standard write to file and could be simplified
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 as international text) to the open_target_file starting at eof
        close access the open_target_file
        return true
  on error
      try
          close access file target_file
      end try
        return false
  end try
end write_to_file

The script can be downloaded from here mailFiles.scpt.zip

It needs to be stored in:-

/Users/username/Library/Application Support/CambridgeSoft CS ChemOffice 2010/ChemDraw