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

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


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 fdo    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 heredone

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 stdoutdone

(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.