What is "delegation"?

Delegation in AppleScript is similar to a filter. Using delegation, you can catch events which are not owned by you, then make some operations or let them flow.

For example, you can trap in your script the command “open location”, which is handled and owned by the Standard Additions osax (only if you receive it, of course). Eg, you can configure your script to open the websites you wish:

set availableSites to {"http://www.macscripter.com/", "http://www.macscripter.net/"}

open location ((choose from list availableSites) as text)

on open location x
	if x contains "macscripter.com" then
		display dialog "Don't you mean \"macscripter.net\"!?" buttons {"Yes!"} default button 1 with icon note
		continue open location "http://www.macscripter.net"
	else
		continue open location x
	end if
end open location

Of course, you can think about more useful uses of delegation…

Delegation also works for user-defined commands, aka “handlers”:

b's do("foo")

script a
	to do(x)
		x & " from a"
	end do
end script

script b
	property parent : a
	to do(x)
		display dialog x & " from b"
		continue do(x)
	end do
end script

As you can see, the statement continue is used to let the command “flow” or “continue”, be handled by the original owner of the command (the Standard Additions in the first example, the script object “a” in the second one, which is the “parent” and the original owner of the handler “do”, and inherited by “b”).