Applescript通过顺序命名移至新文件夹

时间:2018-12-03 22:28:07

标签: applescript subdirectory

我写了下面的基本脚本,以检查文件夹是否存在以及是否确实将文件夹的内容添加到子文件夹中。

正如您在此脚本中看到的那样,生成了名为V1的子文件夹,我想知道是否有一种方法来顺序命名并生成新文件夹?

例如,如果文件夹V1存在,则创建V2并移动内容

当前脚本

--Set image source folder
set SourceFolder to (choose folder with prompt "Choose Base Images:")
set Sourcefoldertext to SourceFolder as text
set FoldertoReplace to Sourcefoldertext & "Images:"

tell application "Finder
    if (exists folder named "Images" in folder SourceFolder) then make new folder in folder FoldertoReplace with properties {name:"V1"}
    if (exists folder named "Images" in folder SourceFolder) then set fileList to every file of folder FoldertoReplace  
    if not (exists folder named "Images" in folder SourceFolder) then make new folder in folder SourceFolder with properties {name:"Images"}    
    if (exists folder named "V1" in folder FoldertoReplace) then set VersionFolder to the folder named "V1" in the folder FoldertoReplace   
    if (exists folder named "V1" in folder FoldertoReplace) then return move the fileList to the VersionFolder'
end tell

谢谢 P

2 个答案:

答案 0 :(得分:1)

添加此处理程序,它将创建具有顺序名称的新文件夹,并将Finder引用返回到新创建的文件夹

on createSequentialVersionFolder(targetFolderPath)
    set counter to 1
    repeat
        set versionFolder to "V" & counter
        tell application "Finder"
            if not (exists folder versionFolder in folder targetFolderPath) then
                make new folder at folder targetFolderPath with properties {name:versionFolder}
                return result
            end if
        end tell
        set counter to counter + 1
    end repeat
end createSequentialVersionFolder

您的其他代码包含很多多余的东西。可以减少到

set Sourcefolder to (choose folder with prompt "Choose Base Images:") as text
set FoldertoReplace to Sourcefolder & "Images:"

tell application "Finder"
    if exists folder FoldertoReplace then 
        set versionFolder to my createSequentialVersionFolder(FoldertoReplace)
        set fileList to every file of folder FoldertoReplace
        move the fileList to the versionFolder
    else 
        make new folder in folder SourceFolder with properties {name:"Images"}
    end if
end tell

答案 1 :(得分:0)

现有的解决方案很棒,但我想我会为某些变化添加一个替代方案。

此处理程序使用系统事件和递归函数调用来检查现有文件夹版本。参数targetFolderPath可以是alias对象,posix路径或HFS路径。

to createSequentialVersionFolder at targetFolderPath given name:dir : "V"
    local targetFolderPath, dir

    script
        property fp : targetFolderPath
        property dirname : dir

        to checkVersion(i)
            tell application "System Events"
                if exists the folder named (dirname & i) ¬
                    in the folder named fp then ¬
                    return my checkVersion(i + 1)

                make new folder at folder named fp ¬
                    with properties ¬
                    {name:dirname & i}
            end tell
        end checkVersion
    end script

    result's checkVersion(1)
end createSequentialVersionFolder

要使用:

createSequentialVersionFolder at "~/Desktop"

或:

createSequentialVersionFolder at (path to desktop folder)