karen steely <[EMAIL PROTECTED]> wrote:
> 
> blockList=["oneURL", "anotherURL"]
... 
> I want to manage the blockList externally so that if
> the URLs change, I don't have to reload the movie.
> I've got a blocklist.txt file in the same folder as
> movie.
...
> How can I get those URLs into my blockList list?

Hi Karen,

You'll find below three handlers, one for writing a list of strings to a
text file, one for reading the lines of a text file in as items in a list,
and one to show an alert if something goes wrong.

The handlers check at each step that no error has occurred, warns the user
of the problem, and stops trying.  They also ensure that any opened files
are closed again.  That accounts for most of the code.


The vital parts are:

-- Writing to file
  tCount = aList.count
  repeat with i = 1 to tCount
    tItem = aList[i]&RETURN
    tFileIO.writeString(tItem)
  end repeat


-- Reading from file
  tList = []
  repeat while TRUE
    tItem = tFileIO.readLine() -- includes final RETURN
    if tItem = "" then
      exit repeat
    end if
    delete the last char of tItem -- RETURN
    tList.append(tItem)
  end repeat
  return tList


A good way to learn from this would be to print out the code and read
through it, making notes in the margin on the purpose of each section.  Once
you're happy with the way the code works, try to rewrite it from memory (or
just from your notes) in Director.  You can ignore any sections which are
unnecessary in your project (for example, those which allow the user to
choose a file if none is specified).

Cheers,

James

----------------------------------------------------------------------------



on writeOutList(aList, aFileName)
  if not listP(aList) then
    exit
  else
    tCount = aList.count
    if not tCount then
      exit
    end if
  end if
  
  -- If we get here, we have a list with at least one item
  
  tFileIO = xtra("fileio").new()
  if not stringP(aFileName) then
    aFileName = tFileIO.displaySave("Save list as", "Untitled.txt")
    case aFileName of
      "", VOID: --the user canceled the dialog
        exit
    end case
  end if
  
  tFileIO.openFile(aFileName, 0) -- write only
  tStatus = tFileIO.status()
  case tStatus of
    -37: -- "Bad file name" -- The file does not exist: create it
    0: -- The file does exist: overwrite it
      tFileIO.delete()
    otherwise:
      return errorAlert("write to", aFileName)
  end case
  
  tFileIO.createFile(aFileName)
  if tFileIO.status() then
    return errorAlert("create", aFileName)
  end if
  
  tFileIO.openFile(aFileName, 0) -- write only
  if tFileIO.status() then
    return errorAlert("open", aFileName)
  end if
  
  -- If we get here, we have an open file that we can write to
  
  repeat with i = 1 to tCount
    tItem = aList[i]&RETURN
    tFileIO.writeString(tItem)
    if tFileIO.status() then
      tFileIO.closeFile()
      return errorAlert("complete writing to", aFileName)
    end if
  end repeat
  
  tFileIO.closeFile()
  
  return aFileName
end writeOutList



on readInList(aFileName)
  tList = []
  
  tFileIO = xtra("fileio").new()
  if not stringP(aFileName) then
    aFileName = tFileIO.displayOpen()
    case aFileName of
      "", VOID: --the user canceled the dialog
        exit
    end case
  end if
  
  tFileIO.openFile(aFileName, 1) -- read only
  if tFileIO.status() then
    return errorAlert("read from", aFileName)
  end if
  
  repeat while TRUE
    tItem = tFileIO.readLine() -- includes final RETURN
    if tItem = "" then
      exit repeat
    end if
    
    delete the last char of tItem -- RETURN
    tList.append(tItem)
  end repeat

  tFileIO.closeFile()

  return tList
end readInList



on errorAlert(aString, aFileName)
  alert "Could not "&aString&&" the file:"&RETURN&\
QUOTE&aFileName&QUOTE
end errorAlert

[To remove yourself from this list, or to change to digest mode, go to 
http://www.penworks.com/lingo-l.cgi  To post messages to the list, email 
[EMAIL PROTECTED]  (Problems, email [EMAIL PROTECTED]). Lingo-L is for 
learning and helping with programming Lingo.  Thanks!]

Reply via email to