From: nubody on
Hello,

I am putting together a script that lets the user select a file.
However, it seems that each time I use tk_getOpenFile, it starts off
from the current script folder instead of where the user made the last
selection. It becomes cumbersome to navigate to the same place again
and again. Is there keyword that I can use so it remembers the last
folder? I am using Tcl 8.4 on Windows XP.
From: Torsten Berg on
On 16 Jul., 20:48, nubody <nub...(a)nospammers.dom> wrote:
> Hello,
>
> I am putting together a script that lets the user select a file.
> However, it seems that each time I use tk_getOpenFile, it starts off
> from the current script folder instead of where the user made the last
> selection.  It becomes cumbersome to navigate to the same place again
> and again.  Is there keyword that I can use so it remembers the last
> folder?  I am using Tcl 8.4 on Windows XP.

You want to use the option -initialdir. Something like

tk_getOpenFile -initialdir $someDirectory

where $someDirectory is a variable holding the last used directory.
The dialog does not itself keep track of that.

Torsten
From: Kaitzschu on
On Wed, 16 Jul 2008, Torsten Berg wrote:

> On 16 Jul., 20:48, nubody <nub...(a)nospammers.dom> wrote:
>> Hello,
>>
>> I am putting together a script that lets the user select a file.
>> However, it seems that each time I use tk_getOpenFile, it starts off
>> from the current script folder instead of where the user made the last
>> selection. �It becomes cumbersome to navigate to the same place again
>> and again. �Is there keyword that I can use so it remembers the last
>> folder? �I am using Tcl 8.4 on Windows XP.
>
> You want to use the option -initialdir. Something like
>
> tk_getOpenFile -initialdir $someDirectory
>
> where $someDirectory is a variable holding the last used directory. The
> dialog does not itself keep track of that.

And the easiest way to achieve that is to use a wrapper. Since I'm around,
you'll get an overkill full of 8.5'isms, but these should be relatively
easy to fix.

[code]

# first, we need a place to keep our directories
variable mem_tk_getOpenFile [dict create]

proc mem_tk_getOpenFile {args} {
variable mem_tk_getOpenFile
# next we'll have to find out who called us
set caller [dict get [info frame -1] proc]
if {[dict exists $mem_tk_getOpenFile $caller]} {
# oh great, we have a previous location!
catch {
# we [catch] to prevent failures for unbalanced $args
dict set args -initialdir [dict get $mem_tk_getOpenFile $caller]
}
}
# in case of unbalanced args, tk_gOF will bark an error
set file [tk_getOpenFile {*}$args]
# now, if user selected a file, we'll remember the location
# otherwise, we'll just use the previous location next time
if {$file ne {}} {
dict set mem_tk_getOpenFile $caller [file dirname $file]
}
# and to be compliant with the wrapped command
return $file
}

[/code]

And now for some explaining.

Procedure [mem_tk_getOpenFile] wraps [tk_getOpenFile], using previously
returned file location as next invocation's -initialdir. Overkill part
comes from the fact, that [mem_tk_gOF] remembers initial directory for
each calling procedure (which, if I may say so, is pretty handy, in case
you are opening mixed sequentially stuff from two directories from two
different procedures; for example opening photos from one place and brush
templates from another).

If this feature is unnecessary (or troublemaker) then the solution is a
bit shorter:

[code]

# first, we need a place to keep our directories
variable mem_tk_getOpenFile {}

proc mem_tk_getOpenFile {args} {
variable mem_tk_getOpenFile
# we can use empty value by default, it should work
catch {
# we [catch] to prevent failures for unbalanced $args
dict set args -initialdir [dict get $mem_tk_getOpenFile $caller]
}
# in case of unbalanced args, tk_gOF will bark an error
set file [tk_getOpenFile {*}$args]
# now, if user selected a file, we'll remember the location
# otherwise, we'll just use the previous location next time
if {$file ne {}} {
set mem_tk_getOpenFile [file dirname $file]
}
# and to be compliant with the wrapped command
return $file
}

[/code]

Getting rid of 8.5'isms is left to an interested reader who isn't
interested in updating their systems.

--
-Kaitzschu
set s TCL\ ;wh 1 {pu [set s [st ra $s 1 3][st in $s 0]]\033\[1A;af 99}

"Good thing programmers don't build bridges
[the same way as Kaitzschu writes code]."
--John Kelly in comp.lang.tcl
From: ZB on
Dnia 16.07.2008 Kaitzschu <kaitzschu(a)kaitzschu.cjb.net.nospam.plz.invalid> napisa�/a:

> And the easiest way to achieve that is to use a wrapper. Since I'm around,
> you'll get an overkill full of 8.5'isms, but these should be relatively
> easy to fix. [..]

"Shooting a sparrow using a cannon". But what for, actually?


# Somewhere in the initialization module
set ::initialDir ""


# When you need to open file

if { $::initialDir eq "" } { set fileName [tk_getOpenFile -initialdir [pwd]]
} else { set fileName [tk_getOpenFile -initialdir $::initialDir]
}

if { $sourceFilename ne "" } { set ::initialDir [file dirname $fileName]
}


....that's enough.
--
ZB
From: ZB on
Dnia 16.07.2008 ZB <zbREMOVE_THIS(a)AND_THISispid.com.pl> napisa�/a:

> if { $sourceFilename ne "" } { set ::initialDir [file dirname $fileName]
^^^^^^^^^^^^^
$fileName, not $sourceFilename, of course
--
ZB