Page 1 of 1

Create a handler in a control programmatically.

Posted: Sat Nov 10, 2018 11:30 pm
by djkesler
Is there a way to create a handler script in a newly created field, button, or group, programmatically?

I want to put a few handler scripts in a control that I am programmatically creating.

Or, programmatically, can handler scripts be copied from a control and pasted into a control?

Thanks for any help.

David

Re: Create a handler in a control programmatically.

Posted: Sat Nov 10, 2018 11:49 pm
by FourthWorld
The script of an object is a property, so common property assignment syntax will do what you need:

Code: Select all

set the script of btn 1 to tSomeVariableContainingAScript
That said, dynamic code is notoriously hard to debug and frequently a disproportionate source of problems during long-term maintenance of a code base.

If you tell us a bit about what you want to accomplish we may be able to find a simpler solution.

Re: Create a handler in a control programmatically.

Posted: Sun Nov 11, 2018 12:28 am
by djkesler
Thanks for the insight.

I am wanting to provide some simple handlers at the group level for controls I create from db table info. They are very simple closefield, menupick, etc. for controls located in the group.

Thanks again. I am working on a presentation for LCGobal on “Arrays to MySQL and back” on Thursday.

Re: Create a handler in a control programmatically.

Posted: Sun Nov 11, 2018 12:42 am
by FourthWorld
Would behaviors do the trick? Then you have code in one place, easily assignable to any number of objects as needed.

http://lessons.livecode.com/m/4071/c/16766

Re: Create a handler in a control programmatically.

Posted: Sun Nov 11, 2018 4:28 am
by dunbarx
There is a concept know as "self modifying code". This is where the code itself is changed while executing to accommodate the programmer's intentions. Livecode will have trouble with this because it does not at all like having a "script" property modified while a handler in that script is running .

I have played around with setting the script of an object from the outside, but have never had to do so to implement any process I ever worked on. If there is a ""code change" required, it should always be possible to simply execute a pre-defined code snippet, separated in, say, a conditional or a switch construction.

In other words, say that "something" engenders "somethingElse", and "this" engenders "that". Those two "methods" should not require a change in the code of a handler, rather only forks in that handler. Further, whatever triggers either situation can also be handled in the, er, handler.

Craig Newman

Re: Create a handler in a control programmatically.

Posted: Sun Nov 11, 2018 5:04 am
by djkesler
Self modifying code is way over my head, and pay grade!

But the behavior idea would work. Since the long ID of the button is specified, it probably doesn’t need to be in the message path, right? I would want the code to work on any card in the stack. As I understand it, if the behavior is tied to a number of groups the phrase “of me” would make reference to the group that was executing the behavior as if the code was located in the group script. Is that correct?

Thanks for the help

David

Re: Create a handler in a control programmatically.

Posted: Sun Nov 11, 2018 10:25 am
by jmburnod
What Dictionary about "me" is not clear for me (LC 9.0.2 rc 1)
put me into myVariable -- puts name, if me is not a container
MyVariable = empty if the target is a button, but

Code: Select all

put the short name of me

works
put me into myVariable -- puts contents, if me is a container
Works with groups, flds and images (with behavior or not)
I tested:
For group: return the short name of group
For Fields: return the text of fld
For images: return the short name of image

Best regards
Jean-Marc