Livecode Wiki
Advertisement

Displays a standard file dialog for the user to select a file. The dialog box displayed is the same one most programs use for the "Open" command in the File menu.

Built-in Message handler[]

Syntax:

answer file[s] <prompt> [with <defaultPath>] [with type types [or type types...]] [titled <windowTitle>] [as sheet]

AnswerFile

OS supported: mac,windows,linux


Examples:

answer files "Select the images you wish to view:" with type "JPEG Images|jpg|JPEG"
answer file "Select an image" with type ("JPEG files|jpg,jpeg|JPEG,JFIF" & return & "Any file||*")
answer file "Select a file to delete:"
if the result is not "Cancel" then
   put it into tChosenFile
   -- Use the file path as required
end if
answer files "Select the files you wish to process:"
if the result is not "Cancel" then
   put it into tChosenFiles
     repeat for each line tFile in tChosenFiles      
      -- do something on files
    end repeat
end if
answer file "Input:" with "/Macintosh HD/"

Parameters:

  • prompt: If you specify empty, no prompt appears.
  • defaultPath: The name and location of the folder whose contents are listed when the dialog box appears. If no defaultPath is specified, the dialog box lists the contents of the last folder you used with a file dialog box.
  • windowTitle: If specified, appears in the title bar of the dialog box. If no windowTitle is given, the title bar is blank. This parameter has no effect on Mac OS systems, because Mac OS file dialog boxes don't have a title bar.
  • Use the types parameter to specify which files should appear and be available for selection. Each set of types is a return-delimited list of values of the form "tag|extensions|filetypes".
  • The result: If the user cancels the dialog, the it variable is set to empty, and the result function returns "Cancel".
  • It: The answer file command places the absolute file path(s) of the selected file(s) as a return delimeted list in the it variable.

Important: The answer file command does not open the file. It only displays the dialog box and retrieves the path to the file the user specifies.

If the as sheet form is used, the dialog box appears as a sheet on OS X systems. On other systems, the as sheet form has no effect and the dialog box appears normally. Attempting to open a sheet from within another sheet displays the second stack as a modal dialog box instead. To give a dialog box a prompt when using the as sheet form a non-empty title must be provided. This will cause the prompt to appear in the same place it would if as sheet was not being used.

If the systemFileSelector property is set to false, LiveCode's built-in dialog box is used instead of the operating system's standard file dialog.

If you wish to filter the list of the files presented to the user, use the answer file with type command.

Note[]

Not explicit in the docs above is that the effect of specifying extensions and filetypes in a filter is additive not subtractive: the user will be offered any file matching the extensions, and any file matching the filetype, not just the files that match both. So using a filter of "Image files|png|JPEG" would match any files with a ".png" extension, regardless of filetype; and any files with a "JPEG" filetype, regardless of extension.

Not noted in the docs above is that you can use multiple comma-delimited items in the 'extensions' part of a file types filter. So for example you could use the filter "JPEG files|jpg,jpeg|JPEG,JFIF" to catch files that match at least one of four tests: extension either ".jpg" or ".jpeg", type either "JPEG" or "JFIF".

Finally, note that whereas "*" in the extension list literally matches files with the extension ".*", "*" in the filetype list is a wildcard that matches any file. So for example you could use the following filter list to ask the user to select a JPEG file matching some broad criteria - with the option to let them select any file if they think it might work:

   answer file "Select an image" with type ("JPEG files|jpg,jpeg|JPEG,JFIF" & return & "Any file||*")

You can also use a wildcard "*" in the filetypes section - however, this appears to override

If the user cancels the dialog "the results" and "it" are both EMPTY. If the user selects a file "the results" contains the string of the type choosen (ie "JPEG Images" from the example above)

According to this post ( http://lists.runrev.com/pipermail/use-revolution/2009-August/126806.html ) the "with filter" form is depricated. It does not work in OS X (10.5.8). Use the "with type" form instead. For example, this does not work:

answer file "Select the text file" with filter "Text File,*.txt"

But this does work:

answer file "Select the text file" with type "Text|txt"


See Also[]

See also: dontUseNS (property), ask file (command), revCopyFile (command), open file (command), answer (command), answer folder (command), revUnixFromMacPath (function), files (function), revMacFromUnixPath (function)

Advertisement