Re: variables vrs properties
Re: variables vrs properties
- Subject: Re: variables vrs properties
- From: Axel Luttgens <email@hidden>
- Date: Thu, 6 Jan 2011 11:16:41 +0100
Le 5 janv. 2011 à 23:20, KOENIG Yvan a écrit :
>> [...]
>
> Hello
>
> Do you know why in this short script :
>
> property list_of_items : {}
>
> on run
> set p2d to "" & (path to desktop)
> set my list_of_items to {p2d & "Courrier 96.cwk" as alias, p2d & "Converted document.doc" as alias}
> run script do_the_duty
> end run
>
> on open sel
> copy sel to my list_of_items
> run script do_the_duty
> end open
>
> script do_the_duty
> display dialog (list_of_items as text)
> end script
>
> when I drop items on the droplet icon I get : "The variable «do_the_duty» isn't defined"
>
> To my knowledge, it's defined exactly as it is in the on run / end run block.
Hello Yvan,
That's because an explicit run handler is very special wrt scoping rules; in particular, an identifier that is not yet "defined" isn't treated as a strictly local one by default.
So, consider this variant; it shouldn't yield any error any more:
property list_of_items : {}
on run
set list_of_items to {1, 2}
run do_the_duty
end run
on open sel
copy sel to list_of_items
run my do_the_duty
end open
script do_the_duty
display dialog (list_of_items as text)
end script
Note that there's only one "my" keyword anymore; it is needed to explicitly look for the value bound to identifier do_the_duty in the global scope.
On the other hand, Shane's suggestion (put the definition of do_the_duty before the open handler) works through lexical scoping; but then, strictly speaking, there is no need for a "my" keyword anymore:
property list_of_items : {}
on run
set list_of_items to {1, 2}
run do_the_duty
end run
script do_the_duty
display dialog (list_of_items as text)
end script
on open sel
copy sel to list_of_items
run do_the_duty
end open
Let's then try the converse; in this variant, each of the three "my" keywords is needed:
on run
set list_of_items to {1, 2}
run do_the_duty
end run
on open sel
copy sel to my list_of_items
run my do_the_duty
end open
script do_the_duty
display dialog (my list_of_items as text)
end script
property list_of_items : {}
HTH,
Axel
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden