简体   繁体   English

Automator:文件夹中文件的Shell脚本

[英]Automator: shell script for files in a folder

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. 我正在尝试使用Automator创建一个简单的应用程序,该应用程序需要将特定文件夹中的所有txt文件合并到一个csv文件中。 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: 我尝试了这个:

在此处输入图片说明

But it gives me an error in the shell (while in terminal works fine). 但这给了我一个shell错误(虽然在终端工作正常)。 What am I doing wrong? 我究竟做错了什么?

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 (...). 您需要更改Run Shell Script操作以将输入as arguments传递-然后,当前选定的Finder项的路径将作为$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. 然后,在shell脚本中,需要将( cd "$1" )更改为传入的文件夹,以便在正确的位置执行合并命令。

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: 我建议你让你的shell脚本更稳健,以确保(a)仅一个文件夹中(如当前在Finder中选定)和(b)只有一个项目通过了:

# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM