Hook to Endnote Articles

Currently, there is no way to Hook to individual references in Endnote. Anyone know a way that this could be done?

Thanks!

last time we checked Endnote lacked automation.

If Endnote has comments fields, you could paste Hook-garnered links in them.

I implemented this on endnote 20: GitHub - aitsc/endnote-hook

1 Like

Thank you for implementing this!

We will integrate it into Hook.

Welcome to the Hook Productivity Forum , @tsc. Thanks for sharing!

We’ll need to replace the python3 call with some ObjectiveC since Apple unfortunately removed python.

We’ve modified the New Item script because macOS does not have Python anymore

could EndNote users please let us know if the following works for them?

# given hook://endnote/?dn=1-1954,1-2093&l=SCI|Area 3&or=false
# Where dn represents the number of the database opened (starting from 1) - the number of the paper #, ..
# l represents the label to be retrieved, different words are separated by vertical bars |, or if true, it means that the relationship between different words is or for retrieval, the default is false and the relationship
# will not encode the url itself, if dn has a value, the l parameter will be ignored


on path2url(thepath)
	tell current application's NSString to set urlString to stringWithString_(thepath)
	set theDecodedURL to urlString's stringByRemovingPercentEncoding -- 4 is NSUTF8StringEncoding
	return theDecodedURL as Unicode text
end path2url

set fullURL to path2url("$0")

on theSplit(theString, theDelimiter)
	set oldDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	set theArray to every text item of theString
	set AppleScript's text item delimiters to oldDelimiters
	return theArray
end theSplit

on intersection(list_list) # 多个list求交集
	local newList, listB, a
	set newList to item 1 of list_list
	repeat with listA in list_list
		set listB to {}
		repeat with a in listA
			set a to contents of a -- dereference implicit loop reference
			if {a} is in newList then set end of listB to a
		end repeat
		set newList to listB
	end repeat
	return newList
end intersection

on union(list_list) # 多个list求并集, 一个list内部重复的元素会去除
	local listA, listB
	set listA to {}
	repeat with listB in list_list
		set listA to listA & listB
	end repeat
	set listB to {}
	repeat with a in listA
		set a to contents of a -- dereference implicit loop reference
		if {a} is not in listB then set end of listB to a
	end repeat
	return listB
end union

set l_or to "false"
if (count of text items of fullURL) is greater than 1 then
	set fragment to item 2 of theSplit(fullURL, "?")
	set AppleScript's text item delimiters to "&"
	set kvs to text items of fragment
	repeat with kv in kvs
		set AppleScript's text item delimiters to "="
		set k to text item 1 of kv
		set v to text item 2 of kv
		if k = "dn" then
			set database_recNum to v
		end if
		if k = "l" then
			set Label to theSplit(v, "|")
		end if
		if k = "or" then
			set l_or to v
		end if
	end repeat
end if

if database_recNum ≠ "" then
	set myResults to {}
	repeat with dr in theSplit(database_recNum, ",")
		set database to item 1 of theSplit(dr, "-")
		set recNum to item 2 of theSplit(dr, "-")
		set end of myResults to "<RecordID database=\"" & database & "\" recNum=\"" & recNum & "\" />"
	end repeat
else
	tell application "EndNote 20"
		set myResults to {}
		repeat with l in Label
			set end of myResults to find l in field "Label"
		end repeat
	end tell
	if l_or = "true" then
		set myResults to union(myResults)
	else
		set myResults to intersection(myResults)
	end if
end if

tell application "EndNote 20"
	activate
	show records in myResults in first window
end tell

error "“NSString” does not understand the “stringWithString_” message. " number -1708 from NSString

1 Like

Sorry. Here is the modified Open item script:

use framework "Foundation"
# 链接例子为 hook://endnote/?dn=1-1954,1-2093&l=SCI|3区&or=false
# 其中dn表示 打开的数据库编号(从1开始)-论文的编号#,..
# l表示检索的Label, 不同词使用竖线|分割, or如果是true则表示不同词是或的关系进行检索, 默认为false是且的关系
# url不要自己encode编码, dn如果有值则会忽略l参数

on path2url(thepath)
	tell current application's NSString to set urlString to stringWithString_(thepath)
	set theDecodedURL to urlString's stringByRemovingPercentEncoding -- 4 is NSUTF8StringEncoding
	return theDecodedURL as Unicode text
end path2url

set fullURL to path2url("$0")

on theSplit(theString, theDelimiter)
	set oldDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	set theArray to every text item of theString
	set AppleScript's text item delimiters to oldDelimiters
	return theArray
end theSplit

on intersection(list_list) # 多个list求交集
	local newList, listB, a
	set newList to item 1 of list_list
	repeat with listA in list_list
		set listB to {}
		repeat with a in listA
			set a to contents of a -- dereference implicit loop reference
			if {a} is in newList then set end of listB to a
		end repeat
		set newList to listB
	end repeat
	return newList
end intersection

on union(list_list) # 多个list求并集, 一个list内部重复的元素会去除
	local listA, listB
	set listA to {}
	repeat with listB in list_list
		set listA to listA & listB
	end repeat
	set listB to {}
	repeat with a in listA
		set a to contents of a -- dereference implicit loop reference
		if {a} is not in listB then set end of listB to a
	end repeat
	return listB
end union

set l_or to "false"
if (count of text items of fullURL) is greater than 1 then
	set fragment to item 2 of theSplit(fullURL, "?")
	set AppleScript's text item delimiters to "&"
	set kvs to text items of fragment
	repeat with kv in kvs
		set AppleScript's text item delimiters to "="
		set k to text item 1 of kv
		set v to text item 2 of kv
		if k = "dn" then
			set database_recNum to v
		end if
		if k = "l" then
			set Label to theSplit(v, "|")
		end if
		if k = "or" then
			set l_or to v
		end if
	end repeat
end if

if database_recNum ≠ "" then
	set myResults to {}
	repeat with dr in theSplit(database_recNum, ",")
		set database to item 1 of theSplit(dr, "-")
		set recNum to item 2 of theSplit(dr, "-")
		set end of myResults to "<RecordID database=\"" & database & "\" recNum=\"" & recNum & "\" />"
	end repeat
else
	tell application "EndNote 20"
		set myResults to {}
		repeat with l in Label
			set end of myResults to find l in field "Label"
		end repeat
	end tell
	if l_or = "true" then
		set myResults to union(myResults)
	else
		set myResults to intersection(myResults)
	end if
end if

tell application "EndNote 20"
	activate
	show records in myResults in first window
end tell
1 Like

Thanks again, @tsc.

@tsc and/or other Endnote users: Does the modified version work for you?

I just started getting interested in possibilities with Hookmark and gave this a try. It is some time later since these posts were first made, so I’m using Endnote 21—I didn’t remember to update the version number in the script but there was a mac prompt that asked me where the app was and so I just pointed to the correct place and it worked (I updated it later in the scripts too).

It seems like the hook works as expected - you can select a single reference or multiple references, and when you open the link it will return exactly what was selected before. One thing that might be interesting to update in the script is the link title so it doesn’t just say “en”. There seem to be ways to extract reference fields (i.e., author) using AppleScipt, but this would break the use case of hooking multiple references at the same time.

Also, based on the way Endnote libraries are compiled, this likely won’t work on different computers or if you update your library (the reference numbers in Endnote aren’t persistent in that way).

Welcome to Hookmark forum, @andrew_m_j .

I am not familiar with EndNote. How can I update library so the reference numbers will change?

I change the script so the url is not depending on the order of how the libraries are opened. The url will include the file path to the library. So the drawback of it is you can’t rename or move the library file.

Normally, how many libraries a user have? If most users will have 1 library, then better use the script in previous post.

Open Item Script
use framework "Foundation"
use scripting additions


on urlDecode(input)
	tell current application's NSString to set urlString to stringWithString_(input)
	set theDecodedURL to urlString's stringByRemovingPercentEncoding -- 4 is NSUTF8StringEncoding
	return theDecodedURL as Unicode text
end urlDecode

set fullURL to "$0"

set AppleScript's text item delimiters to "?"
set urlPath to text item 1 of fullURL
if (count of text items of fullURL) is greater than 1 then
	set fragment to text item 2 of fullURL
	set AppleScript's text item delimiters to "&"
	set kvs to text items of fragment
	repeat with kv in kvs
		set AppleScript's text item delimiters to "="
		set k to text item 1 of kv
		set v to text item 2 of kv
		if k = "rn" then
			set rn to v
		end if
		if k = "fp" then
			set fp to v
		end if
		
	end repeat
end if


set fpath to urlDecode(fp)
set text item delimiters to "/"
set fn to text item -1 of fpath
set len to length of fpath
set fpath to text 8 through len of fpath
set targetWindow to missing value

tell application id "com.ThomsonResearchSoft.EndNote"
	activate
	repeat with i from (count of windows) to 1 by -1
		try
			set thisWindow to window i
			set thisDoc to file of document of thisWindow
			
			set thisDoc to POSIX path of thisDoc
			if thisDoc is not missing value and thisDoc is equal to fpath then
				set targetWindow to thisWindow
				if index of targetWindow is not 1 then
					set index of targetWindow to 1
				end if
				exit repeat
			end if
		end try
	end repeat
	
	if targetWindow is missing value then
		open fpath
		set targetWindow to first window
	end if
	
	set re to item 1 of (retrieve "all" records in first document)
	
	
	tell application "System Events"
		set xmlDoc to make new XML data with properties {text:re}
		set dn to value of XML attribute "database" of XML element "RecordID" of xmlDoc
	end tell
	show records in {"<RecordID database=\"" & dn & "\" recNum=\"" & rn & "\" />"} in first window
end tell
Get Address Script
use framework "Foundation"

property NSURL : a reference to current application's NSURL
tell application  id "com.ThomsonResearchSoft.EndNote"
	
	set f to file of first document
	
	set myPath to POSIX path of f
	
	set rawURL to NSURL's fileURLWithPath:myPath
	set f to rawURL's absoluteString
	
	set myResults to retrieve of "selected" records in first document
	
	set r to item 1 of myResults
	set en to field "Title" of record r
	
	tell application "System Events"
		set xmlDoc to make new XML data with properties {text:r}
		set rn to value of XML attribute "recNum" of XML element "RecordID" of xmlDoc
	end tell
	
	return "[" & en & "](endnote://?rn=" & rn & "&fp=" & f & ")"
	
end tell

Set scheme name: