Archive of UserLand's first discussion group, started October 5, 1998.

Examples of Scripting on Linux

Author:Mark A. Hershberger
Posted:5/17/1999; 11:56:00 PM
Topic:Scripting on Linux
Msg #:6397 (In response to 6341)
Prev/Next:6396 / 6398

> What is the state of interapplication messaging on Linux (keep in
> mind that this question is coming from a Mac user)?

Linux (Unix) likes to use pipes.

> For example, if I'm browsing the Web with Netscape, is it possible
> to ask Perl to get the URL of Netscape frontmost window?

This one took me a bit longer to figure out than the next one. But that is just because I hadn't seen it done before. As long as your app is friendly, you can get all sorts of information via XWindows. I wrote a short script that grabs the urls from all Navigator windows.

#!/bin/sh

# This sort script print out the urls that each navigator # window is pointing to.

xwininfo -root -tree | awk '/"Navigator" "Netscape"/ {print $1}' | xargs --max-args=1 xprop _MOZILLA_URL -id | awk '{print $3}'

# xwininfo -root -tree | Get a list of all windows # | on the screen. # awk '/"Navigator" "Netscape"/ {print $1}' | Get the navigator windows. # xargs --max-args=1 xprop _MOZILLA_URL -id | Run xprop on each Nav window. # awk '{print $3}' | Print out the quoted URLs.

http://everybody.org/mah/samples/get_urls.txt (Please grab the script from this url as my script is getting garbled here.)

Restricting this to just the "frontmost" window is left as an exercise to the reader because I have no clue how to do that at the moment.

> Another example. If I'm getting a Web page with Perl (LWP), can I
> pipe the result to Netscape, so it will display the result in this
> browser?

I did this with Lynx and shell script:

#!/bin/sh

# Invoke this script with a URL on the command line and it will pop up in # your current Netscape session.

# usage: lynx_get http://example.com/

url=$1 # The URL to fetch. tmpfile=/tmp/lynxget.$$ # temporary file. GET='lynx -source' # GET could refer to any program that # will fetch a page and put the page to # stdout (e.g. GET from LWP dist, wget).

$GET $url > $tmpfile # Fetch the URL. netscape -remote "openURL(file:$tmpfile)" # Send the file to Netscape.

http://everybody.org/mah/samples/lynx_get.txt

It would be easy to convert the code to Perl. Heck, a nice messaging API to netscape wouldn't be that hard to build in pure Perl if you wanted. (I don't think it would be portable, though, since this API seems to be UNIX only.)

> This is trivial stuff to do on a Mac, is it possible on
> Linux?

Pretty trivial under Unix. Once you get it.













This page was archived on 6/13/2001; 4:50:15 PM.

© Copyright 1998-2001 UserLand Software, Inc.