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

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

Modifying field contents with script

Post by kaveh1000 » Tue Aug 02, 2022 12:43 pm

Hi folks. I am sure simple answer! I have one field in a stack with some text. I want to modify the text using a script when I say:

Code: Select all

put "hello" after char -1 of fld 1
it works.

When I say:

Code: Select all

put the name of fld 1 into tName
put hello after char -1 of tName
it does not complain, but text is not added.

What is the obvious answer? ;-)
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 » Tue Aug 02, 2022 2:16 pm

My question would be what do you mean by tName?

IFF tName is a variable, you won't see anything until you dump that variable
into the messageBox or a field:
-
SShot 2022-08-02 at 16.15.46.png
-
I am also NOT at all sure why you would choose to use

Code: Select all

after char -1 of fld 1
-
SShot 2022-08-02 at 16.18.01.png
-
When, personally I would put

Code: Select all

before fld 1
-
SShot 2022-08-02 at 16.20.55.png

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 2:24 pm

Thanks for trying it out.

OK. I think I am being dumb here. The word "hello" is being added to the name of the field, not to the text of the field itself.

I am trying to use the name of the field as a reference to that field but not successfully.

So what is simplest way of adding text into the field? I don't want to copy the text out and paste it again as it might have some formatting.
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 2:47 pm

Just to be clear - are you trying to change the name of the field rather than it's text?

As an aside - you don't need to specify char -1 - you can just say after field 1. But neither will put a space between existing and added text so keep that in mind.

If you want to see what's inside variables, just put a breakpoint at or just after the variable being set and you can see it in the debugger. Or if you want, you can just 'put' the variable with no destination and it will be written to the messageBox.

Also note that 'name' may not be what you think it is.
If no name has been assigned to the field in the property panel, 'name' and 'short name' will return the field id.
if you give the field the name 'test' :
- the short name returns : test
- the name returns : field "test"
- the long name returns : field "test" of card id 1002 of stack "Untitled 1"

I presume you're looking to change the (short) name of the field? I'm not sure this is a sensible way to go, but depends on your use-case so can't comment.
Note that if you want to rename the field, you have to treat it as a property, not a variable, eg:

Code: Select all

set the name of field 1 to "test2"

Then again i'm afraid i'm still not sure what you're aiming to achieve. If you describe the whole process maybe someone can point you to a different solution...

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 2:53 pm

Sorry I was not clear. I want to achieve what the first script does:

Code: Select all

put "hello" after char -1 of fld 1
I agree it would be simpler to say

Code: Select all

put "hello" after fld 1
So I want to add text to the contents of the field. I do not need to change the name of the field.
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 » Tue Aug 02, 2022 2:56 pm

SShot 2022-08-02 at 16.55.25.png
-
This is the way I would go about things if I wanted to add something to the name
of a field.
Last edited by richmond62 on Tue Aug 02, 2022 3:01 pm, edited 1 time in total.

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 2:57 pm

kaveh1000 wrote:
Tue Aug 02, 2022 2:53 pm
So I want to add text to the contents of the field. I do not need to change the name of the field.
in that case it's simple indeed.

as Richmond says, use 'before'. Assuming you are targeting field 1 of the card (or just change this as needed), just put this in a button:

Code: Select all

on mouseUp
    put "Hello " before field 1 -- note the space after 'Hello'
end mouseUp
If field 1 contains 'kaveh1000' it will change to 'Hello kaveh1000'

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 3:13 pm

OK. But I have several fields and my handler is in the stack script. So I will be clicking in a field that is the target. The script needs to grab the name of that target and modify that field only.

This is the principle problem I have – passing the name of a field as a variable and editing only that field.
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 3:36 pm

You may be better served by using a behaviour for your fields, which you can assign to fields and intercepts the closeField handler if you want the text to change on exiting the field and if the field's contents have changed, the exitField handler if you want to change the text of the field when exiting it and the contents have not changed, or the textChanged handler if you want to modify 'in realtime' as the text is changing.

This way you do not need to know the name of the field, just assign the behaviour (usually a stack or a button script -- a substack is fine for this but if you are going to use in multiple projects consider using script only stacks instead).

By putting this into a behaviour, you will only have 1 script that will serve all your fields - the alternative is to add all the script to all the fields which is cumbersome and difficult to maintain - i'd strongly recommend considering behaviours for this.

If you have several fields that need this, i'm guessing you will not have the same text being inserted in all fields and you'd need to cater for that. For example the field could have a custom property that defines the standard text to be inserted.

Also, if doing this you'd need to check that the existing text does not already contain the standard text you want to insert... you don't want the field to read 'Hello Hello Hello kaveh1000'...

As mentioned above, if you actually provide a clear picture of what you are trying to achieve you will likely get more helpful advice...

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 3:44 pm

Much simpler, I think, and a great lesson to boot, is to learn the use of "the target". Assuming your fields are locked:

Code: Select all

on mouseup
   put "hello" after the target
   --possibly put space & "hello" after the target
   --or, for example, put "Hello" before word 2 of the target
end mouseup
LC "knows" which field you have clicked on, and the target contains all the information it needs to act.

Know also about "target" (without "the") which returns the field contents, and is not an object reference.

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 » Tue Aug 02, 2022 3:54 pm

Thanks all and hi Craig...

I am familiar with "target". Problem here is that I am passing a message from one handler to another and by the time the second handler starts working the value of target has changed.

Here is what I did that worked:

Code: Select all

put the ID of the target into tID
put hello after fld ID tID
Please see minimal stack attached, with slightly different code
Attachments
change_field.livecode.zip
(1.28 KiB) Downloaded 60 times
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 5:03 pm

Ah, I see.

But I thought you said "So I will be clicking in a field that is the target."

And that is why Stam suggested using behaviors.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7238
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Modifying field contents with script

Post by jacque » Tue Aug 02, 2022 5:46 pm

You could also use the short name of the target instead of the ID. They both do the same thing but I find if I'm debugging it's nice to have the name available so I can identify the target more easily.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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 5:47 pm

dunbarx wrote:
Tue Aug 02, 2022 3:44 pm
Much simpler, I think, and a great lesson to boot, is to learn the use of "the target". Assuming your fields are locked:

Code: Select all

on mouseup
   put "hello" after the target
   --possibly put space & "hello" after the target
   --or, for example, put "Hello" before word 2 of the target
end mouseup
LC "knows" which field you have clicked on, and the target contains all the information it needs to act.

Know also about "target" (without "the") which returns the field contents, and is not an object reference.

Craig
Craig, the OP already stated that the end-user is expected to enter text, hence cannot be locked. ExitField and CloseField are the next best option.

it really is very simple - I don't want to assume the OP is handy with these, so as an example:
In an effort to abstract things as much as possible, the field is given a custom property uStandardText. In the IDE set this to 'Hello'.
Either in the field's script or in a behaviour (easiest to create a substack, call it what you want, and add the below to it's stack script)

Code: Select all

on closefield 
    local tText, tStandardText
    put the uStandardText of me into tStandardText
    if tStandardText is in the text of me then pass closeField
    
    put the text of me into tText
    put tStandardText & space before tText
    set the text of me to tText
    pass closeField
end closefield
If using as a behaviour make sure you assign the button or stack that contains this script to the behaviour or your field. If not, just add to your field's script.

If you then type in 'kaveh1000' into the field, when you exit/click out of it, it will be replaced with 'Hello kaveh1000'.

That's how i would do it anyway. 1 script (i prefer stacks, but a button will do) and 1 custom property in each field to hold the standard text.
You can of course make it more complicated by setting markers for before/after etc (again i would abstract as custom props).

HTH
Stam

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 7:20 pm

I am not getting this at all. Perhaps the answer lies in the posted stack? I did not examine that.

Because I do not see where the OP says a user will enter text. Maybe I just cannot see it?

Kaveh, can you give a step-by-step procedure? Something like:

1- The user clicks on a specific field.
2- I want LC to ask the user what text to append to that field.
3- The user enters the required text.
4- That text is appended to the field.

Craig

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”