did someone write openURL for tcl/Tk?

122 views
Skip to first unread message

mir...@my-dejanews.com

unread,
May 4, 1999, 3:00:00 AM5/4/99
to
Hi,

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?

Thanks,
miritom
Sample script follows:
#################################################################
#
# openURL - open a URL using Netscape (at the moment)
# shows an error message if netscape isn't in the path.
# If netscape is already running on the display, uses "remote"
# to open another window in the existing netscape process.
#
#################################################################
proc openURL {url} {
set netscape [auto_execok netscape]
if {$netscape == ""} {
errorBox "can't find netscape in path!"
} else {
catch {exec $netscape -remote "openURL($url,new-window)"}
netsCode
if {[string first "not running" $netsCode] != -1} {
# No instance of netscape exists
set newErrCode [catch {exec $netscape -install $url >&
/dev/null &}]
} else {
if {$netsCode != ""} {
errorBox "$netsCode"
}
}
}
}
proc errorBox {message} {
update
tk_messageBox -icon warning -title "Error!" \
-type ok -message $message
update
}

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Carsten Zerbst

unread,
May 4, 1999, 3:00:00 AM5/4/99
to
mir...@my-dejanews.com wrote:
>
> Hi,
>
> 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?
>
Look on dejanews for browser, on possibility is


#general::startBrowser
#
# A procedure to start the browser for help or print
#
proc ::general::startBrowser {url} {
if { $::tcl_platform(platform) == "windows" } {

if {[file exists $::env(COMSPEC)]} {
exec $::env(COMSPEC) /c start $url &
}
} else {
exec $::general::browser -remote openUrl($url) &
}
return
}

buit I forgot the original author.

Bye, Carsten

Christopher Nelson

unread,
May 4, 1999, 3:00:00 AM5/4/99
to
mir...@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!

Don Libes

unread,
May 5, 1999, 3:00:00 AM5/5/99
to
Carsten Zerbst <zer...@tu-harburg.de> writes:
> mir...@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?
> >
> Look on dejanews for browser, on possibility is
>
>
> #general::startBrowser
> #
> # A procedure to start the browser for help or print
> #
> proc ::general::startBrowser {url} {
> if { $::tcl_platform(platform) == "windows" } {
>
> if {[file exists $::env(COMSPEC)]} {
> exec $::env(COMSPEC) /c start $url &
> }
> } else {
> exec $::general::browser -remote openUrl($url) &
> }
> return
> }

Anyone have the translation for MacOS?

Don

Reply all
Reply to author
Forward
0 new messages