Macs in Chemistry

Insanely great science

 

Copy File to Dropbox Folder and Create email message containing link.

A while back I posted a script that copied a file to a users MobilMe Public folder and created an email link to that file. With the imminent demise of MobileMe a couple of people have asked me for a script that does the same but uses Dropbox. I thought the easiest way to demonstrate this was to modify the previous script used to email chemical structures.

The Dropbox Public Link

For this script to function we need to be able to generate the link to a file in your Dropbox Public folder, the easiest way to get an example of the format of the URL is to put a file in your Dropbox Public folder then right-click (or control-click) on the file and navigate down to “Dropbox” and then follow the submenu to “Copy Public Link”. If you now paste that into the script editor it should be something like this

http://dl.dropbox.com/u/12333897/temp.cdx

Where “12333897” will be replaced by your unique identifier and we can create a URL in this format to any new file saved in the Dropbox Public folder.

dropbox

The first part of the script creates the path to the Dropbox Public folder, this will be used by the script to save the file in the correct place.

--property target_path : path to desktop as string --if you want to keep original files
set target_path to (path to home folder) as string
set target_path to target_path & "Dropbox:Public:"

We then construct the URL, you will need to replace the “yourID” with your unique identifier.

set filePath to "https://dl.getdropbox.com/u/yourID/“ & "temp.cdx"

The next part creates the files and file alias’

We then tell ChemBioDraw to save the frontmost document to the Dropbox Folder in both CDX and PNG format. I’ve started to modify the scripting of ChemBioDraw, instead of calling the application by name which means we need different versions of the script for the “Std”, “Pro” and “Ultra” versions of ChemBioDraw.

tell application "CS ChemBioDraw Ultra"

Instead we call the application by id

tell application id "com.cambridgesoft.ChemDraw"

Which I hope will mean that we only need one version of the script, let me know how this works. The remainder of the script is similar to the previous one, except that we embed a URL into the body of the main message.

The Script

--Created by Macs in Chemistry (http://www.macinchem.org)
--property target_path : path to desktop as string --if you want to keep original files
set target_path to (path to home folder) as string
set target_path to target_path & "Dropbox:Public:"

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

set filePath to "https://dl.getdropbox.com/u/YourID/“ & "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 id "com.cambridgesoft.ChemDraw"
  activate
    try
      
        if not enabled of command "copyAsSMILES" then
          do command "selectAll"

            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:"Dropbox link to structure with png format and SMILES
    " & theSMILES & " " & filePath}
    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

You can download the script from here mailDropbox.scpt.zip remember you will need to edit the script to insert your unique Dropbox identifier.

You will have noticed I also copy an image file to the dropbox folder, this means you can see it when browsing from your iPhone or iPad regular_smile