Macs in Chemistry

Insanely great science

 

Scripting ChemBioDraw to convert cdx to png

One of the attractions of Applescript is that it helps with the automation of repetive tasks. I was recently asked if I could help with a script to convert ChemBioDraw .cdx files to .png, whilst you can open the file in ChemBioDraw and then choose "Save As" and then choose "PNG" from the dropdown menu this is rather laborious if you have a folder of files your want to convert. The script below automates this task, I actually wrote it as a droplet so you can drop a bunch of cdx files on it and it will convert all to PNG format saving all the new files in the same location as the original, keeping the same file name but changing the extension to .png. If you wan to see the display dialogs that I have commented out simply remove the "--" and recompile, it is perhaps easier to undertand what is happening.

With a list of source files the repeat loop takes each file and determines the file name, extension, type and kind. These are not all needed for this script but it is helpful to know what is what. The "trimmedname" is the file name without the extension, trimmedpath" is the full path minus the extension. We then construct the new file name and path. Since ChemBioDraw scripting only seems to support saving to an alias we create an empty file using writetofile.

Now we can tell ChemBioDraw to open the .cdx file and Save As "PNG".

on open source_files
  
    repeat with i from 1 to number of items in source_files
      set source_file to item i of source_files

        --display dialog source_file


        set file_name to name of (info for alias source_file)
        set file_extension to name extension of (info for alias source_file)
        set file_type to file type of (info for alias source_file)
        set file_kind to kind of (info for alias source_file)


        --If you want to see these results remove the "--" to see the dialog box
        --display dialog file_name
        --display dialog file_extension
        --display dialog file_type
        --display dialog file_kind

        set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name

        --display dialog trimmed_name

        set the trimmed_path to text 1 thru -((length of file_extension) + 2) of the (source_file as text)

        set new_name to trimmed_name & ".png"

        set new_path to trimmed_path & ".png"

        --display dialog new_name

        --display dialog new_path

        set myfile to new_path



        set myinfo to ""
        my write_to_file(myinfo, myfile, false)
        set target_path to myfile as alias


        tell application "CS ChemBioDraw Ultra"
          activate

            --display dialog source_file

            open (source_file as alias)

            save first document in (target_path) as "PNG"

            close first document
      end tell
  end repeat

end open


--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

It is perhaps easier to see the script in action.

ChemBioDraw 12.0 supports importing and exporting the file formats listed below all of which can be used in the script, you should note however not all retain the chemical information if imported back into ChemBioDraw.

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

Note apparently there is a bug in older versions of ChemBioDraw that prevents save to PNG, updating to the latest version solves the problem.

The droplet can be downloaded from here scriptPNGapp.zip.