Can action code set a link action (i.e. onlink)?

[This is pulled through from a DM conversation on this topic]

In short, No. Why? Link actions are _pre link type– (i.e. all links of a type share the action) and are a property of a link type which is itself a property of the links defined in a document. IOW, it isn’t an attribute which is generally how action code interacts with a document.

However, it turns out you can read and edit/create such actions via AppleScript. Here is an example:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Tinderbox 9"
	tell its document "Untitled"
		tell its linkType "exception"
			set action to "$Color(destination)='bright blue';"
		end tell
		tell its linkType "disagree"
			set action to "$Color(destination)=\"bright red\";"
		end tell
	end tell
end tell

N.B. the way AppleScript allows backslash escaping of double-quotes inside an AppleScript string value (with is double-quote delimited)

The above code means that, when executed, it sets a link-type action so that for link types ‘disagree’ and ‘exception’, on linking to another note the target (destination) note has a specific $Color set. Clearly, in real use you’d have more complex code but setting a $Color is good for testing:

†. For clarity, you can’t set different links of the same type to use a different on-link action. Nor is there an on-unlink action, before someone asks.