tk_getOpenFile, file extension and the file type drop down list ... 
Author Message
 tk_getOpenFile, file extension and the file type drop down list ...

Hello,

just one question.

Can I decide, while showing the file selection via tk_getOpenFile,
which file type should be used to filter the shown files?

I use the option "-defaultextension", but this does not help, because
it is only for appending an extension, if the selected filename has
none!

Any chance to influence the used file type, when opening this dialog?

The only thing I thought of is to create the file types always the way,
that the extension of the initial file is the first in the file type
list.

Any hint or suggestion?

Best regards,

Martin Lemburg
UGS - Transforming the Process of Innovation



Mon, 29 Sep 2008 00:47:26 GMT  
 tk_getOpenFile, file extension and the file type drop down list ...

  In a message on 12 Apr 2006 09:47:26 -0700, wrote :

"> Hello,
">
"> just one question.
">
"> Can I decide, while showing the file selection via tk_getOpenFile,
"> which file type should be used to filter the shown files?
">
"> I use the option "-defaultextension", but this does not help, because
"> it is only for appending an extension, if the selected filename has
"> none!
">
"> Any chance to influence the used file type, when opening this dialog?
">
"> The only thing I thought of is to create the file types always the way,
"> that the extension of the initial file is the first in the file type
"> list.
">
"> Any hint or suggestion?

Yes.  Check out the -filetypes option.  'man n tk_getOpenFile':

       -filetypes filePatternList
              If a File types listbox exists in the  file  dialog
              on  the  particular platform, this option gives the
              filetypes in this listbox. When the user  choose  a
              filetype  in  the  listbox,  only the files of that
              type are listed. If this option is unspecified,  or
              if  it  is  set  to  the empty list, or if the File
              types listbox is not supported  by  the  particular
              platform  then  all  files are listed regardless of
              their types. See the section SPECIFYING  FILE  PAT-
              TERNS  below  for  a  discussion on the contents of
              filePatternList.

">
"> Best regards,
">
"> Martin Lemburg
"> UGS - Transforming the Process of Innovation
">
">                                                                                                    

Robert Heller             -- 978-544-6933
Deepwoods Software        -- Linux Installation and Administration
http://www.deepsoft.com/  -- Web Hosting, with CGI and Database



Mon, 29 Sep 2008 03:17:06 GMT  
 tk_getOpenFile, file extension and the file type drop down list ...

Quote:

> Hello,

> just one question.

> Can I decide, while showing the file selection via tk_getOpenFile,
> which file type should be used to filter the shown files?

> I use the option "-defaultextension", but this does not help, because
> it is only for appending an extension, if the selected filename has
> none!

> Any chance to influence the used file type, when opening this dialog?

> The only thing I thought of is to create the file types always the way,
> that the extension of the initial file is the first in the file type
> list.

> Any hint or suggestion?

Read the man page (or Activestate's excellent help file which in most
cases is a verbatim copy of the man page anyway). At the bottom of the
page there is an example code which does exactly what you want:

  set types {
    {{Text Files}       {.txt}        }
    {{TCL Scripts}      {.tcl}        }
    {{C Source Files}   {.c}      TEXT}
    {{GIF Files}        {.gif}        }
    {{GIF Files}        {}        GIFF}
    {{All Files}        *             }
  }
  set filename [tk_getOpenFile -filetypes $types]

  if {$filename != ""} {
    # Open the file ...
  }

In this case the dialog box defaults to listing all .txt files
initially. If you modify it to something like:

  set types {
    {{Text Files} {.txt .tcl .c}}
    {{TCL Scripts} {.tcl}}
    {{GIF Files} {.gif}}
    {{All Files} *}
  }

it defaults to listing all .txt, .tcl and .c files initially. The TEXT
and GIFF bit are for MacOS since filetypes are stored in the resource
fork of a file*. Other OSes will ignore them

* note: On the classic Mac the filename extension is just a name and
does not denote file type, you can for example have a gif file called
foo.txt and it will still open in Photoshop when double clicked if the
filetype is GIFF and creator is 8BIM.



Mon, 29 Sep 2008 12:10:37 GMT  
 tk_getOpenFile, file extension and the file type drop down list ...
Hi,

ok - the -filetypes option is already known and used, but the question
is more ... do I really need to manipulate the list of filetypes to
decide which file type is the initial file type?

Only the first file type is the initial file type and so I have to
change the file type list everytime the file selection is used with a
file not matching the first file type!

So - everytime I open the file selection I do following:

    # get the current extension
    #
    set initExt   [file extension $fileName];

    # set the filetypes lists
    #
    set fileTypes {
        {"AutoForm Geometry file" {.af}}
        {"STL file"               {.stl .sla .slb}}
        {"VRML file"              {.wrl}}
        {"all files"              {.*}}
    };

    # get the first matching file type
    #
    set idx 0;

    foreach fileType $fileTypes {
        set exts [lindex $fileType 1];

        if {[lsearch -exact $exts $initExt] >= 0} {
            break;
        }

        incr idx;
    }

    # move the matching file type
    # to beginning of the list
    #
    if {$idx < [llength $fileTypes]} {
        set fileTypes [linsert \
            [lreplace $fileTypes $idx $idx] \
            -1 \
            $fileType \
        ];
    }

Wouldn't it be nice to have a kind of option like "-initialfiletype"
accepting an index to access the first/initial file type from the
-filetypes list to be shown while popping up?

Best regards and a good week start!

Martin Lemburg
UGS - Transforming the Process of Innovation



Sat, 04 Oct 2008 16:29:17 GMT  
 tk_getOpenFile, file extension and the file type drop down list ...

Quote:

> Hi,

> ok - the -filetypes option is already known and used, but the question
> is more ... do I really need to manipulate the list of filetypes to
> decide which file type is the initial file type?
> <snip>
>     # move the matching file type
>     # to beginning of the list
>     #
>     if {$idx < [llength $fileTypes]} {
>         set fileTypes [linsert \
>             [lreplace $fileTypes $idx $idx] \
>             -1 \
>             $fileType \
>         ];
>     }

> Wouldn't it be nice to have a kind of option like "-initialfiletype"
> accepting an index to access the first/initial file type from the
> -filetypes list to be shown while popping up?

You're right, there should be that kind of option in tk_getOpenFile.
Doing it this way is unfriendly and IMHO outright hostile to the user :
"attack of the moving menu item!!". If the user use a program frequent
enough he will be annoyed that each time an open/save dialog pops up
the filetype menu will be in different order. Personally I'd rather:

  set fileTypes [concat [list $fileType] $fileTypes]

instead of [lreplace] even if this leads to duplicated menu items. At
least menu items are where I expect them to be. But then again users
who don't use the program frequent enough will instead be annoyed by
the duplicated menu item and think that the program is "unpolished".



Sat, 04 Oct 2008 17:06:46 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. File Filed Drop List Problems

2. LIST BOX AND DROP DOWN LIST BOX

3. Show Related Files In A Drop Down Combo (C5EEB)

4. Strange (legacy) File Loaded Drop Down behaviour

5. Inserting new value from file drop-down combo box

6. File Drop Down Combo Box

7. Clear File Combo Drop Down Field?

8. File Drop Down does not work Help!

9. Filtering File Loaded Drop Downs

10. File loaded drop downs won't load...

11. File Drop Down in Edit in Place

12. Type-ahead Drop Down Widget

 

 
Powered by phpBB® Forum Software