Blog

Signals Blog

ChemBot: Batch Convert ChemDraw CDX Files Into PNG Images Using AppleScript

If you've ever worked with a large collection of ChemDraw™ (.cdx) files, you may have needed to convert them into images for publication on the web or elsewhere. You may have concluded that the only way to do this would be by through liberal application of elbow grease, or finding someone willing to do it.

If you're working on a Mac, I have good news for you. As part of an iPhone chemistry application my company is helping to develop, we needed to convert a large number of CDX files into PNGs. Read on to find out how we automated this process and saved a bunch of time.

Downloading and Running ChemBot

ChemBot is a small Mac application written in AppleScript. It can be downloaded from GitHub, and the source code is also available there. ChemBot requires ChemDraw Ultra 12.

To run ChemBot:

  1. Open ChemDraw Ultra 12.0 or later.
  2. Double-click on the ChemBotApp icon and follow the prompts.

Faster execution is possible by launching ChemBot through the AppleScript editor:

  1. Open ChemDraw Ultra 12.0 or later.
  2. Double-click on the ChemBotScript icon.
  3. Click the "Run" button and follow the prompts.

How It Works

ChemBot is written in AppleScript, Apple's built-in application scripting language for Mac. Although conceptually a very simple piece of software, one of the hard things about creating ChemBot was the surprising scarcity of real-world AppleScript examples.

Our starting point was Chris Swain's excellent AppleScript tutorial series. One of his tutorials proved especially useful: Convert ChemDraw to TIFF. Although it wasn't quite what we were looking for, it was a start.

After some experimentation, we hit a major problem. Although we could get ChemDraw to open CDX files automatically and recurse folders, we couldn't get PNG files to be saved. Chris Swain very quickly came back with an answer to my email: ChemDraw apparently can only save images files to an existing alias. Attempting to save to a filename that doesn't exist won't work.

Making the needed change allowed us to finish ChemBot and solve the problem of automatically converting CDX files into PNG images.

Source Code

ChemBot is distributed under the MIT License. For completeness sake, here is the source code:

my process_folder(choose folder with prompt "ChemBot - Choose a Folder to Make PNGs from CDX")

on process_folder(root_folder)
  my create_images(root_folder)

  tell application "Finder"
    set subfolders to every folder in root_folder

    repeat with i from 1 to number of items in subfolders
      set subfolder to (item i of subfolders)

      my process_folder(subfolder)
    end repeat
  end tell
end process_folder

on create_images(root_folder)
  tell application "Finder"
    set fileset to every file in root_folder

    repeat with i from 1 to number of items in fileset
      set filename to item i of fileset as text

      if filename ends with ".cdx" then
        my cdx_to_png(filename)
      end if
    end repeat
  end tell
end create_images

on cdx_to_png(filename)
  set slug to text 1 thru -5 of filename as text
  set cdx_path to slug & ".cdx"
  set png_path to slug & ".png"

  my create_alias(png_path)
  my chemdraw(cdx_path, png_path)
end cdx_to_png

-- Apparently, ChemDraw can only save to an alias, so we create one.
on create_alias(target_path)
  set open_target_file to open for access file target_path with write permission
  close access open_target_file
end create_alias

on chemdraw(cdx_path, png_path)
  tell application "CS ChemDraw Ultra"
    activate
    open (cdx_path as alias)
    save first document in (png_path as alias) as "PNG"
    close first document
  end tell
end chemdraw
 

Other Ways to Automate ChemDraw

Although I didn't find many other examples of ChemDraw automation, one worth noting is Rajarshi Guha's use of Python in ChemDraw scripting. This approach has the advantage of working on Windows. Finally, CambridgeSoft (now Perkin-Elmer) also offers a scripting language for ChemDraw called ChemScript.

Conclusions

AppleScript is a useful piece of technology, although good examples of its use are in short supply. In addition to solving our specific problem of converting CDX files into PNGs, ChemBot could be used as a starting point for more complex ChemDraw automation workflows.