Pipe filepath to ImageJ

0

I have a little command line utility rjp2tif that extracts radiometric data from a jpeg file into a tiff file. I was hoping to be able to pipe the filepath to ImageJ on the command line and have ImageJ open the tiff file. To this end, rjp2tif writes the filepath of the tiff file to standard output. I tried the following in bash:

$ rjp2tif /path/to/rjpeg | open -a imagej

and

$ rjp2tif /path/to/rjpeg | open -a imagej -f

The first opens ImageJ but doesn't open the file. The second opens ImageJ with a text window with the filepath in it.

This is on macOS Monterey, but I don't think that matters. Anyone tried to do this and been successful? TIA.

bash macos
2021-11-23 23:05:07
1

1

Assuming the rjp2tif command returns a file-path in standard output, and you want to pass this output as a regular CLI argument to another command, you may be interested in the xargs command. But note that in the general case, you may hit some issue if the file-path contains spaces or so:

Read space, tab, newline and end-of-file delimited arguments from standard input and execute the specified utility with them as arguments.
The arguments are typically a long list of filenames (generated by ls or find, for example) that get passed to xargs via a pipe.

So in this case, assuming each file-path takes only one line (which is obviously the case if there's only one line overall), you can use the following NUL-based tip relying on the tr command.

Here is the command you'd obtain:

rjp2tif /path/to/rjpeg | tr '\n' '\0' | xargs -0 open -a imagej

Note: I have a GNU/Linux OS, so can you please confirm it does work under macOS?

FTR, below is a comprehensive shell code allowing one to test two different modes of xargs: generating one command per line-argument (-n1), or a single command with all line-arguments in one go:

$ printf 'one \ntwo\nthree and four' | tr '\n' '\0' | xargs -0 -n1 \
  bash -c 'printf "Run "; for a; do printf "\"$a\" "; done; echo' bash
Run "one " 
Run "two" 
Run "three and four" 

$ printf 'one \ntwo\nthree and four' | tr '\n' '\0' | xargs -0 \
  bash -c 'printf "Run "; for a; do printf "\"$a\" "; done; echo' bash
Run "one " "two" "three and four" 

######################################
# or alternatively (with no for loop):
######################################

$ printf 'one \ntwo\nthree and four' | tr '\n' '\0' | xargs -0 -n1 \
  bash -c 'printf "Run "; printf "\"%s\" " "$@"; echo' bash
Run "one " 
Run "two" 
Run "three and four" 

$ printf 'one \ntwo\nthree and four' | tr '\n' '\0' | xargs -0 \
  bash -c 'printf "Run "; printf "\"%s\" " "$@"; echo' bash
Run "one " "two" "three and four" 
2021-11-24 14:55:40

@ErickMD Yes, the tr command you have above does work on macOS, thanks. I found that the -t option (trace) to xargs is helpful to show what xargs does to the command. In the case here, when path/to/tiff is passed to xargs via the pipe, xargs rearranges the command to open -a imagej path/to/tiff. Very useful.
Jim Merkel

@ErickMD One additional thing, you mentioned above that there is one line to standard output from rjp2tif that is piped to xargs. However there could be many lines to standard output if I use a -v option to rjp2tif. This can work also if I use tee /dev/tty to show all lines on the screen followed by tail -n 1 to just get the last line for piping to xargs. In this case the command line is: rjp2tif -v /path/to/rjpeg | tee /dev/tty | tail -n 1 | xargs open -a imagej. I left out the tr command for simplification.
Jim Merkel

Yes, but xargs can also support multiple lines in input as I mentioned in my answer, and you have 2 choices in this case: either run one open command for each file → xargs -0 -n1 open -a imagej; or run a single open command with all files passed as args → xargs -0 open -a imagej. So you don't need tail -n 1 at all (unless you are interested in only one file, of course)
ErikMD

Actually I guess I misinterpreted your remark: with the -v option, you don't have several file-paths involved, but just some additional "noise" that can be removed from stdout by using tail -n 1, indeed.
ErikMD

Yes, rjp2tif just works with one file at present. The -v is for verbose. So the "noise" is a print out of metadata for the file. I guess I could try and make rjp2tif work for multiple files.
Jim Merkel

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................