Sunday, October 22, 2017

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

julifos
Administrator
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:

Applescript:

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

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 "[url]http://www.macscripter.net[/url]"
   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":

Applescript:

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").

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)