views:

20

answers:

3

I'm pretty much a novice to shell scripting. I'm trying to send the output of some piped commands to an open command in bash in OSX.

My ultimate goal is to compile a Flex/Actionscript application from TextWrangler by calling a bash script with a little Applescript and have the result played directly in a Flash Player. The Applescript is pretty much doing it's job. But the bash script doesn't work as I expect. Same results when I ommit the Applescript and simply put it directly in terminal.

This is what the Applescript is sending to terminal:

mxmlc -warnings=false DocumentClass.as | tail -n 1 | sed 's/[[:space:]].*$//' | open -a 'Flash Player'

So basically, I read the last line of the output of mxmlc, which usually looks something like this:

/Users/fireeyedboy/Desktop/DocumentClass.swf (994 bytes)

and I strip everything after the first space it encounters. I know it's hardly bulletproof yet, it's still just a proof of concept. When I get this roughly working I'll refine. It returns the desired result so far:

/Users/fireeyedboy/Desktop/DocumentClass.swf

But as you can see, I then try to pipe this sed result to the Flash Player and that's where it fails. The Flash Player seems to open way too early. I would expect the Flash Player to open only after the script finished the sed command. But it opens way earlier.

So my question is twofold:

  1. Is it even possible to pipe an argument to the open command this way?
  2. Do I need to use some type of delay command to get this working, since the open command doesn't seem to be waiting for the input?
+1  A: 

I'm not familiar with the "open" command as it seems to be a mac thing, but i think what you want to do is:

open -a 'Flash Player' $(mxmlc -warnings=false DocumentClass.as | tail -n 1 | sed 's/[[:space:]].*$//')

In general you can't pipe arguments to a command, you have to specify that you want the output of the previous command to be treated as arguments, either as in my example or with the xargs command. Note that there is a limit on the maximum size of a command line, though.

static_rtti
Genius... It's working! Haha, o man, I'm stoked! Thanks a lot!!
fireeyedboy
+1  A: 

All commands in a pipe are started at the same time. During this step, their input/outputs are chained together.

My guess is that open -a 'Flash Player' doesn't wait for input but simply starts the flash player. I suggest to try to run the player with an argument instead:

name=$(mxmlc -warnings=false DocumentClass.as | tail -n 1 | sed 's/[[:space:]].*$//')
open -a 'Flash Player' "$name"
Aaron Digulla
Thanks for your answer too Aaron!
fireeyedboy
+1  A: 

You're trying to give the name of the swf file as input to stdin of the open command, which it doesn't support.

It expects the file name as an argument (similar to -a).

You can do something like this:

FILENAME=`xmlc -warnings=false DocumentClass.as | tail -n 1 | sed 's/[[:space:]].*$//'`
open -a 'Flash Player' $FILENAME

or on a single line:

open -a 'Flash Player' `xmlc -warnings=false DocumentClass.as | tail -n 1 | sed 's/[[:space:]].*$//'`

If you're using bash (or another modern POSIX shell), you can replace the pretty unreadable backtick character with $( and ):

open -a 'Flash Player' $(xmlc -warnings=false DocumentClass.as | tail -n 1 | sed 's/[[:space:]].*$//')
Joachim Sauer
It's working. I tried static_rtti's answer first. Thanks for the extra info though!
fireeyedboy