Friday, June 23, 2017

#1 2005-10-13 03:17:30 pm

From: MalasaƱa, Madrid
Registered: 2002-11-20
Posts: 2014

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 {"", ""}

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

on open location x
   if x contains "" then
       display dialog "Don't you mean \"\"!?" buttons {"Yes!"} default button 1 with icon note
       continue open location ""
       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").



Board footer

Powered by FluxBB

[ Generated in 0.045 seconds, 11 queries executed ]

RSS (new topics) RSS (active topics)