About setFilterMask

Date: Wed, 18 Mar 1998 13:40:55 +0100
From: Paul <paul.houx@student-kmt.hku.nl>
Subject: Tip for FileIO Xtra
For all people who haven't found out how "setFilterMask" is used: the format is: setFilterMask(object-instance, filterString), where object-instance is the actual fileIO object you create with:

  set myFile to new(xtra "fileio")
and filterString is a description of the desired type of file. This description has to be in a special format, depending on the platform you use. I finally figured out what that format is.

Macintosh


  "TYPE CRTR" (4 chars, space, 4 chars)
Examples:

  setFilterMask(myFile, "TEXT ttxt") --Simpletext file
  setFilterMask(myFile,"ttro ttxt") --Simpletext Read-Only file
  setFilterMask(myFile,"TEXT R*ch")--BBEdit file

Windows


  "Description,List of extensions" (text, comma, text)
Examples:

  setFilterMask(myFile,"Text Document,*.txt;*.doc")--Text Document
  setFilterMask(myFile,"Microsoft Excel File,*.xls")--Microsoft Excel File

Further More

There seems to be a BUG in FileIO. The workaround requires to always use setFilterMask() before you use either displayOpen() or displaySave(). The updateStage Buglist (www.updatestage.com/buglist.html) reports:
under Win31 only, if you don't specify a filterMask, the file dialog filter is labeled "All files" but no files display. (Win 3.1)
Example of proper textfile reading:

-- readTextFile: reads a textfile and returns contents as string
on readTextFile pathAndName
  set theContents to ""
  set myFile to new(xtra "fileio")
  if objectP(myFile) then
    if not stringP(pathAndName) then
      if the machineType = 256 then
        setFilterMask(myFile,"Text Document,*.txt")
      else
        setFilterMask(myFile,"TEXT ????") --Text document of any creator
      end if
      set pathAndName to displayOpen(myFile)
    end if
    if pathAndName <> EMPTY then
      openFile(myFile,pathAndName,1)
      if not status(myFile) then -- no errors reported
        setPosition(myFile,0)
        set theContents to readFile(myFile)
        closeFile(myFile)
      end if
    end if
  end if
  set myFile to 0
  -- For compatibility, you'll have to remove LINEFEED characters from the file.
  set EOL to RETURN & numToChar(10)
  set theContents to searchReplace(theContents, EOL, RETURN)
  return theContents
end

-- searchReplace: replaces all occurences of oldString with newString
on searchReplace theText, oldString, newString
  set output to ""
  repeat while theText contains oldString
    set position to offset(oldString,theText)-1
    put char 1 to position of theText after output
    put newString after output
    delete char 1 to (position + length(oldString)) of theText
  end repeat
  put theText after output
  return output
end