Search a card for button name

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

Post Reply
bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Search a card for button name

Post by bidgeeman » Tue Jun 09, 2009 11:06 am

Hello.
I made a simple search text button and it works great for searching in a field

Code: Select all

on mouseUp
   put line 1 in field "Find" into tText
   find tText in field "test"
end mouseUp

but how do you search a card for a button name?
I tried this but it did not work.

Code: Select all

on mouseUp
   put line 1 in field "Find" into tText
   find tText in card "test"
end mouseUp
Many thanks
Bidge

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Tue Jun 09, 2009 12:56 pm

"find" will only work in fields (to my knowledge). You will need to check for the objects and interrogate their names. There's a million ways you could do this but something like the following function could be a basis:

Code: Select all

function seekButton pButtonNeedle, pCardHaystack
   local tNeedle
   put "button" && quote & pButtonNeedle & quote && "of" && the long name of card pCardHaystack into tNeedle
   if there is a button tNeedle then
      return pButtonNeedle && "found on card" && pCardHaystack
   else
      return pButtonNeedle && "not found on card" && pCardHaystack
   end if
end seekButton
You could then call the function such as:

Code: Select all

answer seekButton("ButtonBlue","CardGreen")
which should tell you if the button is found on the card specified. Of course, I've not error trapped checking that the card itself exists, but you get the idea. (I hope)

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Post by bidgeeman » Tue Jun 09, 2009 1:52 pm

Yikes...thanks SparkOut. I'll have to let that code soak in for a while ;)

Many thanks for youir help
Cheers
Bidge

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Tue Jun 09, 2009 4:56 pm

OK! The simple way to look at it is that instead of trying to "find" an object, you can ask "if there is <an object>" (or file etc).

So in essence the function asks you to give it a button name to find and a target card to search, and the simple thing that is being asked is:

If there is a button "myButton" of card "myCard" then
--yes there is
else
--no there isn't
end if

Wrapping it up in a function just made it more generic for reuse. I only used the long name to make it more specific - but if you hadn't provided an unambiguous card name that could be found then it would have been moot anyway.

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Post by bidgeeman » Tue Jun 09, 2009 11:22 pm

Hi SparkOut.

I'll go away and do some research on functions.

Thanks
Bidge

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Post by bidgeeman » Thu Jun 11, 2009 12:22 am

Hi Again.

Well...I've tried and tried but I keep getting this error:

Code: Select all

stack "Main": execution error at line 3 (Chunk: can't find card), char 72
I adjusted the script and made sure I had a Test card in the stack:
function seekButton pButtonSearch, pCardTest
local tFeedback
put "button" && quote & pButtonSearch & quote && "of" && the long name of card pCardTest into tFeedback
if there is a button tFeedback then
return pButtonNSearch && "found on card" && pCardTest
else
return pButtonSearch && "not found on card" && pCardTest
end if
end seekButton
Can someone please point out where might be making the mistake?

Many thanks
Bidge

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Thu Jun 11, 2009 6:12 am

Hi Bidge,

How are you calling the 'seekButton' function? It should be something like

Code: Select all

on mouseUp
  answer seekButton("ButtonName","CardNameOrNumber")
end mouseUp
And are you sure there is such acard? You can make the function slightly safer:

Code: Select all

function seekButton pButtonSearch, pCardTest
  if there is not a card pCardTest then
    return "could not find card" && pCardTest
  end if
  put "button" && quote & pButtonSearch & quote && "of" && the long name of card pCardTest into tFeedback 
  if there is a button tFeedback then 
    return pButtonSearch && "found on card" && pCardTest 
  else 
    return pButtonSearch && "not found on card" && pCardTest 
  end if 
end seekButton
HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Post by bidgeeman » Thu Jun 11, 2009 7:01 am

Hi Jan.

I have uploaded my test stack if someone could look at it for me and explain where I am going wrong. I think I am calling the function from the Text field incorrectly. (Working with Functions is very new to me).

http://www.errolgray.com.au/Buttonsearch.zip

Some expert advice would be very heplfull indeed :)
Thanks
Bidge

WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm

Post by WaltBrown » Thu Jun 11, 2009 7:53 am

tFeedback is not available between the scripts because it was not declared a global.

You put a value into a variable of the same name as the function, I don't think that would be good.

This does it without the function:

on mouseUp
put text of field "Field" into buttonToSeek
if there is a button buttonToSeek then
answer "Found"&&buttonToSeek
end if
end mouseUp

If do it with the function, the button script that works is:

on mouseUp
answer seekButton(field "Field", the short name of this card)
end mouseUp

And the function in the stack:

function seekButton pButtonSearch, pCardTest
if there is not a card pCardTest then
return "could not find card" && pCardTest
end if
if there is a button pButtonSearch of card pCardTest then
return pButtonSearch && "found on card" && pCardTest
else
return pButtonSearch && "not found on card" && pCardTest
end if
end seekButton

Hope this helps, I'm learning by solving problems too!
Walt
Walt Brown
Omnis traductor traditor

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Post by bidgeeman » Thu Jun 11, 2009 8:09 am

Thanks Walt.
That worked. Finally :) Many many thanks!!!

How do you to adjust the script to look for a button across multiple cards?

Cheers
Bidge

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Thu Jun 11, 2009 10:44 am

Hi Bidge,
in the stack you posted the button Search has this script:

Code: Select all

on mouseUp 
   ask "Name of button"
   put it into tFeedback
   
   answer seekButton("ButtonName","CardNameOrNumber") 
end mouseUp
This can not work. You first create a variable 'tFeedback', this variable holds the name of the button you are looking for. but when you call the function with seekButton("ButtonName","CardNameOrNumber")
you put "ButtonName" into quotes. That tell rev to treat this as a literal. That means just the word ButtonName. Even if you had a variable ButtonName the function would not pass the content of the variable, only the word ButtonName,
If you would put

Code: Select all

seekButton(tFeedback,"CardNameOrNumber")
that would work for the button name, still leaves the CardNameOrNumber. Here the same, you just pass the word without any content to the function. You could put

Code: Select all

on mouseUp 
   ask "Name of button"
   put it into tFeedback
   put the name of this card into tCardName
   answer seekButton(tFeedback,tCardName) 
end mouseUp
into your button. You would additionally fill the variable tCardName with the name of the card this is on and pass it as a variable (not literal word) to the function. This works.

Please Bidge, get this sorted out what is a variable and a literal.

For example the name of a field is a literal just the word so you put it into quotes to indicate to Rev: look no further this is just the word and not the name of a variable. In Rev you should always quote a word, but you can never quote a variable name.
It is very important to make that distinction.
To confuse you a bit: This is how Rev looks at it: Rev finds a word it does not know. It looks around whether there is a variable of that name. If Rev does not find a variable of that name it treats it as a literal. That is the reason why you can get away with a literal without quoting it. BUT I strongly discourage this approach because sooner or later you dont remember if you had a variabel of the same name and you want to take that name for a literal and bang, you have a hard time to figure out why the script does not work.

So: literals -> always quote
variables -> you can not quote without making them literals and defying the purpose of a variable.

regards
Bernd

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Post by bidgeeman » Thu Jun 11, 2009 11:43 am

Thanks for the tips on variables and literals. I am just looking into these and it's a bit hard to get my head around but it's starting to sink in.

So this script looks for a button an one card card "Test". How would it be adjusted to look over several cards?

How do you adjust "pCard" to search ALL cards in the stack...I tried pCard"" but nothing.....I know these are basic questions but I can't find any references to help sort it out for myself? Sorry :oops:

Cheers
Bidge

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Thu Jun 11, 2009 12:21 pm

Bidge,

put

Code: Select all

on mouseUp 
   ask "Name of button"
   put it into tFeedback
   put empty into tCardName
   answer seekButton(tFeedback,tCardName) 
end mouseUp
into your search button
put

Code: Select all

function seekButton pButtonSearch, pCardTest
    -- you dont pass a specific card and left this parameter empty (just pass empty when calling the function)
    -- like " put empty into tCardName"  in your search button, if you dont put empty into it it will 
    -- pass the literal "tCardName" which probably does not exist as a card name
    
    -- checking all the cards
    -- breakpoint
    if pCardTest = empty then
        -- go over every card and every button and look if the name of the button matches pButtonSearch
        -- remember on a card there can be several buttons of the same name
        repeat with i = 1 to the number of cards
            repeat with j = 1 to the number of buttons of card i
                if the short name of button j of card i = pButtonSearch then
                    put "button id " & the id of button j  & " on card id " &  the id of card i & return after tFeedback
                end if
            end repeat
        end repeat
        if last char of tFeedback is return then delete last char of tFeedback
        
        -- return the result, once you issue a return in a function the function ends there
        if tFeedback is not empty then 
            return tFeedback
        else
            return "nothing found"
        end if
         
        -- now check for a button on a specific card
    else -- this else means pCardTest is not empty so you provided a name of a card
        repeat with j = 1 to the number of buttons of card pCardTest
            if the short name of button j of card pCardTest = pButtonSearch then
                put "button id " & the id of button j  & "on card id " &  the id of card pCardTest & return after tFeedback
            end if
        end repeat
         
        if last char of tFeedback is return then delete last char of tFeedback
        
        -- return the result, once you issue a return in a function the function ends there
        if tFeedback is not empty then 
            return tFeedback
        else
            return "nothing found"
        end if
         
    end if -- end of  if pCardTest = empty
end seekButton
into your function
look at the comments.

I set it up that you can either search on a specific card or on all the cards and all the buttons

you could put into your search button

Code: Select all

on mouseUp 
   ask "Name of button"
   put it into tFeedback
   answer seekButton(tFeedback) 
end mouseUp
here you dont even pass the second parameter to the function which is ok in this case, the second parameter will just be empty. And we test for empty in the function.


regards
Bernd

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Post by bidgeeman » Thu Jun 11, 2009 12:38 pm

Bernd....I had no idea this was such a complicated task otherwise I would not have even attempted it. That is a huge script!! You astound me that you can write these codes so quickly. Hopefully one day I will be able to do something like this too. I will study it thoroughly.

Thank you.
Bidge

Post Reply