Modifying field contents with script

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Modifying field contents with script

Post by richmond62 » Tue Aug 02, 2022 7:46 pm

SShot 2022-08-02 at 21.45.11.png
-
If you use mouseUp nothing works . . .
Attachments
Field Me Up.livecode.zip
Stack.
(6.32 KiB) Downloaded 85 times

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Modifying field contents with script

Post by kaveh1000 » Tue Aug 02, 2022 9:48 pm

Hi all

Apologies if I was not clear at the start. Please see my original post.

Simple case – card has only 1 field

1. User clicks on field
2. Text "hello" is added to the end of the field

My simple original script works:

Code: Select all

put "hello" after char -1 of fld 1
(Simpler to delete "char -1" but I was keeping it general)

Complex case - card has several fields

1. User clicks on any of the several fields
2. Text "hello" is added to the end of that particular field

My attempt was originally:

Code: Select all

put the name of fld 1 into tName
put hello after char -1 of tName
which did not work. My modified script does work:

Code: Select all

put the ID of the target into tID
put hello after fld ID tID
Kaveh

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Modifying field contents with script

Post by dunbarx » Tue Aug 02, 2022 10:22 pm

Kaveh.

OK, that is what I thought you wanted. Your handlers will work fine. The "complex" case is likely best, since it will cover you if you ever add or delete other fields. The handler is structured robustly, and will work in all cases.

What message are you trapping to make this work? "MouseUp"? If so, it seems that you are using locked fields. Otherwise that message is not sent. Always try to include that information, as it helps us understand what you are dealing with.

Also, ALWAYS quote literals:

Code: Select all

put "hello" after fld ID tID
And lastly, and it hardly matters, but know that you could have simply:

Code: Select all

put "hello" after the target
As was mentioned earlier. :wink:

Craig

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Modifying field contents with script

Post by stam » Tue Aug 02, 2022 10:52 pm

@kaveh - i think what people here are trying to help you with is limited by the information provided. The user interface doesn't really make sense as described. Is the user going to be fed data in a locked field and will have to click a button to change it? Or do you want to modify the text entered by the user?

Your comments hint at wanting to abstract a command to modify fieldsat card or script level and need some way of identifying the field being targeted (otherwise you could just put the script in the field)

The key thing to know is
a) will the user enter or modify text? or,
b) will the field be populated by other means, eg a popup menu, database etc?

If the latter, that means you can use locked fields, which will provide 'the target'. However this would still require the user to click on a locked field, which user-interface wise can be tricky. Or you could just use the textChanged handler to get the identifier for your field.

If the fields aren't locked (ie. the user can enter or modify the data) then you can either
a) use the enterField handler to store a script local or global variable with a field identifier (usually the long id is best) that will provide you an id you can use in an abstracted script, eg in the card or stack, to target that field, or

b) trigger an action during text input (with the textChanged handler) or after input/exiting the field (with the closeField handler)

I think the reason for these back-and-forth comments is that no one really understands what you have in mind - as Craig says a flow-charted process would be very helpful. Alternatively, why don't you post a mockup of what it is your planning?

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Modifying field contents with script

Post by kaveh1000 » Tue Aug 02, 2022 10:53 pm

Hi Craig

Great to have this interaction with you after a long time!

I take your points about the literal "hello". it was sloppy!

Did you see the minimal stack I uploaded? That answers your questions. I have 3 locked fields, yes. And the stack script is:

Code: Select all

local sFieldClicked

on mouseup
   if word 1 of the target is not "field" then pass mouseup
   put ID of the target into sFieldClicked
   Do_it
end mouseup

on Do_it
   put space & "hello" after fld ID sFieldClicked
end Do_it
Kaveh

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Modifying field contents with script

Post by stam » Tue Aug 02, 2022 10:57 pm

kaveh1000 wrote:
Tue Aug 02, 2022 10:53 pm
Hi Craig

Great to have this interaction with you after a long time!

I take your points about the literal "hello". it was sloppy!

Did you see the minimal stack I uploaded? That answers your questions. I have 3 locked fields, yes. And the stack script is:

Code: Select all

local sFieldClicked

on mouseup
   if word 1 of the target is not "field" then pass mouseup
   put ID of the target into sFieldClicked
   Do_it
end mouseup

on Do_it
   put space & "hello" after fld ID sFieldClicked
end Do_it

except your 3 fields are not locked!

If you set the lockText of these fields to true then it works as you'd expect.

Did you mean to use unlocked fields for this? Sadly you cannot use mouse actions on unlocked fields, as mentioned above.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Modifying field contents with script

Post by dunbarx » Wed Aug 03, 2022 4:17 am

Kaveh.

Good to talk to you as well.

Why do you have the "mouseUp" handler call another handler? Are the two examples just a small pair of snippets of a larger code set?

I see now what you meant with your comment about the "target" losing its "relevance" when the called handler does the work of actually doing the appending. You can pass that information along as a parameter, of course, or you can append the new text before you do the call, but again, do you need to do that at all?

Craig

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Modifying field contents with script

Post by kaveh1000 » Wed Aug 03, 2022 7:16 am

except your 3 fields are not locked!
Apologies. They should have been locked!
Why do you have the "mouseUp" handler call another handler? Are the two examples just a small pair of snippets of a larger code set?
I am keeping it general as it's just a test for a bigger project. In the main project I need to call another handler from the mouseup handler. And as you say "target" is lost.

Main point is that I need to trap the ID of the target and keep it as a local parameter. Then I can call as many other handlers as needed.

I really appreciate the time of all on this as usual. Thanks again.

Regards
Kaveh
Kaveh

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Modifying field contents with script

Post by richmond62 » Wed Aug 03, 2022 9:54 am

I am sorry that my examples were off the point.

I will accept 50% of the responsibility for that. :wink:

The other 50% lies with you . . .

As Granny said, "A picture is worth a thousand words."

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Modifying field contents with script

Post by kaveh1000 » Wed Aug 03, 2022 9:56 am

Richmond:

I will take 100% of blame because:
● I was OP
● I have learnt so much from you over the years.
:-)
Kaveh

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”