Insert link to (various apps) on macOS

Hi
I have a recurring problem that I would like to insert a link to an item in some other app, it is often this tiny bit of information that makes todos that much more actionable. The following Applescript inserts a link to the currently selected document in Finder, an email, a PDF, a Devonthink item etc. It will insert the link at the position of the cursor (if SB+ is the frontmost app) or dump it in today's journal in its own line under a top level heading "Inbox" which it will create if necessary (if the frontmost app is anything but SB+).

The thinking behind this design is that I normally know where I want things to be if I am in Silverbullet so I dont have to first switch to that other app and grab the link from there. Conversely, if I cannot be bothered to switch to SB and determine a place for its destination, I will want to deal with this later and simply want to make sure I will be reminded by its existence in todays journal page.
It takes some getting used to inserting a link either not seeing where it is coming from or not seeing where the link is going to appear but it makes a lot of sense after some time.

Usage
Put the script in the Shortcuts app and call it to action by Spotlight or Launchbar (which is what I use), the a little menu pops up asking you where the linked to item can be found, press the corresponding letter, hit Enter. Done.

Disclaimer
some inspiration was drawn from org-mac-link.el – Grab links from open Mac applications
Claude was put to use for some of the coding
Use at your own risk. Things are certainly a little rough around the edges, I am not a programmer, but maybe someone finds it useful.

In short:

  • if the target document is a PDF, the link will direct you to the currently open page in the PDF (only in Skim and Devonthink)
  • the link will go where the cursor is if frontmost app is SB itself, otherwise it will go in a line of its own located on today's journal page under "Inbox"
  • The Inbox heading will be created if necessary
tell application "System Events"
	set frontApp to name of first process whose frontmost is true
end tell

tell me to activate

set appList to {"m – Mail", "f – Finder", "s – Safari", "k – Skim", "c – Chrome", "d – DEVONthink"}

set chosen to choose from list appList with title "Link holen" with prompt "Quelle wählen:" default items {"m – Mail"}
if chosen is false then
	tell application "System Events"
		set frontmost of (first process whose name is frontApp) to true
	end tell
	return
end if

set choice to item 1 of chosen

if choice is "m – Mail" then
	tell application "Mail"
		set theSelection to selection
		set theMessage to item 1 of theSelection
		set msgID to message id of theMessage
		set msgSubject to subject of theMessage
		set msgSender to extract name from (sender of theMessage)
		set encodedID to do shell script "python3 -c \"import urllib.parse; print(urllib.parse.quote('<" & msgID & ">'))\" "
		set theLink to "[" & msgSubject & " – " & msgSender & "](message://" & encodedID & ")"
	end tell
	
else if choice is "f – Finder" then
	tell application "Finder"
		set theItem to item 1 of (get selection)
		set thePath to POSIX path of (theItem as alias)
		set theName to name of theItem
		set theLink to "[" & theName & "](file://" & thePath & ")"
	end tell
	
else if choice is "s – Safari" then
	tell application "Safari"
		set theURL to URL of current tab of front window
		set theTitle to name of current tab of front window
		set theLink to "[" & theTitle & "](" & theURL & ")"
	end tell
	
else if choice is "k – Skim" then
	tell application "Skim"
		set theDoc to front document
		set thePath to path of theDoc
		set theName to name of theDoc
		set pageNum to index of current page of theDoc
		set encodedPath to do shell script "python3 -c \"import urllib.parse; print(urllib.parse.quote('" & thePath & "', safe='/'))\" "
		set theLink to "[" & theName & ", S. " & pageNum & "](skim://" & encodedPath & "#page=" & pageNum & ")"
	end tell
	
else if choice is "c – Chrome" then
	tell application "Google Chrome"
		set theURL to URL of active tab of front window
		set theTitle to title of active tab of front window
		set theLink to "[" & theTitle & "](" & theURL & ")"
	end tell
	
else if choice is "d – DEVONthink" then
	tell application "DEVONthink"
		set theRecord to item 1 of (get selection)
		set theName to name of theRecord
		set theURL to reference URL of front think window
	end tell
	set theLink to "[" & theName & "](" & theURL & ")"
	
end if

-- Je nach Ausgangs-App unterschiedlich einfügen
if frontApp is "silverbullet-app" then
	-- Direkt am Cursor einfügen
	set the clipboard to theLink
	tell application "System Events"
		set theProc to first process whose name is frontApp
		set frontmost of theProc to true
		repeat until frontmost of theProc is true
			delay 0.05
		end repeat
	end tell
	delay 0.05
	tell application "System Events" to keystroke "v" using command down
else
	-- In Journal-Inbox schreiben
	set todayDate to do shell script "date +%Y-%m-%d"
	set journalPath to "/Users/cheld/kullo/vorher_in_Seafile/Notizen_kullo/Silverbullet_v3/Journal/" & todayDate & ".md"
	set journalContent to do shell script "cat " & quoted form of journalPath
	if journalContent does not contain "# Inbox" then
		do shell script "python3 -c \"
import sys
with open(sys.argv[1], 'a') as f:
    f.write('\\n# Inbox\\n- ' + sys.argv[2] + '\\n')
\" " & quoted form of journalPath & " " & quoted form of theLink
	else
		do shell script "python3 -c \"
import sys
with open(sys.argv[1], 'a') as f:
    f.write('- ' + sys.argv[2] + '\\n')
\" " & quoted form of journalPath & " " & quoted form of theLink
	end if
	-- Zurück zur Ausgangs-App
	tell application "System Events"
		set theProc to first process whose name is frontApp
		set frontmost of theProc to true
		repeat until frontmost of theProc is true
			delay 0.05
		end repeat
	end tell
end if