如何用AppleScript提取Word中选中的Zotero域代码文本
从Word引文跳转Zotero的Automator工作流:用AppleScript替代VBA提取域代码
我正在制作Automator工作流,实现从Word中的引文跳转至参考文献管理软件Zotero,但找不到可用的AppleScript来提取选中的域代码文本(这是第一步)。目前用VBA实现了提取,但VBA会修改文件,希望用AppleScript替代。
Word中的Zotero域代码示例
ADDIN ZOTERO_ITEM CSL_CITATION {"citationID":"AFUiwuqi","properties":{"formattedCitation":"[1]","plainCitation":"[1]","noteIndex":0},"citationItems":[{"id":9752,"uris":["http://zotero.org/users/6410528/items/YYTRWPHH"],"itemData":{"id":9752,"type":"article-journal","container-title":"Nature","DOI":"10.1038/s41586-019-1737-7","ISSN":"0028-0836, 1476-4687","issue":"7782","page":"324-329","title":"Controlled flight of a microrobot powered by soft artificial muscles","volume":"575","author":[{"family":"Chen","given":"Yufeng"},{"family":"Zhao","given":"Huichan"},{"family":"Mao","given":"Jie"},{"family":"Chirarattananon","given":"Pakpong"},{"family":"Helbling","given":"E. Farrell"},{"family":"Hyun","given":"Nak-seung Patrick"},{"family":"Clarke","given":"David R."},{"family":"Wood","given":"Robert J."}],"issued":{"date-parts":[["2019",11,14]]}}}],"schema":"https://github.com/citation-style-language/schema/raw/master/csl-citation.json"}
完整工作流流程
- 提取Word中选中的域代码文本(待解决的核心问题)
- 提取
uris字段中的链接(如http://zotero.org/users/6410528/items/YYTRWPHH) - 提取其中的itemKey(如
YYTRWPHH) - 打开Zotero跳转链接:
zotero://select/library/items?itemKey=YYTRWPHH
现有VBA实现(会修改文件)
Sub GetFiledsCodes() Dim myRange As Range, myCodes As String Set myRange = Selection.Range With myRange If .Fields.Count = 0 Then MsgBox "No Code!", vbInformation Exit Sub Else .Fields.Update .TextRetrievalMode.IncludeFieldCodes = True .TextRetrievalMode.IncludeHiddenText = True myCodes = .Text myCodes = VBA.Replace(myCodes, Chr(19), "{") myCodes = VBA.Replace(myCodes, Chr(21), "}") .SetRange .End, .End .InsertAfter myCodes .Font.Name = "Times New Roman" .Font.Size = 12 .Cut End If End With End Sub
替代的AppleScript提取方案(不修改文件)
以下AppleScript可直接提取选中区域的域代码文本,无需修改原文档,直接复制到剪贴板:
on run {input, parameters} tell application "Microsoft Word" activate set selectedRange to selection range if count of fields of selectedRange is 0 then display dialog "未选中任何域代码" buttons {"确定"} default button 1 return input end if -- 临时保存原文本检索设置 set includeFieldCodesOriginal to include field codes of text retrieval mode of selectedRange set includeHiddenTextOriginal to include hidden text of text retrieval mode of selectedRange -- 开启域代码和隐藏文本检索 set include field codes of text retrieval mode of selectedRange to true set include hidden text of text retrieval mode of selectedRange to true -- 获取并处理域代码文本 set fieldCodeText to text of selectedRange set fieldCodeText to replace fieldCodeText using find text (ASCII character 19) replace with "{" set fieldCodeText to replace fieldCodeText using find text (ASCII character 21) replace with "}" -- 恢复原设置 set include field codes of text retrieval mode of selectedRange to includeFieldCodesOriginal set include hidden text of text retrieval mode of selectedRange to includeHiddenTextOriginal -- 复制结果到剪贴板 set the clipboard to fieldCodeText end tell return input end run
调整后的Automator工作流
- 运行上述AppleScript:替代原有的VBA调用步骤,直接提取域代码到剪贴板
- 从剪贴板获取内容
- 从文本中提取URL
- 筛选以
http://zotero.org/users/开头的段落 - 复制到剪贴板
- 运行原有的Zotero跳转AppleScript:
set myStr to do shell script "pbpaste" tell application "Zotero" to activate set AppleScript's text item delimiters to linefeed set myList to every text item of myStr set codes to "" repeat with itemValue in myList if itemValue starts with "http://zotero.org/users/" then set zoterocode to (do shell script "sed -E 's#http://zotero.org/users/[0-9]+/items/##g' <<< " & quoted form of itemValue) if codes is "" then set codes to zoterocode else set codes to codes & "," & zoterocode end if end if end repeat tell application "System Events" key code 18 using {command down, control down, option down} delay 0.5 set collectionKey to do shell script "pbpaste" if collectionKey = myStr then set theurl to "zotero://select/library/items?itemKey=" & codes else set theurl to collectionKey & "/items?itemKey=" & codes end if open location theurl end tell
内容的提问来源于stack exchange,提问作者heihei Han
相关产品推荐
相关产品推荐

