beetlebug▾
/

tk_getSaveFile

Shows a dialog to choose the name of a file to save, and gives its path.

tk_getSaveFile ?-option VALUE ...?

Description

Shows a modal dialog of files and folders, waits until the user chooses a name or cancels, and gives the path. A cancel gives an empty string. The file does not have to exist. The dialog gives only the path: the script writes the file.

When the chosen file exists, the dialog asks the user to confirm that it replaces the file, unless -confirmoverwrite is false.

A dialog gives paths, so it needs the file capability. In the jail of a root folder, the path is from the root.

The dialog depends on the backend:

-message exists only with a dialog of the platform, and the dialogs of Windows show no message. The message of a bad option lists the options of the dialog of the backend.

Options

OptionTypeDefaultDescription
-confirmoverwriteboolean1Asks the user before the path of a file that exists is given.
-defaultextensionstringemptyThe extension that the dialog adds to a name with none.
-filetypeslistemptyThe filters of the dialog: a list of file types, each a name and its patterns.
-initialdirpathemptyThe folder that the dialog shows at the start.
-initialfilestringemptyThe name that the dialog shows in the field of the name at the start.
-messagestringemptyA text that the dialog of the platform shows over the files.
-parentwindow.The window that the dialog belongs to.
-titlestringnoneThe title of the window of the dialog.
-typevariablevariable nameemptyA global variable with the name of the file type of the dialog.

-confirmoverwrite BOOLEAN

Asks the user before the path of a file that exists is given.

With a true value, a name of a file that exists opens a message box that asks to replace the file. A no goes back to the dialog. With a false value the dialog gives the path at once.

% tk_getSaveFile -confirmoverwrite 0
% tk_getSaveFile -confirmoverwrite maybe
expected boolean value but got "maybe"
-errorcode NONE

-defaultextension EXTENSION

The extension that the dialog adds to a name with none.

A name with no extension, such as notes, gets the extension, such as notes.txt. The value can start with a dot or not.

% tk_getSaveFile -defaultextension .txt

Any text is an extension.

-filetypes LIST

The filters of the dialog: a list of file types, each a name and its patterns.

Each element is a list of a name and a list of extensions or patterns, such as {Images {.png .jpg}}, with an optional third element of Mac file types. An extension .txt matches *.txt, and * matches each file. The dialog shows the files of the first type at the start, and the user can select another type.

% tk_getSaveFile -filetypes {{Text {.txt .md}} {All *}}
% tk_getSaveFile -filetypes {{Text}}
bad file type "Text", should be "typeName {extension ?extensions ...?} ?{macType ?macTypes ...?}?"
-errorcode NONE

-initialdir FOLDER

The folder that the dialog shows at the start.

In the dialog that ztk draws, an empty value or a folder that does not exist shows the current folder.

% tk_getSaveFile -initialdir /nonexistent

A folder that does not exist is not an error.

-initialfile NAME

The name that the dialog shows in the field of the name at the start.

The user can keep the name or change it.

% tk_getSaveFile -initialfile notes.txt

Any text is a name.

-message TEXT

A text that the dialog of the platform shows over the files.

The option exists with a dialog of the platform. The dialogs of Windows show no message, and the dialog that ztk draws has no such option.

% tk_getSaveFile -message "Name the copy."

Any text is a message.

-parent WINDOW

The window that the dialog belongs to.

The dialog shows over the toplevel of WINDOW. The window must exist.

% tk_getSaveFile -parent .
% tk_getSaveFile -parent .nope
bad window path name ".nope"
-errorcode NONE

-title TEXT

The title of the window of the dialog.

The default is Save As, in the language of msgcat for the dialog that ztk draws.

% tk_getSaveFile -title "Export the picture"
% tk_getSaveFile -title
value for "-title" missing
-errorcode NONE

-typevariable VARNAME

A global variable with the name of the file type of the dialog.

When the variable has the name of a type of -filetypes, the dialog shows that type at the start. After the user chooses a name, the variable gets the name of the type that the dialog showed. A cancel leaves the variable as it is.

# The cancel of the headless backend sets no variable.
% unset -nocomplain ::kind
> tk_getSaveFile -typevariable ::kind -filetypes {{Text .txt} {All *}}
> info exists ::kind
0

Any text is a name of a variable.

Returns

The path of the file, or an empty string after a cancel.

Examples

# The headless backend answers with a cancel.
% tk_getSaveFile -title "Save the script" -filetypes {{Tcl .tcl} {All *}}

Errors

# The list of the headless backend, which has the options of a dialog of the platform.
% tk_getSaveFile -name a.txt
bad option "-name": must be -confirmoverwrite, -defaultextension, -filetypes, -initialdir, -initialfile, -message, -parent, -title, or -typevariable
-errorcode NONE

% tk_getSaveFile -title
value for "-title" missing
-errorcode NONE

Differences from Tcl

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztk 1.0.0-beta.1

See also