0

I have a situation in which I'm getting HTML content via a pipe. I'd like to display it in a browser window. But the obvious step,

somethingThatProducesHTMLonStdout | open -f -a "Safari"

does not work: Safari displays the content, but as plain text; it does not render the HTML. I know for a fact that my HTML content is valid HTML content. In fact, it is easy to demonstrate this problem with a trivial test case:

echo "<html><body>foo</body></html>" | open -f -a "Safari"

does the same thing – displays plain text, not rendered content. I'm at a loss as to why Safari insists on doing this. What is the secret to making it render the HTML?

Note: I know I can make this work by saving the content to a temporary file and then opening the temporary file. I want to make it work without using a temporary file, if it is possible.

  • 2
    I'm surprised to learn that Safari does read anything from stdin at all, most Cocoa applications don't. You could try with process substitution (see man bash for details) but I don't know whether it's worth the hassle. – nohillside Aug 4 '17 at 20:25
  • 1
    Ah, did some further digging. The problem is that open -f creates a temporary .txt file from standard input, and Safari always seems to open .txt files as text (and not as HTML), as can be easily tested by running open -a Safari any-html-file.txt. – nohillside Aug 4 '17 at 21:19
  • I don't think this is possible without saving it to a temporary file since Safari can only open URLs to files (locally or on a server). You could however simply make a small bash script which you can pipe the stdout to that creates this temp file and opens it with Safari – M.J.K Aug 4 '17 at 21:25
  • Well, foo! That's annoying. Thank you for discovering this. If you post this as an answer (i.e., "it can't be done"), I'll be happy to accept it. – mhucka Aug 4 '17 at 21:25
  • @M.J.K Thanks, and yes, I know I can create a temporary file, but the point of my question is precisely to try to avoid temporary files.... – mhucka Aug 4 '17 at 21:26
2

You can use something like

echo '<html><body><h2>Hi, there!</h2>foo</body></html>' > /tmp/x.html && open -a "Safari" /tmp/x.html

I entered some HTML that contains an exclamation mark to show that you need to use single quotes for the shell, otherwise it will interpret this as a history recall. Yes, longer and you need a file name, but you can use your somethingThatProducesHTMLonStdout tool from your question.

  • 1
    The very last thing said in the OP was "Note: I know I can make this work by saving the content to a temporary file and then opening the temporary file. I want to make it work without a temporary file, if it is possible.", so your answer only echoes what mhucka already knows! – user3439894 Aug 12 '17 at 3:31
  • Exactly what @user3439894 wrote. I'm going to edit my question to put that part in bold face :-) – mhucka Aug 12 '17 at 3:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .