开发者

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

开发者 https://www.devze.com 2022-12-13 14:09 出处:网络
I have an automator script that I\'d like to run on a folder.I want the script to开发者_JAVA技巧 take each file in the folder and run my shell command on it.Automator is set to pass input to stdin, bu

I have an automator script that I'd like to run on a folder. I want the script to开发者_JAVA技巧 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


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.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号