Macromedia Director: Find in all scripts function

Director Message Window I often find myself using ctrl-F in Macromedia Director to look for a piece of code. It’s quite useful and I use it to hop around from script to script a lot instead of finding and clicking on them in the cast window.

However, occassionally I’m searching for a needle in a haystack, and Director’s Find and Find Again (ctrl-g) aren’t very good for that. Especially when the needle I’m searching for is a common word that occurs a lot in comments.

I wrote this piece of code to solve that problem. It’s a find command that’s run from the Message Window. It will search through all the scripts in the movie looking for the word you entered, and by default it excludes comments. It outputs its results in the message window, which makes it easy to scan, and you won’t lose your place in your current script.

For example:
find "paster"
will find all occurances of “paster” in all your scripts, excluding comments. It will output it like this:
	

####### initscript (82,1)"
-- "2 global paster"
-- "

####### initscript: on beginsprite me (82,1)"
-- "8 genimage(paster)"
-- "9 checkresults(paster)"


It displays all the scriptnames the term was found in (after the “###”) and it displays the handler name if it was found inside of a handler. It displays the line number and the complete text (including comments) of the line it was found on.

To use it, copy it into a global script, and just execute it from the Message Window. Here’s the complete code:
on find itext, iopt
  -- set iopt to #all to include comments in the search.
  -- this looks for itext in every script and puts the line it found it on in the message window.

  -- 01/12/06 Original code by H. Lemoore;  http://blog.hanfordlemoore.com/

  put "Finding" &&  itext & ":"
  if iopt <> #all then
    put "(Comments excluded, use #all to include them)"
  end if

  icastcount      = the number of castlibs

  repeat with icast = 1 to icastcount
    imembCount = the number of members of castlib(icast)

    repeat with imemb = 1 to imembCount
      if voidp(member(imemb, icast).script) = false then
        iscript = member(imemb, icast)

        ishowname = 0 -- flag to render the handler/script once only
        handlername = ""

        ilines = iscript.scripttext.lines.count
        ist = iscript.scripttext

        repeat with i = 1 to ilines -- Step through every line of code
          if ist.line[i].word[1] = "on" then
            -- Include the name of the behavior
            handlername =  ": " & ist.line[i]
            ishowname = 0
          end if

          irender = 0  -- flag to determine whether or put search results
          itextoffset = offset(itext, ist.line[i])

          if itextoffset > 0  then --if it was found
            ifound = ist.line[i]
            ig= offset("--",ist.line[i]) -- Check to see if it was a comment line

            if ig >  0 then
              if ig  > itextoffset then
                -- the comment dashes were after the found text, so render it
                irender = 1
              else  -- text was found inside of a comment
                if iopt = #all then
                  irender = 1
                end if
              end if
            else
              irender = 1
            end if

            --VV-- Display the line
            if irender = 1 then
              if ishowname = 0 then
                put  return & "#######" && iscript.name & handlername && "(" & imemb & "," & icast & ")"
                ishowname = 1
              end if
              put i && ifound
            end if
            --AA--
          end if
        end repeat
      end if
    end repeat
  end repeat
end

Leave a Reply

For security, enter the word TURING below:
Comments RSS feed