Subject: Re: did someone write openURL for tcl/Tk? - DN [1]


Christopher Nelson <chris@pinebush.com> - 04 May 1999 - comp.lang.tcl

 miritom@my-dejanews.com wrote:
 > I'm looking for a script that can open a URL using the user's default
 > browser. I have written a script that works in Unix'es, by searching in the
 > path for netscape. I would like to make the script usable under other
 > platforms, and try other browsers if Netscape doesn't work. Unfortunately, I
 > don't have access to any other browsers or platforms. Can anyone help me make
 > this script better?

 On what platform?  On Windoze, the following works.  Note that you can't use

     exec $env(COMSPEC) /c start $link

 if link includes an anchor like somefile.html#anchor because start can't find an
 association for the extension .html#anchor.  This proc works around that.
 (FWIW, these are examples taken from my Tcl/Tk Programmer's Reference due out
 from McGraw-Hill this summer.)

                                                Chris
 ----------------------------------------------------------------
     package require registry
     proc showHtml { htmlFile } {
         # Look for the application under HKEY_CLASSES_ROOT
         set root HKEY_CLASSES_ROOT
         # Get the application key for HTML files
         set appKey [registry get $root\\.html ""]
         # Get the command for opening HTML files
         set appCmd [registry get \
                 $root\\$appKey\\shell\\open\\command ""]
         # Substitute the HTML file name into the command for %1
         set appCmd [perSub $appCmd %1 $htmlFile]
         # Double up the backslashes for eval (below)
         regsub -all {\\} $appCmd  {\\\\} appCmd
         # Invoke the command
         eval exec $appCmd &
     }

     # Substitute for % "variables" (like %W, etc. in event bindings)
     # Based on [percent_subst] from Effective Tcl/Tk Programming by Mark
     # Harrison and Michael McLennan
     # Example:
     #   tclsh>perSub "%1 %2 %3" %1 one %3 three %2 two
     #   one two three
     package require opt
     ::tcl::OptProc perSub {
         {string  -string "" "String to work on"}
         {pattern -string "" "What to substitute for, e.g., %v"}
         {subst   -string "" "What to put in for $pattern"}
         {args    -list   {} "More pattern/subst pairs"}
     } {
         # Add the required instances to the optional list
         set args [linsert $args 0 $pattern $subst]
         # Process the list
         foreach {pattern subst} $args {
             # Validate pattern
             if { ! [string match %* $pattern]} {
                 error "Bad pattern <<$pattern>>: Should be %something"
             }
             # Escape dangerous characters ('\' and '&') in
             # substitution string
             regsub -all {\\|&} $subst {\\\0} subst
             # Do substitutions on string
             regsub -all $pattern $string $subst string
         }
         return $string
     }

 --
 Rens-se-LEER is a county.  RENS-se-ler is a city.  R-P-I is a school!

Last modified
1999-09-27

(195.108.246.50)

Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated