-- check if the field exists, otherwise create one
if the number of member pFieldName < 0 then
set newField = new(#field)
set the name of member newField to pFieldName
end if
-- check which mode
case pFileMode of
"save" : saveFile me
"open" : openFile me
otherwise
nothing
end case
end
on saveFile me
-- check if an instance is already open
if objectP(aFile) then set aFile = 0
-- create a new instance of the xtra
set aFile = new(xtra "fileio")
-- put the text of the field into a variable
set textStoreRoom = the text of field pFieldName
-- set the filermask for the file to save
setfiltermask (aFile, pFileType)
-- display the "Save as" dialog window
set pFileToSave = displaySave( aFile, "Save text as...", "*.*")
-- make the file
createFile(aFile, pFileToSave)
-- check for the file
if status(aFile) = 0 then
-- open the file with R/W access to write the data to it
openFile(aFile, pFileToSave, 0)
-- now write your text to the file
writeString( aFile, textStoreRoom)
else
-- check for errors
alert "Status: "&error(aFile,status(aFile))
end if
-- finally close the file
closeFile (aFile)
-- get rid of the instance
set myFile = 0
end
on openFile me
-- check if an instance is already open
if objectP(aFile) then set aFile = 0
-- create a new instance of the xtra
set aFile = new(xtra "fileio")
-- set the filermask for the file to open
setfiltermask (aFile, pFileType)
-- display the "Open" dialog window
set pFileToOpen = displayOpen(aFile)
-- check for the file
if not voidP(pFileToOpen) and not (pFileToOpen = EMPTY) then
-- it's there so let's open it to read
openFile(aFile, pFileToOpen, 1)
if status(aFile) = 0 then
-- read the file into a variable
set textStoreRoom = readFile(aFile)
-- clear the target field
set the text of field pFieldName to EMPTY
-- put the stored text into the target field
put textStoreRoom into field pFieldName
else
-- something went wrong...
alert error(aFile,status(aFile))
end if
end if
-- finally close the file
closeFile(aFile)
-- get rid of the instance
set aFile = 0
end
on getPropertyDescriptionList
set lFileModeList = ["open","save"]
set lFileTypesList = ["All files, *.*", "Text files,*.txt", "HTML files, *.html"]
set p_list = [ ¬
#pFileMode: [ ¬
#comment:"Select the file mode:",¬
#format: #string,¬
#default:getAt(lFileModeList,1),¬
#range:lFileModeList ],¬
#pFileType: [ ¬
#comment:"Select the file type:",¬
#format: #string,¬
#default:getAt(lFileTypesList,1),¬
#range:lFileTypesList ],¬
#pFieldName: [¬
#comment:"Select the field:",¬
#format:#field,¬
#default:""]¬
]
return p_list
end
on getBehaviorDescription
return ¬
"Simple behavior to open or save a file with the click of a button." & RETURN & "Display an open-dialog and write the contents of a file to a field or save the contents of a field to a file." & RETURN & "Important: This behavior requires the fileio xtra !" & RETURN & RETURN & "Drag the behavior to a sprite, specify the file mode, the file type you want to open or save and the field for your operation." & RETURN & "This behavior currently opens or saves HTML and plain text files. The error checking is very limited so feel free to add error checking routines, more file types or other features. Also, let me know if you have any request for additional features." & RETURN & RETURN & "written by Martin Kloss, November 1997" & RETURN & "email: martin@triquart-partner.de"
end
Contact
MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA