applescript chemdraw question

Hi
I was trying to write a script this evening which should open a
sdf file in Chemdraw perform 13C-NMR Shifts prediction on it and
then save the NMR result in the cdxml format (so that i can easily get out the shift information).
That is what I came up with:

on test()
set theFolder to choose folder
set theFile to (choose file with prompt "Select the file:") as alias
set the target_path to ((theFolder as string) & "test.cdxml") as alias
-- i made sure that test.cdxml exists in the target_path by touch test.cdxml

tell application "CS ChemDraw Ultra"
open theFile
do menu item "Select All" of menu "Edit"
do menu item "Predict 13C-NMR Shifts" of menu "Structure"
close second document
--display dialog target_path as text
save first document in target_path as "ChemDraw XML"
close first document
end tell
end test

The thing which is driving me crazy is that I am not able to save
the NMR Shift result in the CDXML format. It is possible to save
it in the TIFF, BMP or CDX format but not in CDXML or Postscript.
First i thought I got the as "FileType" String wrong so I tried all
kind of forms (such as cdxml cdXML ChemDrawXML ....).

Chemdraw takes all of them as an input and does not return an error as it
would when entering e.g. "BLABLA" but it always results in a binary CDX
file and not as suspected in a parse able cdxml file.
Doing it by hand works fine (but i'd like to do that for a lot of files that 's why I want to use the script).
So maybe one of you has a clue why that thing is not working? Another possibility would be to use a opensource NMR shift predictor, maybe some of you know about where I can find such a software?

well i am grateful for any hints :)

best,
konrad

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Try ChemDraw support?

I have often received excellent responses from ChemDraw support (http://www.cambridgesoft.com/services/DesktopSupport/.) It might simply be a bug they can fix in the next release.

As far as open source NMR predictors, there isn't much. All I know is NMRShiftDB and I think there are a few other NMR databases. At least at one point, there was a way to get prediction (via HOSE) from the NMRShiftDB, but I don't know if that's still possible -- a little poking around didn't find much.

Save as

I've tried a few things and it looks like a bug, I've sent a message to their support outlining it. However like most companies their priorities are driven by the users so the more people who report it the sooner it will rise to the top of the pile.

NMR Prediction

xdrawchem http://xdrawchem.sourceforge.net/ also has NMR prediction but I don't have any experience of using it. Since it uses OpenBabel I wonder if the NMR prediction stuff could also be combined with OpenBabel?
Any thoughts Geoff?

Possible work around

set theFile to "Macintosh HD:Users:username:Desktop:test.cdxml"

tell application "CS ChemDraw Ultra"
activate
open theFile
do menu item "Select All" of menu "Edit"
do menu item "Predict 13C-NMR Shifts" of menu "Structure"

--display dialog "Here"

if enabled of menu item "Select All" then
do menu item "Select All"
end if

do menu item "Cut"
close first document saving no
do menu item "Paste" of menu "Edit"
do menu item "Save" of menu "File"
close first document saving no
end tell

Beware this saves all the data into the original file! so you may want to have a copy. I've done no error testing so i don't know what will happen with files that don't give NMR data etc.

If this helps would you mind posting your complete solution when finished:-)

A better solution

This version first creates a blank cdxml file (the \ before all the quotes are needed to tell applescript to ignore it), it then opens theFile generates all the NMR data close all open windows then opens the created cdxml file and saves the NMR data into it.

I've hard-coded paths and file names but you should be able to generate unique names for them within the script.

set theFile to "Macintosh HD:Users:username:Desktop:test.cdxml"
set the_cdxml_File to "Macintosh HD:Users:usename:Desktop:New.cdxml"
set cdxml_file_text to "

CreationProgram=\"ChemDraw 10.0\"
Name=\"Untitled untitled-6\"
BoundingBox=\"0 0 0 0\"
WindowPosition=\"0 0\"
WindowSize=\"0 0\"
FractionalWidths=\"yes\"
InterpretChemically=\"yes\"
ShowAtomQuery=\"yes\"
ShowAtomStereo=\"no\"
ShowAtomEnhancedStereo=\"yes\"
ShowAtomNumber=\"no\"
ShowBondQuery=\"yes\"
ShowBondRxn=\"yes\"
ShowBondStereo=\"no\"
ShowTerminalCarbonLabels=\"no\"
ShowNonTerminalCarbonLabels=\"no\"
HideImplicitHydrogens=\"no\"
LabelFont=\"21\"
LabelSize=\"12\"
LabelFace=\"96\"
CaptionFont=\"20\"
CaptionSize=\"12\"
HashSpacing=\"2.7\"
MarginWidth=\"2\"
LineWidth=\"1\"
BoldWidth=\"4\"
BondLength=\"30\"
BondSpacing=\"12\"
ChainAngle=\"120\"
LabelJustification=\"Auto\"
CaptionJustification=\"Left\"
PrintMargins=\"36 36 36 36\"
MacPrintInfo=\"00030000004800480000000003290232FFF0FFF0033A02430367057B03E000020000004800480000000002D802280001000000640000000100030303000000017FFF000100010000000000000000000000006808001901900000000000200000000000000000000000000000000000000000000000000000\"
>












id=\"5\"
BoundingBox=\"0 0 522 769\"
HeaderPosition=\"36\"
FooterPosition=\"36\"
PrintTrimMarks=\"yes\"
HeightPages=\"1\"
WidthPages=\"1\"
/>"

my write_to_file(cdxml_file_text, the_cdxml_File, true)

tell application "CS ChemDraw Ultra"
activate
open theFile
do menu item "Select All" of menu "Edit"
do menu item "Predict 13C-NMR Shifts" of menu "Structure"

--display dialog "Here"

if enabled of menu item "Select All" then
do menu item "Select All"
end if

do menu item "Cut"
close first document saving no
close first document saving no
open the_cdxml_File
do menu item "Paste" of menu "Edit"
do menu item "Save" of menu "File"
close first document saving no
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
return true
on error
try
close access file target_file
end try
return false
end try

end write_to_file

NMR Prediction from NMRShiftDB

XDrawChem is no longer actively developed, from the last time I talked with the author. I do know there are a few new open source programs in the works.

The question here was about NMR prediction -- XDrawChem uses the CDK and NMRShiftDB HOSE code and database for NMR prediction. So it's not any different from what I posted above (i.e., it's not the same quality as ChemDraw.)

Until there's a more accurate database for NMR prediction, I'm not sure I'd encourage it to be used in Open Babel.

my version for more than 1 set of files

Hi,
thanks a lot for your help. The workaround was a really great idea.
Now my script (incorporating your code) can process many sdf files in a given directory (see the script attached below).

best,
konrad

------------------------
getCDXML()

on getCDXML()
set cdxml_file_text to "

CreationProgram=\"ChemDraw 10.0\"
Name=\"Untitled untitled-6\"
BoundingBox=\"0 0 0 0\"
WindowPosition=\"0 0\"
WindowSize=\"0 0\"
FractionalWidths=\"yes\"
InterpretChemically=\"yes\"
ShowAtomQuery=\"yes\"
ShowAtomStereo=\"no\"
ShowAtomEnhancedStereo=\"yes\"
ShowAtomNumber=\"no\"
ShowBondQuery=\"yes\"
ShowBondRxn=\"yes\"
ShowBondStereo=\"no\"
ShowTerminalCarbonLabels=\"no\"
ShowNonTerminalCarbonLabels=\"no\"
HideImplicitHydrogens=\"no\"
LabelFont=\"21\"
LabelSize=\"12\"
LabelFace=\"96\"
CaptionFont=\"20\"
CaptionSize=\"12\"
HashSpacing=\"2.7\"
MarginWidth=\"2\"
LineWidth=\"1\"
BoldWidth=\"4\"
BondLength=\"30\"
BondSpacing=\"12\"
ChainAngle=\"120\"
LabelJustification=\"Auto\"
CaptionJustification=\"Left\"
PrintMargins=\"36 36 36 36\"
MacPrintInfo=\"00030000004800480000000003290232FFF0FFF0033A02430367057B03E000020000004800480000000002D802280001000000640000000100030303000000017FFF000100010000000000000000000000006808001901900000000000200000000000000000000000000000000000000000000000000000\"
>












id=\"5\"
BoundingBox=\"0 0 522 769\"
HeaderPosition=\"36\"
FooterPosition=\"36\"
PrintTrimMarks=\"yes\"
HeightPages=\"1\"
WidthPages=\"1\"
/>"
set theFolder to choose folder --folder that contains the sdf files
set ext to "sdf"
tell application "CS ChemDraw Ultra"
activate
end tell
tell application "Finder"
try
set the datlist to ¬
((every file in theFolder whose name ends with "sdf") as alias) ¬
as list -- set name criteria
on error number -1700 from f
set datlist to f as list
end try
end tell
repeat with dat in datlist --list of alias references to the files
--first a corresponding target cdxml file has to be created
tell application "Finder"
set file_name to the name of dat as text
set theOffset to offset of "sdf" in file_name
set chopped to characters 1 through (theOffset - 1) of file_name
set directory to the container of dat as text
set newName to chopped & "cdxml" as text
set targetFile to directory & newName as text
end tell
write_to_file(cdxml_file_text, targetFile, true)
set alias_target to targetFile as alias
get_nmr(dat, alias_target)
end repeat
end getCDXML

on get_nmr(theFile, the_cdxml_File)
tell application "CS ChemDraw Ultra"
open theFile
do menu item "Select All" of menu "Edit"
do menu item "Predict 13C-NMR Shifts" of menu "Structure"

if enabled of menu item "Select All" then
do menu item "Select All"
end if
do menu item "Cut"
close first document saving no
close first document saving no
open the_cdxml_File
do menu item "Paste" of menu "Edit"
do menu item "Save" of menu "File"
close first document saving no
end tell
end get_nmr

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
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file

ChemDraw Applescript

Very nice.