Question

I'm trying to create an easy App with Automator, this app needs to merge all the txt files in a specific folder into one csv file. This is the script:

cat *.txt >merged.csv

This works fine if I open terminal, move to the folder containing the files and execute it.

I'd like if possible to make it work as an app where I drag the folder on the App Icon and the merged file is then created in the folder. Then show a notification of the completed work.

I tried this:

enter image description here

But it gives me an error in the shell (while in terminal works fine). What am I doing wrong?

Was it helpful?

Solution

You need to change your Run Shell Script action to pass input as arguments - then the path of the currently selected Finder item(s) will be passed as $1 (...).

In your shell script, you then need to change (cd "$1") to the passed-in folder in order to perform your merge command in the right place.

I suggest you make your shell script more robust to ensure that (a) only a folder is passed in (as currently selected in Finder) and (b) just one item:

# Make sure that a _single folder_ is currently selected in Finder; exit otherwise.
[[ $# -eq 1 && -d "$1" ]] || 
  { osascript -e 'display alert "Please select a single folder."'; exit 1; }

# Change to the folder.
cd "$1" || exit

# Perform the merge command.
cat *.txt >merged.csv
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top