Previewで開いているPDFをPapers3にインポートするAppleScript

我ながら恐ろしく便利なプラグインを作ってしまった。
⌘+\で起動するように設定した。

tell application "System Events"
	tell process "Preview"
		set thefile to value of attribute "AXDocument" of window 1
	end tell
end tell

set thefile to replaceText(thefile, "%20", space)
set thefile to replaceText(thefile, "File://", "")
set newFile to POSIX file thefile

tell application "Papers"
	open file newFile
end tell

to replaceText(someText, oldItem, newItem)
	set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, oldItem}
	try
		set {itemList, AppleScript's text item delimiters} to {text items of someText, newItem}
		set {someText, AppleScript's text item delimiters} to {itemList as text, tempTID}
	on error errorMessage number errorNumber -- oops
		set AppleScript's text item delimiters to tempTID
		error errorMessage number errorNumber -- pass it on
	end try
	
	return someText
end replaceText