【问题标题】:Automator Move Preview/Thumb image to Folder with similar FileName as the image (Applescript)Automator 将预览/拇指图像移动到与图像具有相似文件名的文件夹(Applescript)
【发布时间】:2018-06-03 20:11:55
【问题描述】:

我得到 2 个错误。

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

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

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

所以,我关注了这个类似的tutorial,到目前为止我得到了这个:

第一)

on run {input, parameters}
    return input
end run

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

on run {input, parameters}
    tell application "Finder"
        set FileName to name of file input
    end tell
end run

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

{FilePath} 出现问题,Automator 不接受新创建的变量:FilePath,在包含字段中,新创建的变量名为 FilePath。

7) 获取变量的值:文件名 8th) 将 Finder 项移动到:FilePath

这是file的工作流程。

【问题讨论】:

    标签: applescript automator


    【解决方案1】:

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

    根据您的工作流程接收输入的设置方式,您可以移除 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"
    

    这使用正则表达式匹配来挑选出符合以下条件的文件名:

    1. 从任何东西开始(但不是什么都没有);那么
    2. 后跟一个空格,正好是三位数字,一个空格,然后是“预览”一词;那么
    3. 绝对跟随任何东西(包括任何东西);那么
    4. 以“.jpg”或“.png”结尾。

    因此,它将以下文件移动到给定文件夹中(如果文件夹不存在则创建该文件夹,并覆盖同名文件):

    • My image_file 001 preview.foo bar.jpgMy image_file 001/
    • 004 2 001 preview.png004 2 001/

    但以下文件不会受到影响:

    • 001 preview.png
    • My image_file 01 preview.jpg

    【讨论】:

    • 非常感谢 CJK,它运行良好,我解释错误:我意识到我没有正确解释它,有时我在“预览”和扩展名“.jpg 或 .png”之间有一个字符串。因此,我尝试将后缀从 preview.jpg 更改为仅预览,但不起作用。我该怎么做通配符。然后从“预览”中取左侧作为文件夹的名称?
    • 我也想将完成的文件夹移动到一个新文件夹,但我尝试了很多东西,但我无法让它工作:如果(存在)然后移动将此处名为 jpg 的文件移动到那里 将名为此的文件夹移动到“/Users/sebba/BLENDER/BLENDS” end if
    • @sebseb 我已经更新了我的答案以实施您要求的更改。如果有任何不妥之处,请告诉我。
    • 再次感谢@CJK,我收到一个错误:操作“运行 Shell 脚本”... mkdir:没有这样的文件或目录 (NSFOD) mv 重命名为:NSFOD mv。 NSFOD。所以我删除了 mkdir 部分,因为我不想创建文件夹来查看我丢失的文件夹。并得到了这个 ERROR mv: NSFOD
    • @sebseb hm,奇怪,我不确定我的想法,因为测试似乎适用于我的测试文件(我的命名非常多样化)。让我们继续这个in a chat room,我会帮助调试问题。这将是一件非常小而明显的事情。
    猜你喜欢
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 2011-08-07
    相关资源
    最近更新 更多