[comp.sys.mac.hypercard] question about implementing a 'find'

robin@csuchico.EDU (Robin Goldstone) (03/04/89)

I am writing my first hypercard stack and could use a little advice on how
to implement something:  I want to implement the 'find' command via a
button, allowing the user to hit the button repeatedly to continue the 
search for the same item if the first match is not the right one.  With
the "Find..." menu command, this is implemented by doing an initial 'find'
then repeatedly pressing return to find subsequent matches.  I do not want
to use the menu "Find..." command because it requires the user to understand
about entering the text only the first time, then using return.  In fact,
I will probably hide the menu bar when I am done.  Instead, I want to use the
HyperTalk 'ask' command to get the string, then use the HyperTalk 'find'. 

For those of you who are still awake, here's the problem - my 'find' button
cannot keep issuing the 'find' command, because this just finds the first 
occurrance over and over.  How do I simulate the pressing of the return key
to find subsequent occurrances of the string? 

I hope I haven't confused you all or asked something that you just covered
yesterday... Post a reply if you want or reply directly to robin@csuchico.edu.

Thanks,
Robin Goldstone, Systems Software Specialist
California State University, Chico  Computer Center 

stores@unix.SRI.COM (Matt Mora) (03/07/89)

In article <1191@csuchico.EDU> robin@csuchico.EDU (Robin Goldstone) writes:
>
>I am writing my first hypercard stack and could use a little advice on how
>to implement something:  I want to implement the 'find' command via a
>button, allowing the user to hit the button repeatedly to continue the 
>search for the same item if the first match is not the right one.  With
>the "Find..." menu command, this is implemented by doing an initial 'find'
>then repeatedly pressing return to find subsequent matches.  I do not want
>to use the menu "Find..." command because it requires the user to understand
>about entering the text only the first time, then using return.  In fact,
>I will probably hide the menu bar when I am done.  Instead, I want to use the
>HyperTalk 'ask' command to get the string, then use the HyperTalk 'find'. 
>
>For those of you who are still awake, here's the problem - my 'find' button
>cannot keep issuing the 'find' command, because this just finds the first 
>occurrance over and over.  How do I simulate the pressing of the return key
>to find subsequent occurrances of the string? 
>
>I hope I haven't confused you all or asked something that you just covered
>yesterday... Post a reply if you want or reply directly to robin@csuchico.edu.
>
>Thanks,
>Robin Goldstone, Systems Software Specialist
>California State University, Chico  Computer Center 

This is the find command that I paste into any stack that I
create or get from someone else that implements the Find...
comand from the menu.
  Just create a button and paste the code below into to script of the button.
  It uses two other fields if you want to use them. One is a help field
that comes up if you click on the button while the option key is down.
the other is if you want to find all occurances of a word in the
stack and puts a list into  a field called findlist.
 When you click on the find button it will ask you what to find and 
how you want to search. It will the find where that word is and also
puts a find command into the msg box. that way the next time you hit
the return key it will continue to find that word.

on mouseup
  push card
  global username,Askflag
  put word 1 of username into temp
  if the optionkey is down then -- if option key is down show help field
    go to first card
    set visible of Card Field "FindHelp" to B
    not the visible of Card Field "FindHelp"
    exit mouseup
  end if
  
  if the commandkey is down then -- id comand key we want to find all occur.
    put true into specFind
  else
    put false into specfind
  end if
  Put "What would you like to find," into temp1
  if askflag is true then
    Put " else" after word 1 of temp1
  end if
  if temp is empty then
    put "?" into last char of temp1
  end if
  if temp is not empty then
    put " " & temp & "?" after temp1
  end if
  if Specfind then
    put temp1 & "(list)" into temp1
  end if
  ask temp1
  if it is empty then exit mouseup
  put it into findname
  Answer "How do you want me to search:" with "Normal"or "Find String " or B
  "Find Whole"
  if the result="Find String" then
    put 1 into Findlevel
  else if the result="Find Whole" then
    put 2 into Findlevel
  else
    put zero into Findlevel
  end if
  put true into askflag
  
  if specfind then
    specialFind findname,findlevel
    exit mouseup
  end if
  
  DoFind findname,findLevel
end mouseUp


on Specialfind findname
  set lockscreen to true
  put empty into cardcount
  put empty into card field "Findlist"
  put the number of this card into SecondCardcount
  set the cursor to 4
  put true into firsttime
  put empty into list
  repeat until cardcount = SecondCardcount
    go to next card
    dofind findname,findLevel
    
    if firsttime then
      put the number of this card into cardcount
      put false into firsttime
    else
      put the number of this card into SecondCardcount
      put list & item 1 of bkgnd field 1 & item 2 of bkgnd field 1B
      && the name of this card & return & return into list
    end if
 
 end repeat
  go to first card
  set lockscreen to false
  put list into card field "FindList"
  send "mouseup" to card button id 26 --the id of the "show find list" button
end SpecialFind

on DoFind findname,findlevel
  if Findlevel=0 then
    find findname
  else if Findlevel=1 then
    Find String findname
  else
    Find whole Findname
  end if
  if the result = "not found" then
    beep
    put "Sorry, " & quote & Findname & quote & " not found!" into X
    answer X with "Oops!"
    pop card
    exit Dofind
  end if
  
  put "find " & quote & Findname & quote into message
  hide message
end Dofind

Hope this helps
Matthew Mora
SRI International