Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: opening browser from script

by zentara (Archbishop)
on May 16, 2007 at 12:44 UTC ( #615765=note: print w/replies, xml ) Need Help??


in reply to opening browser from script

Here is a more "feature-filled" script that you can derive some ideas from (for linux). More ideas are here-> Perl Windows Open a Web Browser
#!/usr/bin/perl -w use strict; use Cwd; my $pid; my @docs; if(@ARGV){ my $cwd = getcwd; foreach my $arg(@ARGV){ if( $arg =~ /^http:\/\/.*$/ ){ push @docs, $arg } else{ push @docs , "file://$cwd/$arg" } } } else{ #load defaults @docs = qw( file:///home/zentara/1down/1GET http://www.google.com ); } # Look for instance of mozilla. `mozilla -remote "ping()"`; if ( $? ) { # Error, mozilla is not running. if (!defined($pid = fork())) { # Undef branch of fork: die "Cannot fork: $!"; } elsif ($pid == 0) { # Child branch of fork: `mozilla`; # Start new instance.... } else { # Parent branch of fork: do { `mozilla -remote "ping()"`; #sleep 1; select(undef,undef,undef,.5); } while ( $? ); #sleep 1; #give mozilla some time select(undef,undef,undef,.5); load_docs(); $pid=waitpid($pid, 0); # Wait for the pseudo-process # containing the new mozilla instance # to end. } } else { load_docs(); } ############################################################### sub load_docs { my $element; foreach (0..$#docs){ if ( $docs[$_] =~ /~file:\/\// ) { `mozilla -remote "openFile($docs[$_], new-tab)"` ; #`mozilla -remote "openFile($docs[$_], new-window)"` ; } else { `mozilla -remote "openURL($docs[$_], new-tab)"` ; # `mozilla -remote "openURL($docs[$_], new-window)"` ; } } }

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Replies are listed 'Best First'.
Re^2: opening browser from script
by diamantis (Beadle) on May 16, 2007 at 19:53 UTC
    Thank you shmem & zentara. I couldn't find htmlview in my ubuntu 6.10, so I finally used
    if ($output = `firefox`) {print "no browser found\n"}
      Are you sure that works like you think? The way I interpret it, you are setting $output to the backticks returned string with the =. Maybe you need == or eq ? Or I am possibly just oblivious to the magic invoved. :-)

      I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      That
      if ($output = `firefox`) {print "no browser found\n"}

      would mean that you invoke firefox, wait for firefox to exit and stuff its output into $output. Meanwhile (while firefox is running) your application is blocked. Is that the intended behaviour?

      What happens if the invocation fails? Most probably the shell will send an error message to its STDERR, which you don't collect in $output. Bummer, no error message "no browser found"...

      Collecting the output of a program that doesn't exist is no good to test its existence. A better approach would be

      BROWSER: { for my $dir (split /:/, $ENV{PATH}) { for my $browser (qw(firefox mozilla opera konqueror)) { if (-x "$dir/$browser") { system "$dir/$browser" # or "$dir/$browser &" to bac +kground and die "running $browser returned $?\n"; last BROWSER; } } } print "no browser found.\n"; }

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        Actually, I meant
        if (`firefox&`) {print "no browser found\n"}
        but I wrote it wrong in the previous post! I should better pay attention next time I start hearing "Brain overflow...out of memory"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://615765]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2021-06-13 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (54 votes). Check out past polls.

    Notices?