Scripting EndNote

I need to export references into BibTex, but EndNote does not create citation keys and BibTex will add random letters to a key so that keys are unique. That random nature leads me to believe that a future move of references from EndNote to BibTex will leave orphaned citations in older documents. While it is possible to use EndNote's Record Number as a citation key by modifying the Export template, that does not lead to human readable citation keys.

I attempted to use AppleScript, based on Endnotes documented examples. I just wanted to read the unformatted record, parse out the author name, year, and record number, create a string like Armstrong_1969_1, and place it in the label field. The Name_Year_RecNum combination will always be the same as long I always start from the same EndNote database. I attempted various incantations but kept running into error (-1728) indicating that AppleScript could not get the variable from EndNote and into a local variable. EndNote support didn't have an answer but forwarded my inquiry to the development team.

After some Googling I found Appscript on Sourceforge as a Python package and installed it. Appscript is no longer supported but was worth a try. I also found ASTranslate on Sourceforge that took my AppleScript code and returned code that Appscript needed. This was EXTREMELY helpful.

Here is a Python script that does what I described above. I hope others find it useful. And if someone wants to show me the magic for using AppleScript with EndNote, I can move this back to AppleScript.

Use at your own risk. I had to add spaces to make the code a little more readable in this blog. Be careful, Python is VERY sensitive to indentation.

# Python script to read the 'unformatted record' in EndNote and create# a citation key for EndNotes Label field.# The citation keys are unique since EndNotes record field is used.# The ciation keys are ready for import by BibTex## Bill Seufzer, Ph.D. NASA Langley Research Center

# bill dot seufzer at nasa dot gov# March 2013

from appscript import *import unicodedata

en = app('Endnote X6')

# retrieve the list of 'shown' references# if a group is selected, that is the shown list of recordstheListOfRefs = en.retrieve(u'shown', records_in=en.documents[1])

for aRef in theListOfRefs: # get the unformatted record unf_rec = en.unformatted_record(aRef) # unf_rec will look like {Armstrong, 1969 #1} # find the positions of the comma, #, and the } comPos=unf_rec.find(',') hashPos=unf_rec.find('#') end=unf_rec.find('}') author=unf_rec[1:comPos] #remove any spaces from the authors name and replace with '_' # BibTex does not like spaces in the citation key spaces=author.count(' ') if spaces > 0: author=author.replace(' ','_') if len(author) == 0: author='No_Author' year=unf_rec[comPos+2:hashPos-1] if len(year) == 0: year='xxxx' rec_num=unf_rec[hashPos+1:end]