Page 1 of 1
Calling a user-written function located externally
Posted: Tue Jan 20, 2009 8:01 am
by Steelweaver52
Hi, friends:
In the script of button "A", I would like to call a user-written function that resides in the script of button "B". Is it possible to do that?
I have found that the following doesn't work:
Code: Select all
put (myCustomFunction() of button "B") into myVariable
Would it work if I put myCustomFunction() into the script of the card (instead of button "B", that is), since the card is located "upstream" in the message hierarchy?
Thanks!
---Tom Nally, New Orleans
Posted: Tue Jan 20, 2009 11:23 am
by bn
Hi Tom,
Would it work if I put myCustomFunction() into the script of the card (instead of button "B", that is), since the card is located "upstream" in the message hierarchy?
That is the way to go, put it either into the card script or the stack script, depending on if it is called only from controls of the card or from more cards than just that card. Sometimes I put all this stuff into the stack script for maintenance reasons, I find it easier when debugging, but that is a question of style, habits and circumstances.
regards
Bernd
Posted: Tue Jan 20, 2009 11:31 am
by Klaus
Hi Tom,
or you could just add the buttons script to the message hierarchy (temporarily or not):
...
insert script of btn "B" of cd X of stack Y into back
put myCustomFunction() into myVariable
remove script of btn "B" of cd X of stack Y from back
## Optional
..
Et voila!
It's magic, ladies and gentlemen
Best from germany
Klaus
Posted: Tue Jan 20, 2009 2:53 pm
by Steelweaver52
Thanks, friends.
I think that I'm going to do it in the following way.
(To review, myCustomFunction() resides in the script of button "B". I want to call this function from the script of button "A".)
In my particular application, program control need not return to button "A" after myCustomFunction() is called. Therefore, I'm going to write a mini-handler in the script of button "B" which does nothing but call myCustomFunction()
Code: Select all
on CallMyCustomFunction
put myCustomFunction() into AAA
end CallMyCustomFunction
Then, from the script of button "A", I'm going to call this mini-handler in button "B":
Code: Select all
send CallMyCustomFunction to button "B" of this card
In subsequent projects, however, I think that I will move the functions to the card on which the buttons reside, rather than doing it as I just described.
Thanks again!
---Tom Nally, New Orleans
Posted: Tue Jan 20, 2009 4:25 pm
by Klaus
Hi Tom,
yep, whatever
But please put ALWAYS QUOTES around the message you are sending:
...
send "CallMyCustomFunction" to button "B" of this card
...
It may work without, but "Murphy's Law" is always striking when you least exspect it!
You may also want to look at the the "CALL" command in the docs!
Best
Klaus
Posted: Tue Jan 20, 2009 6:01 pm
by Steelweaver52
Klaus wrote:
But please put ALWAYS QUOTES around the message you are sending:
Oh, thanks!
I didn't know that was a "best practice".
Actually, I might not adhere to that best practice with perfect fidelity. But if I fail to adhere to it, I will try to remember to acknowledge to readers that I'm neglecting a best practice, so as not to lead others astray.
For that matter, if I ever post the code of an application, it could become a case study in "How Not To Program".
Thanks again!
---Tom Nally, New Orleans
Posted: Tue Jan 20, 2009 8:21 pm
by malte
Hi Tom,
there is an easy way to enforce best practise. Set the script editor to script compile mode.
You will also need to declare all variables then and quote all literals, but it will save you many many headaches while debugging.
fwiw,
Malte
Posted: Tue Jan 20, 2009 10:56 pm
by Janschenkel
You can actually also use
Code: Select all
put valued("myCustomFunction()", button "B") into myVariable
to get the result of a function defined in the other button - which can get tricky with quotes.
But the best idea is to move it 'upstream' into the card or stack script.
Jan Schenkel.