Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Handler query - exchanging values




On 27/06/2006, at 11:58 AM, email@hidden wrote:


G'day,

I'm struggling with what I think should be a simple transfer of a variable from one handler to another.

Could someone please offer assistance?

In your example you are treating all the variables as if they were global. They are not. You could make them global by declaring them so.


-------------------------------------
global apples, bananas, grapes, oranges

firstHandler()

on firstHandler()
global apples, bananas, grapes, oranges --> declares that these variables are global - not local
    set {apples,bananas} to {1,2} --> as a general rule lists are faster
my secondHandler() --> no need to pass params, they're global!
log {a:apples,b:bananas,g:grapes,o:oranges}
end firstHandler


on secondHandler() --> no params because we're using globals
global apples, bananas, grapes, oranges --> declares that these variables are global - not local
if apples is 1 then set oranges to 3
if bananas is 2 then set grapes to 4
--> no need to return data as the variables are global and will be seen outside this handler
end secondHandler
------------------------------------------------


Because you've made the assumption that your variables are global you do not return or collect the values of the variables that are being modified. This is how to do that:
------------------------------------------------
on firstHandler()
        set {apples,bananas} to {1,2} --> as a general rule lists are faster
        set {oranges, grapes} to my secondHandler(apples, bananas) --> collect and store result of secondHandler()
log {a:apples,b:bananas,g:grapes,o:oranges}
end firstHandler


on secondHandler(apples, bananas)
        if apples is 1 then set oranges to 3
        if bananas is 2 then set grapes to 4
	    return {oranges,grapes} --> return the values
end secondHandler
----------------------------------------------

Generally I prefer the second form, unless there is a need to use globals.

By the way, when your are writing test code, get into the habit of returning useful information. I've replaced your if/then ... display dialog with a log statement which pops the values of all the variables into a record. It's simple and direct - you know what your output was - you'll find it helps to keep you on top of your code.




Malcolm Fitzgerald

FOR YOUR COMPUTER Customised software built to your specifications. Using Macs? Automate your workflow with AppleScript. FOR YOU Computer training, software installation + upgrades, computer setups. IN TIMES OF NEED Troubleshooting, maintenance + repairs.

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/applescript-users/email@hidden

This email sent to email@hidden
References: 
 >Handler query - exchanging values (From: email@hidden)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.