指向您的工作流文件的链接不起作用,但没关系 - 感谢您尝试共享它,但是,在这种情况下,我可以不使用它,因为我实际上是从头开始做的:
根据您的工作流程接收输入的设置方式,您可以移除 Get Specified Finder Items 操作并允许工作流程作为服务运行,例如,其输入将是包含要处理的图像的文件夹。正如您所说,包含图像的是您的 Downloads 文件夹,我觉得您不太可能将工作流程设置为 Folder Action。 p>
出于测试目的,您可以保留 Get Specified Finder Items 操作并用您的 Downloads 替换我放在其中的 Example 文件夹> 文件夹。
这是 Run AppleScript 操作中的代码,供您复制/粘贴:
on run {input, parameters}
# input will be the folder containing the images, e.g. the Downloads folder
set [here] to the input
set suffix to "preview.jpg"
set my text item delimiters to {pi, space & suffix}
tell application "Finder"
set jpgs to the name of every file in here whose name ends with the suffix
repeat with jpg in jpgs
set this to the text items of jpg as text
set there to (a reference to the folder named this in here)
if not (there exists) then set there ¬
to make new folder in here ¬
with properties {name:this}
move the file named jpg in here to there
end repeat
end tell
end run
目标文件夹是否存在无关紧要:如果存在,图像将被移动到适当的文件夹中;对于那些不这样做的人,该文件夹是在图像移动之前创建的。
更新以回应评论
①有时我在“预览”和扩展名.jpg或.png之间有一个字符串
② 我也想把完成的文件夹移动到一个新文件夹/Users/sebba/BLENDER/BLENDS
鉴于您现在披露的文件名的可变性,我认为使用 shell 脚本而不是 AppleScript 来处理文件更容易。
删除 Run AppleScript 动作并插入 Run Shell Script 动作,具有以下选项:Shell: bin/bash, 通过输入: as arguments.
删除编辑字段中出现的任何示例代码并输入此代码:
cd "$1"
folder=()
while read -r file
do
folder+=("$(egrep -io '^.+ \d{3}' <<<"$file")")
[[ -d "${folder[@]: -1}" ]] || mkdir "${folder[@]: -1}"
mv -v "$file" "${folder[@]: -1}"
done <<<"$( ls | egrep -i '^.+ \d{3} preview.*\.(jpg|png)' )"
mv -v "${folder[@]}" "/Users/sebba/BLENDER/BLENDS"
这使用正则表达式匹配来挑选出符合以下条件的文件名:
- 从任何东西开始(但不是什么都没有);那么
- 后跟一个空格,正好是三位数字,一个空格,然后是“预览”一词;那么
- 绝对跟随任何东西(包括任何东西);那么
- 以“.jpg”或“.png”结尾。
因此,它将以下文件移动到给定文件夹中(如果文件夹不存在则创建该文件夹,并覆盖同名文件):
-
My image_file 001 preview.foo bar.jpg → My image_file 001/
-
004 2 001 preview.png → 004 2 001/
但以下文件不会受到影响:
001 preview.png
My image_file 01 preview.jpg