Evernoteのノート間リンクを削除するApplescript

Evernoteでノート間リンクを貼るApplescript - 蟲が知らせるの続き。
設定済みのリンクを削除するApplescriptも組んだので貼っておきます。
使い方は、リンク解除したいノートのペアを両方開いておいてApplescriptを実行するだけ。

-- Internote Link書式:
-- ***Evernote Link Header***>>>{refTo list}***<<<{refFrom list}***
on run
	tell application "Evernote"
		activate
		if 3 > (count of windows) then
			display dialog "最低2つのノートウィンドウが必要です。" buttons {"OK"} default button 1
		end if
		set w1 to name of window 1
		set note1 to note 1 of window w1
		set lid1 to local id of note1
		set w2 to name of window 2
		set note2 to note 1 of window w2
		set lid2 to local id of note2
	end tell
	-- idを抽出
	set id1 to last item of split(lid1, "/p")
	set id2 to last item of split(lid2, "/p")
	
	set linkDeclaration to "***Evernote Link Header***"
	
	set str1 to getStr(w1, linkDeclaration)
	set notDeclared1 to "" is str1
	if notDeclared1 then
		display dialog "リンクはありません" buttons {"OK"} default button 1
		return
	else
		set refTo1 to getRefTo(str1)
		set refFrom1 to getRefFrom(str1)
	end if
	set str2 to getStr(w2, linkDeclaration)
	set notDeclared2 to "" is str2
	if notDeclared2 then
		display dialog "リンクはありません" buttons {"OK"} default button 1
		return
	else
		set refTo2 to getRefTo(str2)
		set refFrom2 to getRefFrom(str2)
	end if
	-- link headerを作成
	set refTo1 to remove(refTo1, id2)
	set refFrom1 to remove(refFrom1, id2)
	set refTo2 to remove(refTo2, id1)
	set refFrom2 to remove(refFrom2, id1)
	if length of refTo1 is 0 and length of refFrom1 is 0 then
		deleteHeader(w1)
	else
		setHeader(w1, refTo1, refFrom1, notDeclared1, linkDeclaration)
	end if
	if length of refTo2 is 0 and length of refFrom2 is 0 then
		deleteHeader(w2)
	else
		setHeader(w2, refTo2, refFrom2, notDeclared1, linkDeclaration)
	end if
end run

on deleteHeader(wName)
	delay 0.2
	tell application "System Events" to tell process "Evernote"
		tell menu item wName of menu 1 of menu bar item "Window" of menu bar 1
			click
		end tell
		delay 0.2
		tell UI element 1 of scroll area 1 of window wName
			key code 126 using command down
			delay 0.2
			key code 125 using shift down
			delay 0.2
			key code 51 -- delete
		end tell
	end tell
end deleteHeader
on setHeader(wName, ref1, ref2, notDeclared, declaration)
	set the clipboard to ""
	set str1 to concatp(ref1)
	set str2 to concatp(ref2)
	delay 0.2
	tell application "System Events" to tell process "Evernote"
		tell menu item wName of menu 1 of menu bar item "Window" of menu bar 1
			click
		end tell
		delay 0.2
		tell UI element 1 of scroll area 1 of window wName
			if notDeclared then
				set str to declaration & ">>>" & str1 & "***" & "<<<" & str2 & "***" & (ASCII character 13) & (ASCII character 13)
				set the clipboard to str
				delay 0.2
				keystroke "v" using command down
			else
				set str to declaration & ">>>" & str1 & "***" & "<<<" & str2 & "***"
				set the clipboard to str
				delay 0.2
				keystroke "v" using command down
			end if
		end tell
		delay 0.2
	end tell
end setHeader
on getStr(wName, linkDeclaration)
	set the clipboard to ""
	delay 0.2
	tell application "System Events" to tell process "Evernote"
		tell menu item wName of menu 1 of menu bar item "Window" of menu bar 1
			click
		end tell
		delay 0.2
		tell UI element 1 of scroll area 1 of window wName
			key code 126 using command down -- command-up: 先頭にカーソル移動
			delay 0.2
			key code 124 using {command down, shift down} -- 一行選択
			delay 0.2
			keystroke "c" using command down -- コビー
			delay 0.2
			set theLine to the clipboard
			if theLine contains linkDeclaration then
				-- 先頭行がheaderなら(リンクが定義済みなら)さらに3行選択し、カット
				return theLine
			else
				-- 始めてリンクを設定する場合は、カーソルを先頭へ
				key code 126 using command down
				return ""
			end if
		end tell
	end tell
end getStr

on getRefTo(str)
	set flat to item 3 of split(str, "***")
	if ">>>" is flat then
		return {}
	else
		return rest of split(characters 4 thru end of flat as text, "p")
	end if
end getRefTo
on getRefFrom(str)
	set flat to item 4 of split(str, "***")
	if "<<<" is flat then
		return {}
	else
		return rest of split(characters 4 thru end of flat as text, "p")
	end if
end getRefFrom

on remove(lst, itm)
	set res to {}
	repeat with i in lst
		if i as text is not itm then set res to res & i
	end repeat
	return res
end remove

on concatp(lst)
	set str to concat(lst, "p")
	if "" is str then
		return ""
	else
		return "p" & str
	end if
end concatp

on split(str, delim)
	--	try
	considering case
		set lastDelim to AppleScript's text item delimiters
		set AppleScript's text item delimiters to delim
		set spl to every text item of str
		set AppleScript's text item delimiters to lastDelim
	end considering
	return spl
	--	on error
	--		return ""
	--	end try
end split
on concat(lst, delim)
	set lastDelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to delim
	set conc to lst as string
	set AppleScript's text item delimiters to lastDelim
	return conc
end concat
on replace(str, repFrom, repTo)
	set spl to split(str, repFrom)
	if spl is "" then
		return str
	else
		return concat(spl, repTo)
	end if
end replace