Automator (2.5) 根据文件名创建文件夹并将文件移动到文件夹的脚本

问题描述 投票:0回答:2

感谢您抽出时间查看。 我正在尝试使用 automator 2.5 创建一个工作流程,该工作流程将读取目录中的视频文件,使用文件名创建一个文件夹,然后将文件移动到新创建的文件夹中。 我尝试了多个在线可用的脚本,但遇到了错误。 然后我创建了这个混合脚本,它不会出错或执行任何操作。 任何帮助将不胜感激(我对编程并不陌生,但对这个版本的自动化程序缺乏经验) Screenshot of script

applescript automator
2个回答
3
投票

在前面的 Finder 窗口中选择文件后,在 Script Editor.app 中运行以下代码应该可以实现您想要实现的目标

tell application "Finder"
    activate
    set selectedFiles to selection as alias list
    set containingFolder to container of (item 1 of selectedFiles) --as alias
    repeat with i from 1 to count of selectedFiles
        set foldersRef to (a reference to folders of containingFolder)
        set foldersRefItems to name of (contents of foldersRef)
        set thisItem to item i of selectedFiles
        set fileName to (text items 1 thru -5) of (name of thisItem as text) as string
        if fileName is not in foldersRefItems then
            move thisItem to (make new folder at containingFolder ¬
                with properties {name:fileName})
        else
            move thisItem to folder fileName of containingFolder
        end if
    end repeat
end tell

0
投票

无论文件扩展名长度如何,该脚本都可以工作。它也只适用于文件,因此可以选择某个目录中的所有内容,并且任何文件夹都不会受到影响。

tell application "Finder"
    activate
    set selectedFiles to selection as alias list
    set containingFolder to container of (item 1 of selectedFiles)
    repeat with i from 1 to count of selectedFiles
        set thisItem to item i of selectedFiles
        if kind of thisItem is not "folder" then
            set foldersRef to (a reference to folders of containingFolder)
            set foldersRefItems to name of (contents of foldersRef)
            set fileName to name of thisItem
            set extensionOffset to (offset of "." in (reverse of characters of fileName as string))
            if extensionOffset is not 0 then
                set baseName to (text 1 thru -(extensionOffset + 1) of fileName)
            else
                set baseName to fileName
            end if
            if baseName is not in foldersRefItems then
                move thisItem to (make new folder at containingFolder with properties {name:baseName})
            else
                move thisItem to folder baseName of containingFolder
            end if
        end if
    end repeat
end tell
© www.soinside.com 2019 - 2024. All rights reserved.