Mil*_*len 3 automator osx-lion macos
我有一个带有两个参数的 shell 脚本:
如何使用 Automator 向 Finder 添加上下文菜单条目,以使用选定的文件路径和文件名作为参数运行 shell 脚本?
选择以在 Automator 中创建一个服务,该服务仅接收选定的文件和文件夹作为Finder 中的输入。添加Run Shell Script操作并将输入作为参数传递。
您收到的参数是所选文件和文件夹的完整 Unix 路径。使用growlnotify, Growl 的一部分用于演示目的:
由于在文件上运行它而产生的咆哮消息:
该命令出现在 Finder 中文件或文件夹的上下文菜单中。如果适用的服务太多,则将它们分组到一个子菜单服务中。
如果你的脚本需要两个完整的文件路径和文件名,你可以不喜欢下面,首先从完整路径解压文件名:
for f in "$@"
do
name="$( basename $f )"
/usr/local/bin/growlnotify "$name" -m "$f"
done
Run Code Online (Sandbox Code Playgroud)
您可以看到文件名用作标题,而路径在 Growl 中用作消息:
如果您需要查询额外的输入,您可以执行一个简短的 AppleScript 来做到这一点。以下是一个完整的 shell 脚本(growlnotify如上),用于查询输入,并将所选文件重命名为新名称。我没有包括错误处理等,例如在新文件名中添加冒号和斜杠可能会破坏脚本。
# repeat for every file in selection
for f in "$@"
do
# capture input of a simple dialog in AppleScript
OUT=$( osascript -e "tell application \"System Events\" to text returned of (display dialog \"New Name of $f:\" default answer \"\")" )
# if the user canceled, skip to the next file
[[ $? -eq 0 ]] || continue
# old file name is the loop variable
OLD="$f"
# new file name is the same directory, plus the user's input
NEW="$( dirname "$OLD" )/$OUT"
# print a message announcing the rename
/usr/local/bin/growlnotify "Renaming…" -m "$OLD to $NEW"
# perform the actual rename
mv "$OLD" "$NEW"
done
Run Code Online (Sandbox Code Playgroud)
通过growlnotify以下方式宣布的示例重命名操作的屏幕截图: