Automator将预览/缩略图图像移动到文件名与图像类似的文件夹(Applescript)

Automator将预览/缩略图图像移动到文件名与图像类似的文件夹(Applescript),applescript,automator,Applescript,Automator,我有两个错误 目标:在我的下载文件夹中,我有: 文件夹:fileName 001 图像:fileName 001 preview.jpg 文件夹:fileName 002 图片:fileName 002 preview.jpg 文件夹:fileName 003 图片:fileName 003 preview.jpg 我希望Automator将所有预览移动到各自的文件夹中 所以,我遵循了这个类似的方法,到目前为止我已经做到了: (一) 2)过滤器查找器项目:种类为文件夹 3)设置变量值:FileP

我有两个错误

目标:在我的下载文件夹中,我有:

文件夹:fileName 001 图像:fileName 001 preview.jpg 文件夹:fileName 002 图片:fileName 002 preview.jpg 文件夹:fileName 003 图片:fileName 003 preview.jpg

我希望Automator将所有预览移动到各自的文件夹中

所以,我遵循了这个类似的方法,到目前为止我已经做到了:

(一)

2)过滤器查找器项目:种类为文件夹 3)设置变量值:FilePath(图像必须经过的路径) 第4)在这一步中,我得到一个错误:检查操作属性(我是新的,所以不知道)

5)设置变量值:FilePath(图像必须经过的路径) 6)过滤器查找器项:种类是图像,名称包含{FilePath}(与文件夹名路径相同)

问题发生在{FilePath}上,自动机不接受新创建的变量FilePath,在contains字段中,新创建的变量FilePath

7)获取变量的值:FileName 8)将查找器项目移动到:FilePath


这是工作流程。

指向您的工作流程文件的链接不起作用,但也可以,谢谢您尝试共享它,但是,在这种情况下,我可以不使用它,因为我实际上要从头开始:

根据将如何设置工作流以接收输入,您可以删除“获取指定的查找项”操作,并允许工作流作为服务运行,例如,其输入将是包含要处理的图像的文件夹。正如您所说,是您的下载文件夹包含图像,我觉得您不太可能将工作流设置为文件夹操作

出于测试目的,您可以保留获取指定的查找项操作,并将我放在其中的示例文件夹替换为您的下载文件夹

以下是要复制/粘贴的运行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来处理文件更容易

删除运行AppleScript操作并插入运行Shell脚本操作,使用以下选项:Shell:
bin/bash
,传递输入:
作为参数

删除编辑字段中出现的任何示例代码,然后输入此代码:

    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"
cd“$1”
文件夹=()
读取-r文件时
做

文件夹+=(“$(egrep-io'^.+\d{3}”非常感谢CJK,它工作得很好,我错误地解释:我意识到我没有正确解释它,有时我在“preview”和extension.jpg或.png之间有一个字符串“。因此,我尝试将后缀从preview.jpg更改为仅预览,但不起作用。如何使用通配符。然后仅将“preview”的左侧作为文件夹的名称?我也想将已完成的文件夹移动到新文件夹,但我尝试了许多方法,但我无法使其工作:if(存在)然后将名为jpg的文件移到此处,将名为this的文件夹移到“/Users/sebba/BLENDER/BLENDS”末尾if@sebseb我已更新我的答案以实现您请求的更改。如果有任何不合理的地方,请告诉我。再次感谢@CJK,我收到一个错误:操作“运行Shell脚本”…mkdir:没有这样的文件或目录(NSFOD)mv rename to:NSFOD mv.NSFOD。因此我删除了mkdir部分,因为我不想创建一个文件夹来查看是否丢失了该文件夹。并收到以下错误mv:NSFOD@sebseb嗯,奇怪,我不太清楚,因为测试似乎对我的测试文件起作用(我给它起了不同的名字).让我们继续这个,我将帮助调试这个问题。它将是非常小和明显的。
    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
    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"