Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Pipe to Safari from stdin UNIX
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:
<html>
<head></head>
<body>Test</body>
</html>
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.
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[13,261 views]  

Pipe to Safari from stdin | 16 comments | Create New Account
Click here to return to the 'Pipe to Safari from stdin' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Pipe to Safari from stdin
Authored by: sophistry on Dec 01, '05 07:39:40AM

This doesn't work for me in 10.3. here's my error:

[computer:~] shortname% open -a /Applications/Safari.app -f
2005-12-01 10:35:31.604 open[1331] No such file: /Users/longname/-f

huh?

Am I doing something wrong?

---
soph



[ Reply to This | # ]
Pipe to Safari from stdin
Authored by: thochman on Dec 01, '05 11:11:52AM

Try this... I'm on 10.4, so I don't know if this will work any better for you, but I suspect it might:

open -f -a /Applications/Safari.app



[ Reply to This | # ]
Thanks!
Authored by: googoo on Dec 01, '05 07:47:55AM

Thanks for the great hint! This will come in handy when writing code that generates HTML output (i.e., CGIs).

-Mark



[ Reply to This | # ]
Bundle identifier ignored when using stdin
Authored by: rspeed on Dec 01, '05 09:45:47AM
Very cool! Never thought to do that. Here's a quick test I did:
echo "<html><body>Moo</body></html>" | open -a /Applications/Safari.app -f
One weird thing I noticed is that it doesn't seem to work with -b. For example,
echo "<html><body>Moo</body></html>" | open -b com.apple.safari -f
is sent to TextEdit instead.

[ Reply to This | # ]
Pipe to Safari from stdin
Authored by: xSmurf on Dec 01, '05 09:53:27AM

Wow great hit! This is very swell! I see you mention Xalan for parsing XSLT Stylesheets, could you please post more info? If it Xalan-J from Fink? If not where can I get it? Google didn't seem to know...

---
SnitchCTL : http://snitchctl.smurfturf.net/

PM G4 DP 800 / 1.25gb / 120Gb+80Gb / CD/DVD±RW/RAM/DL
- The only APP Smurf



[ Reply to This | # ]
Pipe to Safari from stdin
Authored by: lou on Dec 01, '05 04:09:03PM

I'm using a xalan-c I built from source (for some rather convoluted reasons to deal with a dependency on a backlevel version of xerces), but Darwin Ports has a pre-built version for download.

The Darwin Ports installation should be something like:

sudo port install xalanc

I'd test the installation from Darwin Ports, but I'm afraid to do this and clobber my "from scratch" build.



[ Reply to This | # ]
Not just Safari
Authored by: tbdavis on Dec 01, '05 10:03:53AM

The -f flag can also be used to pipe to other applications as well, including other browsers. Unfortunately open saves the pipe output to a temporary file with a .txt extension, which the Mozilla Suite and Firefox browsers interpret to mean a text/plain content-type.



[ Reply to This | # ]
Not just Safari
Authored by: lou on Dec 01, '05 04:12:59PM

I noticed this also. It i very irritating, it would be nice to be able to control the extension OSX uses for the temporary file. Some applications use the extension to determine how to handle the file.



[ Reply to This | # ]
Pipe to Safari from stdin
Authored by: nxg on Dec 01, '05 12:03:30PM

This sounds extremely nice, but there's something Clever happening which stops it being reliable.

The suggestion by `rspeed' works as expected -- Safari says `Moo' -- but

open -a /Applications/Safari.app -f <file.html

doesn't, when file.html is an XHTML file. Instead, Safari just displays the contents as plain text. And lo and behold, the following

echo "<?xml version='1.0'?><html><body>Moo</body></html>" | open -a /Applications/Safari.app -f

doesn't work. Safari appears to be sniffing the content, and selecting text/plain if it doesn't match <html> promptly. The sniffing isn't completely dim, because

echo '<!-- hello --><html><body>Moo</body></html>' | open -a /Applications/Safari.app -f

is processed as text/html. That's reasonable, but still leaves it unreliable.

What would be ideal is a mime-type option with 'open -f', with or without the -a option. Thus, 'open -m text/html -f' would do the Right Thing, and avoid having to specify the -a option (which is from one point of view a wart) at all.

This is Safari 2.0.2

Norman



[ Reply to This | # ]
Pipe to Safari from stdin
Authored by: lou on Dec 01, '05 04:22:36PM

Ref the posting by tbdavis, I *think* this behavior is because OSX stores the temporary file with a .txt extension. Safari isn't smart enough to look inside the file to determine how to handle it, it depends on the the filename extension to determine how to handle the file.



[ Reply to This | # ]
Pipe to Safari from stdin
Authored by: Afuna on Dec 01, '05 11:33:36PM
open -a /Applications/Safari.app -f <file.html
should just be
open -a /Applications/Safari.app file.html
-- the normal way to use open. I can't get the first one to work over here at all. The -f is used when you're expecting something from the standard input. In this case, there's nothing there, so it's not doing anything.

[ Reply to This | # ]
Pipe to Safari from stdin
Authored by: nxg on Dec 02, '05 07:46:45AM

The first case _is_ reading from stdin: note the angle-bracket before the file.html. That redirects stdin to come from the file. This is similar to

% cat file.html | open -a /Applications/Safari.app -f

but without the extra process. Also it's a standard unix idiom.

As regards the temporary .txt file: this a bit clunky -- I'd have thought a named pipe would be cleaner. But then, it wasn't me implementing it.

I also think this is a bit of a red herring, since it's clear from some of the examples that work that Safari _is_ sniffing the content (otherwise the `Moo' examples wouldn't work), just not very well, or not completely successfully. Allowing a MIME type would be more robust.



[ Reply to This | # ]
Pipe to Safari from stdin
Authored by: Afuna on Dec 02, '05 08:28:15AM

Sorry about that. It was taking a long time to open in Safari, and I wrongly assumed that it was waiting for something, so I used ctrl-c to interrupt the process after a few seconds.

Just tested it now and actually _waited_ until it finished, and I see what you mean now.



[ Reply to This | # ]
Pipe to Safari from stdin
Authored by: xSmurf on Dec 02, '05 01:17:13PM
I've wrote a quick script to address the issue, allows you to preview in any browse and set the extension to what ever you want... It over there:

http://forums.macosxhints.com/showthread.php?p=252816

---
SnitchCTL : http://snitchctl.smurfturf.net/

PM G4 DP 800 / 1.25gb / 120Gb+80Gb / CD/DVD±RW/RAM/DL
- The only APP Smurf

[ Reply to This | # ]

Pipe to Safari from stdin
Authored by: xSmurf on Dec 03, '05 10:27:25AM
If this is a bug, I hope they don't fix it. It is truly an undocumented feature

Not exactly... man open reports:
-f Reads input from standard input and opens the results in the default text editor. End input by sending EOF character (type Control-D). Also useful for piping output to open and having it open in the default text editor.

"ls | open -f" writes the output of the 'ls' command to a file in /tmp and opens the file in the default text editor (as determined by Launch-Services).

---
SnitchCTL : http://snitchctl.smurfturf.net/

PM G4 DP 800 / 1.25gb / 120Gb+80Gb / CD/DVD±RW/RAM/DL
- The only APP Smurf

[ Reply to This | # ]

Pipe to Safari from stdin
Authored by: jacobolus on Feb 15, '06 05:23:22PM
Did you read the hint?
The -f parameter says that the open command will open the default text editor from stdin.


[ Reply to This | # ]