How to create Markdown, RTF, HTML, MediaWiki, LaTeX, Mathematica links using Hook URLs to one or more selected items in Finder

The fact that the Hook URLs are persistent is a huge advantage and I can finally “manage” my files using links without worrying that they will break. My second brain or personal encyclopedia is a local MediaWiki installation but sometimes I need these links to be in another format. As a result I wrote an AppleScript in order to easily get links in various formats.

The files in Finder need to be selected from top to bottom (with no gaps) otherwise the script does not work properly. While the script is running Finder should always be in the foreground and no user interaction should happen. The links are sent to the clipboard at the end of the script.

The script is the following:

tell application "Finder"
	activate
	set the clipboard to ""
	set theLinks to ""
	set selectedItems to the selection
	if selectedItems is {} then error "Please select at least one file."
	set numberOfItems to count of selectedItems
	
	tell application "System Events"
		repeat (numberOfItems - 1) times
			key code 126 -- up arrow
			delay 0.5
		end repeat
	end tell
	
	set theType to chooseRefType({"Markdown", "MediaWiki", "RTF", "HTML", "LaTeX", "Mathematica"}) of me
	activate
	set indexItem to 1
	repeat numberOfItems times
		activate
		set itemName to name of item indexItem of the selectedItems
		delay 1
		--get the Hook URL
		tell application "System Events"
			--activate Hook
			keystroke space using {command down, shift down}
			delay 1
			--Copy Link 
			keystroke "c" using {command down}
			delay 1
			set HookURL to do shell script "pbpaste"
		end tell
		
		if theType is "RTF" then
			set the theLink to createLink(itemName, HookURL, "HTML") of me
			set theLinks to theLinks & theLink & "<br />"
		else
			set the theLink to createLink(itemName, HookURL, theType) of me
			set theLinks to theLinks & theLink & return
		end if
		
		tell application "System Events"
			key code 125 --down arrow
		end tell
		set indexItem to indexItem + 1
	end repeat
	
	tell application "System Events"
		key code 126 --up arrow
	end tell
	
	if theType is "RTF" then set the theLinks to createLink(itemName, theLinks, "RTF") of me
	set the clipboard to theLinks
	
end tell


on chooseRefType(linkType)
	set linkType to item 1 of (choose from list linkType with prompt "Choose the type of link:" default items {"Markdown"})
	return linkType
end chooseRefType

on createLink(linkText, linkURL, linkType)
	if linkType is "RTF" then
		set linkHTML to "<html>" & linkURL & "</html>"
		set the clipboard to linkHTML
		set theLink to do shell script "pbpaste | textutil -stdin -format html -convert rtf -stdout" as «class RTF »
	else if linkType is "Markdown" then
		set theLink to "[" & linkText & "](" & linkURL & ")"
	else if linkType is "MediaWiki" then
		set theLink to "[" & linkURL & " " & linkText & "]"
	else if linkType is "LaTeX" then
		set theLink to "\\href{" & linkURL & "}{" & linkText & "}"
	else if linkType is "HTML" then
		set theLink to "<a href=\"" & linkURL & "\">" & linkText & "</a>"
	else if linkType is "Mathematica" then
		set theLink to "Hyperlink[\"" & linkText & "\",\"" & linkURL & "\"]"
	end if
	return theLink
end createLink
1 Like