我想用Automator创建Finder Services插件,该插件会删除“特殊字符” [\ W ___] +,并用破折号代替。最终可以通过sed和mv的组合实现此目标,然后通过“运行Shell脚本”将其添加到Automator工作流程中吗?
背景:我在名为ForkLift的应用中编写了这样的动作,请参见图像ForkLift RegEx Action,但也希望在Finder中也具有类似的功能。
创建新的Automator服务。确保它[Receives input as text from Finder
,并检查Replace selected text with output
的选项(或类似的内容)。
添加Run Shell Script操作以从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
。
添加Run 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
重命名模式中的微小差异是为了防止句点("."
)被替换,否则将删除所有文件扩展名。