Working with File IO - Exporting text from Director
Exporting and importing information with Director is done using the FileIO
Xtra. Below outlines how to create a text file from Director, write text
to it, open an existing text file and read it in Director.
1. In the message window type:
scribe = new (xtra "FileIO")
Nothing seems to have happened, but behind the scenes an object has been
created. Now we have to know how to communicate with this object.
2. In the message window type:
put mMessageList (xtra "FileIO")This
should produce a bit of info as shown below:
-- "xtra fileio -- CH 18apr97
new object me -- create a new child instance
-- FILEIO --
fileName object me -- return fileName string of the open file
status object me -- return the error code of the last method called
error object me, int error -- return the error string of the error
setFilterMask object me, string mask -- set the filter mask for dialogs
openFile object me, string fileName, int mode -- opens named file. valid
modes: 0=r/w 1=r 2=w
closeFile object me -- close the file
displayOpen object me -- displays an open dialog and returns the selected
fileName to lingo
displaySave object me, string title, string defaultFileName -- displays
save dialog and returns selected fileName to lingo
createFile object me, string fileName -- creates a new file called fileName
setPosition object me, int position -- set the file position
getPosition object me -- get the file position
getLength object me -- get the length of the open file
writeChar object me, string theChar -- write a single character (by ASCII
code) to the file
writeString object me, string theString -- write a null-terminated string
to the file
readChar object me -- read the next character of the file and return it
as an ASCII code value
readLine object me -- read the next line of the file (including the next
RETURN) and return as a string
readFile object me -- read from current position to EOF and return as a
string
readWord object me -- read the next word of the file and return it as a
string
readToken object me, string skip, string break -- read the next token and
return it as a string
getFinderInfo object me -- get the finder info for the open file (Mac Only)
setFinderInfo object me, string attributes -- set the finder info for the
open file (Mac Only)
delete object me -- deletes the open file
+ version xtraRef -- display fileIO version and build information in the
message window
* getOSDirectory -- returns the full path to the Mac System Folder or Windows
DirectoryA few keywords keep popping up. These are object
and me, which are indicators of the parent/child structure
which we will substitute with the name of the object. The rest is a list
of commands.
Creating an external file
3. In the message window type:
createFile (scribe, "Multimedia.txt")
What we’ve just done is used the command createFile to create a text file from the object scribe and named Multimedia.txt. One thing we left out was the location where the text file should be saved to (the above will just use default settings).
If we wanted write to specify the path we could use the property the pathname in a command like:
createFile (scribe, the moviePath&"Multimedia.txt")
That should write the file to the same folder as the movie from which the
command is issued. Type the following in the Message window:
put the moviePath
We can specify a file in a directory relative to the current pathname by
using moviePath&"text/deans.txt"
The section in the “ ” represents the pathname and filename. deans.txt is in
a subdirectory, text, below the current directory of the movie.
We can also use the @ operator followed by the path structure using either
/ (forward slash) \ (backslash) : (colon)
Example 1: These are equivalent expressions that specify the subfolder
bigFolder, which is in the current movie’s folder:
@/bigFolder
@:bigFolder
@\bigFolder
Example 2: These are equivalent expressions that specify the file
linkedFile, in the subfolder bigFolder, which is in the current movie’s
folder:
@:bigFolder:linkedFile
@\bigFolder\linkedFile
@/bigFolder/linkedFile
Example 3: These are equivalent expressions that specify the file
linkedFile, which is in the folder otherFolder. The otherFolder folder is
in the folder one level up from the current movie’s folder.
@::otherFolder:linkedFile
@\\otherFolder\linkedFile
@//otherFolder/linkedFile
Search the @ pathname operator under the help file for more examples
Back to the text file….
Opening the file
If you open Multimedia.txt, you will see it’s
empty because we’ve simply created it, we haven’t written to it. Before
we can write to the file, we need to open it.
Looking at the mMessageList documentation we find the syntax:
openFile object me, string fileName, int mode
-- opens named file. valid modes: 0=r/w 1=r 2=w
Now this may look strange, but is not all that complicated. The bit about
“valid modes” relates to reading and writing the file. If the value is set
to 0, the object is allowed to read and write. If set to 1, the object can
read only, and 2 allows write only. This is mainly a network us, where many
people might need access to the same data.
4. In the message window type:
openFile (scribe, the moviePath&“multimedia.txt”,
0)
Again nothing appears to have happened, but the file is now opened in RAM.
Exporting to the file
Now to put some text into it.
5. In the message window type:
writeString (scribe, “Exporting text in Director is
easy!”)
You’ll note we don’t need to the name of the file, the object has it open.
Objects can’t open more than one file at a time.
We could have entered the entire contents of a text cast member to the file
by typing:
writeString (scribe, the text of member “story”)
Reading the file
We’ve just output data to the text file. To input, all we do is open
the file and read it in Director as a text string. You can place the text
into a text or field cast member. Create a text cast member and field cast
member, giving both names, but no text.
6. In the message window type:
openFile (scribe, the moviePath&"multimedia.txt”,
0)
put readFile (scribe) into member 1
closeFile(scribe)
Open text/field member 1
Try the above again using the cast number of the field member
Disposing the object
The scribe object like all other objects,
instances of Xtras stick around taking up memory unless you specifically
get rid of them, which is what you should do now.
7. In the message window type:
scribe=0
Macromedia have a How
to use the FileIO Xtra sample movie here.
and that is FileIO in a nutshell.