FileType

From Xojo Documentation

Class (inherits from Object)

Specifies types of files for use by your application.

Properties
Extensions Name
Icon fa-lock-32.png UTI
File Type Set Properties
All fa-lock-32.png

Notes

Dynamically created File Types do not have access to all the properties that are available for File Types created in the File Types Set Editor.

You can use the FileType class to dynamically create file types, although dynamically created File Types do not have access to all the properties that are available for File Types created in the File Types Set Editor. Specifically, you cannot use All, UTI or Icon.

The FileType class has a built-in conversion to String. This enables you to use a FileType object anywhere you would normally use a String to indicate the file type. It also has built-in addition operators that let you add two FileTypes, or a FileType and a String, to get a String. The result is the name of the FileType joined with the other one (or the String) with a semicolon. This is the format required by functions like FolderItem.ShowOpenFileDialog. These built-in operators make it possible to work with FileType objects as if they were strings. The example illustrates the operators. You can, of course, specify the file type as a string by passing its name.

File Type Sets

In the File Types Set Editor, each file type is shown as one row in the list. A File Type Set created in the File Types Set Editor has the String property All that returns all of the file types in the set. Use All instead of multiple file types when you want to refer to the group of file types. Suppose you create a File Type Set called ImageTypes in which you specify all of the valid image types that your application can open. You can specify the entire list of image types with a line such as:

f = FolderItem.ShowOpenFileDialog(ImageTypes.All)

An advantage of using All is that you can modify the members of the ImageTypes set and you won't need to update your code. Otherwise you would have to add each individual file type together to get the entire set:

f = FolderItem.ShowOpenFileDialog(ImageTypes.JPEG + ImageTypes.MacPICT)

(This assumes that the file types JPEG and MacPICT are the members of the ImageTypes set)

Sample Code

The following code dynamically creates two FileTypes, one for JPEG files and one for PNG files. You must provide a value for the Extensions property. It then displays an Open File dialog box that allows the selection of only those two file types.

Var jpegType As New FileType
jpegType.Name = "image/jpeg"
jpegType.Extensions = "jpg;jpeg"

Var pngType As New FileType
pngType.Name = "image/png"
pngType.Extensions = "png"

Var f As FolderItem

// Use the addition and conversion operators to combine the file types
f= FolderItem.ShowOpenFileDialog(jpegType + pngType)

To specify the Display Name using a constant, set the name property equal to the name of the constant. In the following example a global constant, kMyPNGType, has been created in a module.

Var pngType As New FileType
pngType.Name = kMyPNGType // name of a constant
pngType.Extensions = "png"

Var f As FolderItem

f = FolderItem.ShowOpenFileDialog(pngType)

See Also

FolderItem, FolderItemDialog, GetOpenFolderItem, GetSaveFolderItem, OpenDialog, SaveAsDialog classes