我想用Automator创建Finder Services插件,该插件可以删除“特殊字符” [\ W ___] +,并用破折号代替。最终可以通过sed和mv的组合实现这一点,然后通过“运行Shell脚本”将其添加到Automator工作流程中吗?
背景: 我在名为ForkLift的应用中编写了这样的动作,请参见图片ForkLift RegEx Action,但也希望在Finder中也能使用类似的功能。
答案 0 :(得分:0)
创建一个新的 Automator 服务。确保它 Receives input as text from Finder
,并选中 Replace selected text with output
(或类似的选项)中的选项。
添加一个运行Shell脚本操作,以接收来自stdin
的输入:
#!/bin/bash
input="$(</dev/stdin)" # assign contents of stdin to variable
shopt -s extglob # activate extended pattern matching
output="${input//+([![:alnum:]_])/-}" # replace runs of non-alphanumeric, non-underscore
# characters with a single hyphen
printf '%s' "$output" # print the result
创建一个新的 Automator 服务。确保 Receives input as file/folder from Finder
。
添加运行Shell脚本操作,该操作接收输入作为参数:
#!/bin/bash
shopt -s extglob # activate extended pattern matching
for f in "$@"; do
filename="$(basename "$f")"
dirpath="$(dirname "$f")"
filename="${filename//+([![:alnum:]_.])/-}"
mv "$f" "$dirpath/$filename"
done
重命名模式中的微小差异是为了防止替换句点("."
),否则将删除所有文件扩展名。