Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

What is a portable (i.e. working on Win9x and WinNT/2k/xp/2k3) way for a perl script to open browser window for Win32? The script is for console and mostly unix-oriented, but I won't mind to insert Win32-specific block.. Preferably it shouldn't open specified URL in the current browser window if it's opened (in order not to clobber user's history). I've tried just using system("start http://mysite.com"); but it brings console window for a second on Win9x (that's what I NEED to avoid too) and replaces current browser window..
Thanks in advance!

Replies are listed 'Best First'.
Re: how to open browser window with given url on Win32?
by bmann (Priest) on Apr 20, 2004 at 19:58 UTC
    You can use ShellExecute to use the default app to open any document (including a URL). When you use it with a URL, it'll respect any and all of the settings you have for your default browser, including reusing windows.

    Here's an example using Win32::API and ShellExecute.

    #!/usr/bin/perl -w use strict; use warnings; use Win32::API; my $ShellExecute = new Win32::API( "shell32", "ShellExecute", [qw(N P P P P N)], 'N' ); my $GetDesktopWindow = new Win32::API( "user32", "GetDesktopWindow", [], 'N' ); my $hWnd = $GetDesktopWindow->Call(); my $url = 'http://perlmonks.org'; $ShellExecute->Call( $hWnd, 'open', $url, '', '', 1 );
Re: how to open browser window with given url on Win32?
by jdporter (Paladin) on Apr 20, 2004 at 21:01 UTC
    If you don't mind using IE, you can do it very simply as follows:
    use Win32::OLE; my $ie = new Win32::OLE 'InternetExplorer.Application'; $ie->Navigate( $url, 0, $window_name );
    If $window_name is not the name of a currently open IE window, it will open a new window with that name.
      I think this will work in most cases. I was looking at this method at home before I saw this responce and I found this article Titled Connect to a Running Instance of Internet Explorer in MSDN. It is in VB and C++ and I have not had time to see if it can be 'Perlized'.
      HTH

      MADuran
      Who needs a spiffy sig
Re: how to open browser window with given url on Win32?
by halley (Prior) on Apr 20, 2004 at 19:00 UTC
    system("start http://www.halley.cc/") if $^O =~ /win/i;
    That's about as good as it gets. Internet Explorer doesn't have any special command line switches to recycle the window that already has that page shown.

    --
    [ e d @ h a l l e y . c c ]

      Mozilla, at least on win98, *will* reuse an open browser window (or even an open browser tab) with the start command. There may be a registry setting that would allow IE to do likewise.