views:

279

answers:

2

When I create a Automator action in XCode using Bash, all files and folder paths are printed to stdin.

How do I get the content of those files? Whatever I try, I only get the filename in the output.

If I just select "Run shell script" I can select if I want everything to stdin or as arguments. Can this be done for an XCode project to?

It's almost easier to use Applescript, and let that run the Bash.

I tried something like

xargs | cat | MyCommand
A: 

read will read lines from the script's stdin; just make sure to set $IFS to something that won't interfere if the pathnames are sent without backslashes escaping any spaces:

OLDIFS="$IFS"
IFS=$'\n'
while read filename ; do
  echo "*** $filename:"
  cat -n "$filename"
done
IFS="$OLDIFS"
Ignacio Vazquez-Abrams
read -r filename
ghostdog74
A: 

What's the pipe between xargs and cat doing there? Try

xargs cat | MyCommand

or, better,

xargs -R -1 -I file cat "file" | MyCommand

to properly handle file names with spaces etc.

If, instead, you want MyComand invoked on each file,

local IFS="\n"
while read filename; do
  MyCommand < $filename
done

may also be useful.

Christopher Creutzig
Never code by night...
Pepijn
Can you explain the options for xargs? I can't find them in man.
Pepijn
Huh? They are listed in my xargs man page.The page lists August 2, 2004 as its date, so it should not look all that different on earlier versions.
Christopher Creutzig