Macs in Chemistry

Insanely great science

 

Scripting the Organic Chemistry Reference Resolver

The Organic Chemistry Reference Resolver is an invaluable web service that takes as input a text reference to a published article in a variety of formats and then finds and displays the corresponding publication. Recently a reader asked me about accessing it from the desktop rather than having to go to the website first.

chemresolver

The real beauty of a web service is that it can be accessed via a variety of means, including an applescript. This applescript uses as input either text that has been copied to the clipboard or the text the user has typed into a text box, it submits the request to the web service and displays the result in the web browser.

The first part of the script tidies up the text on the clipboard just in case the user has accidentally included any images in the copied text.

The next part displays the dialog box below, in which the displayed text is that copied to the clipboard. If the user clicks the “Use Clipboard” button the copied text is used, alternatively the user can type in the reference into the text box and use the appropriate button.

scriptbox

The text resulting is now encoded using a python script. Encoding is used when placing text in a query string to avoid it being confused with the URL itself. It is often used when the browser sends form data to a web server. URL Encoding replaces “unsafe” characters with ‘%’ followed by their hex equivalent.

With the text encoded we can now construct the URL (SearchURL) and open it with the default web browser.

--Uses http://chemsearch.kovsky.net
--Organic Chemistry Resolver

--ensure only text on clipboard
set the clipboard to «class ktxt» of ((the clipboard as text) as record)

set the_clip to the clipboard


display dialog "Search using Clipboard text " & return & the_clip default answer "" buttons {"Cancel", "Use Text Box", "Use Clipboard"} default button 3


copy the result as list to {TheTextToEncode, button_pressed}

if button_pressed is "Use Clipboard" then
  set TheTextToEncode to the_clip
    --display dialog TheTextToEncode

else if button_pressed is "Use text box" then
  --display dialog TheTextToEncode

end if

--encode the text to avoid characters not allowed in URLs
set encoded_reference to (do shell script "/usr/bin/python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of TheTextToEncode)


set SearchURL to "http://chemsearch.kovsky.net/index.php?q=" & encoded_reference


open location SearchURL
--Tested with
--Acc. Chem. Res. 2000, 33, 346
--10.1021/ja207775a
--10.1021/ci200304v

Whilst you could put the applescript anywhere I use the script menu, if you don't have this running go to the Applescript Editor preferences. You should get the dialog shown below, check the box "Show Script menu in menu bar", and the applescript icon should appear in the top right group of menu bar icons.

applescript_prefs-2-1

The applescript can be downloaded from here ReferenceLookup.scpt.zip

UPDATE

Brian Myers has modified the script so that what is on the clipboard is the default text when the script is launched.

screenshot2

This applescript can be downloaded from here ReferenceResolver.scpt.zip

Last Updated 22 May 2017