Pipe to Safari from stdin

Dec 01, '05 06:26:00AM

Contributed by: lou

I stumbled on this partially-documented feature while experimenting with XSL and XML processing. Quite frequently, I'll want to pipe my XML through several stylesheets and view the result in a browser. This is an iterative process, so dealing with intermediate files is time consuming and confusing.

You can pipe output from stdout through a pipe to Safari with the open command. This example shows xalan transforming a file for a browser, and piping this into Safari:

xalan literal.xml html.xsl | open -a /Applications/Safari.app -f
The manpage for open doesn't say you can use the -a {application} option with the -f parameter. The -f parameter says that the open command will open the default text editor from stdin. But using -a {application} in conjunction with -f opens the application with the first command line parameter as stdin. In this case, the pipe from xalan output becomes stdin for Safari, which happily displays the page.

This trick should work with any standard application that accepts a filename as the first parameter input. For example, if you try the following:
open -a /Applications/Safari.app -f
Safari will wait for you to enter a properly formatted HTML page; here's a simple example:
You finish by typing Control-D, and at that point, Safari will then display the page you enter.

If this is a bug, I hope they don't fix it. It is truly an undocumented feature. I think this sort of simple trick is very cool. It certainly shows how the whole Mac environment has integrated the best of the simple UNIX command line and the best desktop environment around.

Comments (16)

Mac OS X Hints