Using apple's Automator to pass filenames to a shell script

John   picture John · Dec 7, 2009 · Viewed 34.8k times · Source

I have an automator script that I'd like to run on a folder. I want the script to take each file in the folder and run my shell command on it. Automator is set to pass input to stdin, but I don't think I'm using stdin correctly below, can you help please?

for f in "$@" 
do
    java -Xmx1000m -jar /Users/myprog/myprog.jar $f 
done

Answer

Dennis Williamson picture Dennis Williamson · Dec 7, 2009

The special variable $@ represents all the command-line arguments provided to the script.

The for loop steps through each one and executes the jar file each time. You could shorten the script to:

for f
do
    java -Xmx1000m -jar /Users/myprog/myprog.jar "$f"
done

since the default behavior of for is to use $@.

You should put quotes around $f at the end of the java command in case there are spaces in the arguments.