Macs in Chemistry

Insanely great science

 

Multi SMILES to Chemdraw files

A recent tweet https://twitter.com/janhjensen/status/1145639870318174208 got me interested.

tweet

You could of course just and paste as SMILES but this would be very tedious, this is an ideal task for Applescript. The first part of the script simply defines the location of the folder where the resulting ChemDraw files will be stored. In this case in the home folder, you can change this to desktop by editing the script

set target_path to (path to desktop) as string

We then allow the user to choose the file containing the SMILES strings and read them in, and calculate how many lines there are. The next part loops through each line and copies the SMILES string to the clipboard, we then create a unique file name for each structure. Then scripting ChemDraw menus, to paste as smiles, and then save as ChemDraw with the unique filename, finally clearing the document to allow pasting of the next SMILES string.

The Script

set target_path to (path to home folder) as string
set target_folder to target_path & "SMILES2CD:"


set this_file to choose file

set smiList to (read this_file as «class utf8» using delimiter linefeed) as list

set smiLen to length of smiList


repeat with i from 1 to smiLen
  
    set mySMILES to item i of smiList as text
    --display dialog mySMILES

    set the clipboard to {text:(mySMILES as string), Unicode text:mySMILES}

    set file_name to "temp" & i & ".cdx"
    set my_file to target_folder & file_name
    --display dialog my_file

    tell application "ChemDraw 18.0"
      activate

        if enabled of menu item "Paste" then do menu item "SMILES" of menu "Paste Special" of menu "Edit"

        save first document in (my_file) as "ChemDraw"

        do menu item "Cut"

  end tell

end repeat

You can download the script here http://macinchem.org/applescript/Applescripts/SMILES2CD.zip

Last Updated 2 July 2019