tk_getSaveFile

This call will pop up a dialog box for the user to select a file to save.

To read the manual page for tk_getSaveFile and tk_getOpenFile online, see:


SYNOPSIS

tk_getSaveFile ?option value ...?

DESCRIPTION

The procedure tk_getSaveFile pops up a dialog box for the user to select a file to save. The tk_getSaveFile command is usually associated with the Save as command in the File menu. If the user enters a file that already exists, the dialog box prompts the user for confirmation whether the existing file should be overwritten or not.


On Unix, when typing a filename without extension, the first extension belonging to the currently selected filetype will be appended to the filename. On Windows, this is not the case, but one can define a fixed extension with the -defaultextension option. To get the same behaviour as on Unix, use -defaultextension {}.


How to suppress displaying of hidden directories in Linux (starting with a dot ".") was described in tk_getOpenFile.


caspian: I have used tk_getSaveFile on Windows, and know that it works fine with this operating system. Will it work in a TCL script executed in a UNIX environment?

It works in a Tk script:

  package require Tk
  set a [tk_getSaveFile]
  puts $a

produces a dialog box for you to specify a file name, with the behavior as mentioned above. Of course, the dialog itself does not write out the file - it only returns the file name for your script to use.

MG - tk_getSaveFile works on all platforms, though it won't be identical on them all. On Windows, it uses the native dialog box. I'm not sure about MacOS [NEM-'tis native on Mac OS X], but I believe Unix uses a pure-Tcl dialog box (which has the advantage that you can edit it).

And, for the heck of it, here's a very basic example of how to use tk_getSaveFile to save the contents of a text widget (.t) into a file (although only the first few lines of the saveAs proc really relate to tk_getSaveFile...).

 package require Tk

 # set up the demo frame
 pack [text .t] -expand 1 -fill both -side top
 pack [button .b -text "Save As..." -command "saveAs"] -side top

 proc saveAs {} {

   set file [tk_getSaveFile -title "Crappy Save Example" -parent .]
   if { $file == "" } {
        return; # they clicked cancel
      }
   set x [catch {set fid [open $file w+]}]
   set y [catch {puts $fid [.t get 1.0 end-1c]}]
   set z [catch {close $fid}]
   if { $x || $y || $z || ![file exists $file] || ![file isfile $file] || ![file readable $file] } {
   tk_messageBox -parent . -icon error \
                 -message "An error occurred while saving to \"$file\""
      } else {
   tk_messageBox -parent . -icon info \
                 -message "Save successful"
      }
 };# saveAs

KPV One OS X oddity, if you specify a path instead of just a file in -initialfile you get a very odd result. Luckily the solution is easy (if you're aware of the problem):

tk_getSaveFile -initialfile /tmp/foo
/tmp/:tmp:foo
tk_getSaveFile -initialfile [file tail /tmp/foo] -initialdir [file dirname /tmp/foo]
/tmp/foo

See also: