English 中文(简体)
Using apple s Automator to pass filenames to a shell script
原标题:

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
最佳回答

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.

问题回答

You re correct when you say you re "not using stdin correctly". In fact, from the script snippet you provided, your script assumes you re getting the files as arguments on the command line ... you re not using stdin at all!

In the top right corner of the Run Shell Script action, below the X is a drop down box with 2 options: Pass input: to stdin and Pass intput: as arguments . These options dictate how the selected files are passed to your script action. If the option as arguments is selected, your script should use the following template

for f in "$@"; do
# so stuff here
done

This template is provided by the action itself when as arguments is selected.

On the other hand, if the to stdin option is selected, then you script should use this tamplate:

while read fname; do  # read each line - assumes one file name per line
   # do clever stuff with the filename
   echo $fname # to illustrate we ll simply copy the filename to stdout
done

(In the event you don t know bash scripting, everything after the # on a line is a comment)

Note that the template provided by the script action is the simple, single command

cat

Not very helpful in my opinion.

Note that until you actually enter text into script area, changing between to stdin and as arguments will change the contents of the script box (I assume this is a hint to what your script should look like) but once you enter something, the switching no longer happens.

Based on what I ve seen here, you can set up a handy "service" which allows you to right-click and convert files using the contextual menu in the finder.

To set this up:

  • Launch Automator
  • From the main menu choose "New"
  • Select "Service" (the gear icon)
  • Drag and drop "Utilities -> Run Shell Script" from the left-hand library into the main "editor" area.
  • Replace the existing text "cat" with the following code:

    for f in "$@"; do
        /usr/bin/afconvert -f caff -d LEI16@32000 -c 1 --mix "$f"
        echo "$f converted"
    done
    

    NOTE: The setup above will convert to mono (-c 1 --mix) at 16 bit @ 32000hz (LEI16@32000 = little endian 16 bit). Use /usr/bin/afconvert -h in terminal to see all of the available options.

  • At the top of the editor you should see two drop downs, set them as follows:

    Service receives selected [Files and Folders] in [Finder.app]
    
  • In the Shell Script editor area, ensure:

    "Shell" is set to "/bin/bash"
    

    and

    "pass input" is set to "arguments" 
    
  • Save the thing something like "Convert to CAF"

  • Navigate to a folder with some WAVs in there.
  • Right click on a WAV and you should see "Convert to CAF" at the bottom of the contextual menu.

I m not sure I understand your question since your script does not appear to be using stdin. The $@ special parameter expands to the positional parameters passed as arguments in argv. It works the same way as if you called the script directly from the shell this way:

$ cat >test
for f in "$@"
do
  echo $f
done
$ chmod 755 test
$ ./test a b c
a
b
c

If you wanted to get the arguments from stdin, you could do something like this:

$ cat >test2
for f in $(cat)
do
  echo $f
done
$ chmod 755 test2
$ ./test2 <<EOF
> a b c
> EOF
a
b
c




相关问题
What does it mean "to write a web service"?

I just asked a question about whether it was possible to write a web-page-checking code and run it from free web server, and one supporter answered and said that it was possible only if I run "a web ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Ivy, ant and start scripts

I have a project that uses ant to build and ivy for dependencies. I would like to generate the start scripts for my project, with the classpath, based on the dependencies configured in Ivy, ...

热门标签