Importing Text On the Fly

Two examples:
  1. Alan Florence
  2. Glenn Pilcher

Date:    Thu, 9 Mar 1995 10:03:25 -0500
From:    Alan Florence 
Subject: Re: Importing text on the fly
Here are some actual scripts i used last year to get text into the cast on the fly, based on the value entered by the user :
 
--
-- Programming by Alan Florence, copyright Alan Florence 1994, all rights reserved
-- (NOTE: substitute a special character "token" for all "=A5" instances in this script)
 
on readFile
  global readObj, name, companyFound, textCast
 
  if objectP(readObj) then
    readObj(mDispose)
  end if
  set companyFound = FALSE
  if name = "?" then  -- user may enter a "?" to select the file to load
    set readObj = FileIO(mNew,"?read","TEXT")
    if objectP(readObj) then
      set name = readObj(mFileName)
      if name starts the pathname then
        set start = length(the pathname) + 1
        if the machineType = 256 then -- ibm
          set stop = length(name) - 4
        else
          set stop = length(name)
        end if
        set name = chars(name,start,stop)
      end if
      set the text of cast "company" to name
    end if
  else
    if the machineType = 256 then -- ibm
      set readObj = FileIO(mNew,"read",the pathname & name & ".tex")
    else
      set readObj = FileIO(mNew,"read",the pathname & name)
    end if
    -- put readObj && objectP(readObj)
  end if
 
-- the file read is broken into segments, each segment going into a
different cast member as defined by the list textCast. In the file the
segments are separated by the "^" (caret) symbol. The text to be loaded
from the file is terminated by the YEN symbol.
 
  if objectP(readObj) then
    set companyFound = TRUE
    put readObj(mReadToken,"","=A5") into content
   -- put  content
    set mark = "^"
    set start = 1
    repeat with i = 1 to count(textCast)
      set stop = offset(mark,content) - 1
      if stop < 1 then
        set stop = length(content)
      end if
      set the text of cast getAt(textCast,i) to chars(content,1,stop)
      --    put getAt(textCast,i) && chars(content,1,stop)
      -- put stop && length(content)
      set start = stop + 2
      set hold to chars(content, start, length(content))
      set content = hold
    end repeat
  end if
  -- put "companyFound" && companyFound
end readFile
 
on writeFile
  global name, writeObj, textCast
 
  if name = " " then
    set name to the text of cast "company"
  end if
  --  put "writing file" && name
  set mark = "^"
 
  if objectP(writeObj) then
    writeObj(mDispose)
  end if
 
  if the machineType = 256 then -- ibm
    set writeObj = FileIO(mNew,"write",the pathname & name & ".tex")
  else
    set writeObj = FileIO(mNew,"write",the pathname & name)
  end if
 
  if objectP(writeObj) then
    set content = the text of cast getAt(textCast,1)
    repeat with i = 2 to count(textCast)
      set content = content && mark & the text of cast getAt(textCast,i)
    end repeat
    set content = content && "=A5"
    set code = writeObj(mWriteString,content)
    -- put "writefile return code" && code
    writeObj(mDispose)
  end if
 
end writeFile
 

Date:    Wed, 8 Mar 1995 20:58:00 CST
From:    "Glenn M. Picher" 
Subject: Re: Importing text on the fly
In a movie script, use this Lingo handler:
on readtextfile filename
  if not stringP(filename) then
    alert "readtextfile(): bad filename"
    return ""
  end if
  global fileioobject
  if objectP(fileioobject) then fileioobject(mDispose)
  put FileIO(mNew, "read", filename) into fileioobject
  if not objectP(fileioobject) then
    alert "readtextfile(): file missing"
    return ""
  end if
  put fileioobject(mReadFile) into returnstring
  fileioobject(mDispose)
  return returnstring
end
Example: to use the handler to put a text file into a text cast member "Names", use the Lingo:
   put readtextfile("names.txt") into field "Names"
Or store the text file in a string variable "names", use:
   put readtextfile("names.txt") into names
Note that in Director for Windows, you need to:
    openxlib "fileio.dll"
within projectors, either with LINGO.INI or in the movie itself. FILEIO.DLL should be in the same directory as the projector.

Also on Windows, text files created with standard DOS/Windows tools will include linefeed characters which show up as blocks in Director text cast members. You can strip them by testing for numtochar(10).

--Glenn M. Picher  gmpicher@isdn.net  gmpicher@pilot.njin.net
--Phone 502/745-0936  Fax 502/796-8102  24-hr voice mail 502/745-0365
--1366 Center St., Bowling Green, KY 42101
--Multimedia & animation programmer/artist, SPC, Nashville, TN