Collecting button names

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

Collecting button names

Post by anmldr » Fri Jun 05, 2020 8:27 pm

In my stacks, I would like to know the name of each button that is clicked (mouseUp). I know how to get the short name of the button. No problem with that. But, this is in retrospect and I don't want to go back to each button in the stack and write the script or to write a "pass mouseUp" in the buttons' script. Is there a script that would allow me to get this info even though I have already created the buttons and their scripts?

I have been trying myself to find the info...I promise.

TIA,
Linda

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Collecting button names

Post by bogs » Fri Jun 05, 2020 8:44 pm

in the stack script, you'd be looking for the target (if I understand your post correctly).

So, you know (because you wrote pass mouseUp I am guessing here) that any button without a mouseUp script will just pass that message along until it ultimately hits the main stack (see or look for 'message path' in the lessons area).

In the stack, you can write a simple handler like -

Code: Select all

on mouseUp
   if word one of the target is "button" then answer the short name of the target
end mouseUp
...which will only fire if no other mouseUp has happened before it reached the stack, and this is what you would see -
aPic_target.png
Whats in a name ?
Image

anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

Re: Collecting button names

Post by anmldr » Fri Jun 05, 2020 9:32 pm

anmldr wrote:
Fri Jun 05, 2020 8:27 pm
I don't want to go back to each button in the stack and write the script or to write a "pass mouseUp" in the buttons' script.
I don't have "pass mouseUp" in the card or button scripts. That is what I was trying to avoid. This will only be used during development. So, I wanted the scripts to be only in one place so that I can delete them or comment them out later.

Hmm...Why is it that I often search and search for an answer, then I decide to ask the question here and then...I find the answer myself. Hmm. :roll:

Thanks for "listening" to me.

BTW, I put this into the stack's scripts and it is working. I am absolutely sure that there is a less cumbersome way to collect this info and store it. But, it will work for me. As I wrote, it is just during development that I will need it.

Code: Select all

global gPreviousCard, gMouseClickName

on preOpenStack
   put empty into gPreviousCard 
end preOpenStack

on mouseDown
   put the short name of the target into gMouseClickName
end mouseDown

on closeCard
   local tTextFromFile
   
   open file specialFolderPath("desktop") & "/CardHistory.txt" for read
   read from file specialFolderPath("desktop") & "/CardHistory.txt" until EOF
   put it into tTextFromFile
   close file specialFolderPath("desktop") & "/CardHistory.txt"
   put the short name of this card into gPreviousCard
   open file specialFolderPath("desktop") & "/CardHistory.txt" for write  
   write tTextFromFile & cr & gPreviousCard && gMouseClickName to file specialFolderPath("desktop") & "/CardHistory.txt" 
   close file specialFolderPath("desktop") & "/CardHistory.txt"
end closeCard
Linda

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Collecting button names

Post by bogs » Fri Jun 05, 2020 10:52 pm

anmldr wrote:
Fri Jun 05, 2020 9:32 pm
I don't have "pass mouseUp" in the card or button scripts. That is what I was trying to avoid. This will only be used during development. So, I wanted the scripts to be only in one place so that I can delete them or comment them out later.
Yes, I think maybe I wasn't as clear as I could have been.

I'll try again :)

When you have a button or other object that doesn't have a script attached to it, the message that would normally be acted upon moves down the message path to the next object that it might act upon, until (if it doesn't hit anything else) it reaches the main stack.

As an example, let us say you have a stack, card, and button (keeping this simple). Your button has no 'mouseUp' handler script, but your card does. When you click on the button, without any script at all in it, the mouseUp will trigger on the card.

To further this example, your button and card have no mouseUp script, but the stack does (like the one I posted above). You click the button, the message looks at the script on the card, doesn't see a handler for mouseUp, and moves down to the stack where it is handled by the script I showed you. All of this is done without putting 'pass mouseUp' anywhere either in the buttton, or the card.

To complete this example, any button you *have* a script in, the mouseUp will trigger in that button and, unless you specifically put 'pass mouseUp', that is where it will stop.

So, to satisfy your goal of wanting any buttons that don't have a script in their object naming itself when you click on it, you would put the script I wrote above (or one like it) in the stack script. Buttons that have scripts will fire the scripts you wrote, and buttons that have no script will tell you their names.

I hope that clarifies it for you.
Image

anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

Re: Collecting button names

Post by anmldr » Fri Jun 05, 2020 10:55 pm

I do.

Linda

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Collecting button names

Post by dunbarx » Fri Jun 05, 2020 11:20 pm

If you never have a mouseDown handler in any button, then, unless I misunderstand what you are after, in the stack script:

Code: Select all

global buttonList
on mouseDown
  put the short name of the target & return after buttonList
end mouseDown 
If you do in fact have mouseDown handlers in certain buttons, you can kludge. As an example:

Code: Select all

global buttonList
on mouseWithin
  if the mouse is down then  put the short name of the target & return after buttonList
end mouseWithin
You could also use "mouseStillDown", and likely others...

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7392
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Collecting button names

Post by jacque » Sat Jun 06, 2020 5:12 pm

Probably the easiest way is to put the logging handlers into a frontscript that traps mouseUp, logs it, and then passes the mouseUp so the rest of the controls will receive the message. When it's no longer needed, omit inserting the script into front.

This is a good example of when to use "open file for append" when you want to start logging. That way you can do everything in your original script with one line after you've opened the file:

Code: Select all

write the short name of the target & cr to file <path/to /file>
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Collecting button names

Post by Klaus » Sat Jun 06, 2020 5:51 pm

Hi Linda,

as I wrote in another thread, you can have this much, much, much, much shorter! :-)

Code: Select all

on closeCard
   put specialFolderPath("desktop") & "/CardHistory.txt" into tFile
   put the short name of this card & CR AFTER url("file:" & tFile)
end closeCard
Yes, much shorter! :D


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Collecting button names

Post by dunbarx » Sat Jun 06, 2020 9:36 pm

Jacque.

Perfect time to use the frontscript.

Craig

Post Reply