CommonDialog Reference

Reference for the CommonDialog object, which handles the "Open" and "Save As" dialogs (Win32 OPENFILENAME).

Notation: P = property (read/write), M = method. Member names are matched case-insensitively.

Table of Contents

  1. How to Obtain
  2. Methods
  3. Properties
  4. OFN_ENABLEHOOK (Folder Selection)
  5. Notes
  6. Constants
  7. Examples

1. How to Obtain

const commdlg = api.CreateObject("CommonDialog");   // recommended
const commdlg2 = te.CommonDialog;                   // te.CommonDialog is also the same (a new object is created on every reference)

2. Methods

Name Return value Description
ShowOpen() bool Shows the "Open" dialog. true on OK, false on cancel or failure. The selected result is stored in FileName. If Flags includes OFN_ENABLEHOOK, folders and similar items can also be selected (Chapter 4)
ShowSave() bool Shows the "Save As" dialog. The return value and the handling of the result are the same as for ShowOpen. There is no special handling of OFN_ENABLEHOOK

Both of them, if the file name buffer has not been allocated yet before showing the dialog, allocate MaxFileSize characters and initialize it with an empty string.


3. Properties

Name Type Description
FileName string The initial file name of the dialog / the path of the selection result. An assigned string is truncated to MaxFileSize characters
InitDir string The folder shown first
Filter string The file types. The format is "description|pattern|description|pattern…", using | as the separator (converted to NUL internally). Example: "Text|*.txt|All files|*.*". Multiple patterns can be listed with ; ("*.jpg;*.png"). When read, the contents of InitDir are returned (because of how it is implemented), so do not use it for reading
FilterIndex int The number of the selected filter (1-based). 0 means unspecified (the "custom filter")
DefExt string The default extension added when the extension is omitted (without .; for example "txt")
Title string The title of the dialog
Flags int OFN_* flags (see Chapter 6 below)
FlagsEx int Reads and writes OPENFILENAME.FlagsEx (the extended flags). It is a separate value from Flags, and its initial value is 0. It is passed to both ShowOpen and ShowSave. The values you can specify follow the Windows SDK definitions (OFN_EX_NOPLACESBAR = 1 in the SDK)
MaxFileSize int The number of characters in the FileName buffer

4. OFN_ENABLEHOOK (Folder Selection)

If you include OFN_ENABLEHOOK (0x20) in Flags for ShowOpen, TE's own behavior applies (it has no effect with ShowSave).

OpenDialogEx in sync.js sets this flag when bFilesOnly is false, so that both files and folders can be selected.


5. Notes


6. Constants

Defined in consts.js.

OFN_* (Flags) OFN_READONLY = 1 / OFN_OVERWRITEPROMPT = 2 / OFN_HIDEREADONLY = 4 / OFN_NOCHANGEDIR = 8 / OFN_SHOWHELP = 0x10 / OFN_ENABLEHOOK = 0x20 / OFN_ENABLETEMPLATE = 0x40 / OFN_ENABLETEMPLATEHANDLE = 0x80 / OFN_NOVALIDATE = 0x100 / OFN_ALLOWMULTISELECT = 0x200 / OFN_EXTENSIONDIFFERENT = 0x400 / OFN_PATHMUSTEXIST = 0x800 / OFN_FILEMUSTEXIST = 0x1000 / OFN_CREATEPROMPT = 0x2000 / OFN_SHAREAWARE = 0x4000 / OFN_NOREADONLYRETURN = 0x8000 / OFN_NOTESTFILECREATE = 0x10000 / OFN_NONETWORKBUTTON = 0x20000 / OFN_NOLONGNAMES = 0x40000 / OFN_EXPLORER = 0x80000 / OFN_NODEREFERENCELINKS = 0x100000 / OFN_LONGNAMES = 0x200000 / OFN_ENABLEINCLUDENOTIFY = 0x400000 / OFN_ENABLESIZING = 0x800000 / OFN_DONTADDTORECENT = 0x2000000 / OFN_FORCESHOWHIDDEN = 0x10000000

A commonly used combination: OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_ENABLESIZING | OFN_HIDEREADONLY | OFN_NODEREFERENCELINKS (the same as OpenDialogEx in sync.js)


7. Examples

// Open a file
const commdlg = api.CreateObject("CommonDialog");
commdlg.InitDir = "C:\\Users";
commdlg.Title = "Select an image";
commdlg.Filter = "Images|*.jpg;*.png;*.gif|All files|*.*";
commdlg.FilterIndex = 1;
commdlg.Flags = OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_ENABLESIZING | OFN_HIDEREADONLY | OFN_NODEREFERENCELINKS;
if (commdlg.ShowOpen()) {
    const path = commdlg.FileName;
}

// Save as (confirm before overwriting an existing file)
const save = api.CreateObject("CommonDialog");
save.FileName = "output.txt";
save.DefExt = "txt";
save.Filter = "Text|*.txt|All files|*.*";
save.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING | OFN_NOCHANGEDIR;
if (save.ShowSave()) {
    const dest = save.FileName;
}

// An "Open" dialog that can also select folders (OFN_ENABLEHOOK)
const dlg = api.CreateObject("CommonDialog");
dlg.InitDir = "C:\\";
dlg.Filter = "All files|*.*";
dlg.Flags = OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_ENABLESIZING | OFN_ENABLEHOOK;
if (dlg.ShowOpen()) {
    const folderOrFile = dlg.FileName;
}

// Enlarge the buffer first, to be ready for long paths
const big = api.CreateObject("CommonDialog");
big.MaxFileSize = 1024;     // before setting FileName
big.FileName = "";

sync.js has OpenDialogEx(path, filter, bFilesOnly) and MakeCommDlgFilter, which builds the filter string. In TE's standard scripts, calling the dialog through these is the common usage.