Created
May 6, 2016 08:43
-
-
Save yankchina/869efcd752115561aba77b10bbcb4d10 to your computer and use it in GitHub Desktop.
自动在 Endnote X7 For Mac 上为每个文献条目增加一个Label
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
""" | |
filename: zhcn_endnote_label_generator | |
create datetime: 2016-05-06 | |
author: yankchina@gmail.com | |
version: 1.0 | |
description: > | |
- 自动获取 Endnote X7 For Mac 正在显示的列表,然后根据中文第一作者名的汉语拼音与年月增加 Label | |
- @see http://community.thomsonreuters.com/t5/EndNote-General/Scripting-EndNote/td-p/38941 | |
todo: | |
- | |
""" | |
from appscript import * | |
import unicodedata | |
from pypinyin import pinyin | |
def get_author_py(author): | |
author_py_list = pinyin(author,0) | |
author_py = u'' | |
# snake_case | |
for char in author_py_list: | |
author_py = u'{0}_{1}'.format(author_py,char[0]) | |
# convert snake_case to camel_case | |
author_py = author_py.title().replace(u'_','') | |
return author_py | |
pass | |
def format_year(year): | |
if len(year) == 0: | |
return u'xxxx' | |
return year | |
def format_author(author): | |
# get pinyin string from chinese author name | |
author = get_author_py(author) | |
# remove any spaces from the authors name and replace with '_' | |
spaces = author.count(u' ') | |
if spaces > 0: | |
author = author.replace(u' ',u'_') | |
if len(author) == 0: | |
author = 'None' | |
return author | |
def AddLabel(app,file_group_type=u'shown'): | |
theListOfRefs = app.retrieve(file_group_type,records_in=app.documents[1]) | |
for aRef in theListOfRefs: | |
# get the unformatted record | |
unf_rec = app.unformatted_record(aRef) | |
# unf_rec like: {吴吉义, 2011 #10} | |
# find the positions of the comma, #, and the } | |
comPos = unf_rec.find(u',') | |
hashPos= unf_rec.find(u'#') | |
end = unf_rec.find(u'}') | |
author = unf_rec[1:comPos] # get author name | |
year = unf_rec[comPos+2:hashPos-1] # get year | |
number = unf_rec[hashPos+1:end] #get reference number | |
author = format_author(author) | |
year = format_year(year) | |
cite_key = u'{0}_{1}_{2}'.format(author,year,number) | |
# this flattens the citation key into ascii | |
cite_key = unicodedata.normalize('NFKD',cite_key).encode('ascii','ignore') | |
# put the cite key into EndNote Label field | |
app.set_field('Label',to=cite_key,of_record=aRef) | |
pass | |
if __name__ == '__main__': | |
endnote = app('Endnote X7') | |
AddLabel(endnote) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment