Version 11 of tk_getSaveFile

Updated 2004-09-03 14:16:43

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

manual page for tk_getSaveFile: http://www.tcl.tk/man/tcl8.2.3/TkCmd/getOpenFile.htm


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.


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 identical on them all. On Windows, it uses the native dialog box. I'm not sure about MacOS, 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...).

 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

See also:


Category Dialog - Category Command - Tk syntax help - Arts and Crafts of Tcl-Tk Programming