在AppleScript中移动和合并文件夹

时间:2013-08-20 23:14:22

标签: macos vba applescript taxonomy finder

我有一个名为“目录”的文件夹,大约有1000个子文件夹。每个子文件夹(“学生”)包含一个或多个子文件夹,其中包含一个或多个文件。是否可以编写一个脚本:

  • 检测子文件夹是否包含一个或多个子文件夹
  • 如果“学生”只有一个子文件夹,请将其移至父目录中名为“Bad”的新文件夹
  • 如果“学生”有超过一个子文件夹,请将其移至父目录中名为“Good”的新文件夹

显然,因此,我需要“Directory”文件夹中的两个文件夹:一个名为“Bad”,其中包含包含一个文件夹的所有文件夹,另一个名为“Good”,其中包含包含多个文件夹的所有文件夹一个文件夹。换句话说,我希望分类法来自:

/Directory/Billy/Grades
/Directory/Billy/Student_Info
/Directory/Bob/Grades
/Directory/Bob/Student_Info  
/Directory/Joe/Student_Info
/Directory/George/Grades

要:

/Directory/Good/Billy/Grades
/Directory/Good/Billy/Student_Info
/Directory/Good/Bob/Grades
/Directory/Good/Bob/Student_Info
/Directory/Bad/Joe/Student_Info
/Directory/Bad/George/Grades

1 个答案:

答案 0 :(得分:3)

在此之前,它使用了一些您可以在将来使用的核心Finder和AppleScripting创意。

请先备份您的数据,以防万一。

tell application "Finder"
    -- Define the full path to your data
    set student_data_folder to folder POSIX file "/Users/Foo/Desktop/bar/students/data"

    -- Get the student folders, ignoring good & bad incase they have already been created
    set all_student_folders to every folder of student_data_folder whose name is not in {"Good", "Bad"}

    --Create the good & bad folders if they don't exist
    set good_folder to my checkFolderExists("Good", student_data_folder)
    set bad_folder to my checkFolderExists("Bad", student_data_folder)

    -- Now loop through all student folders doing the sort based on how many subfolders they have
    repeat with student_folder in all_student_folders
        if (get the (count of folders in student_folder) > 1) then
            -- Its good
            move student_folder to good_folder
        else
            -- It's bad
            move student_folder to bad_folder
        end if
    end repeat

end tell

on checkFolderExists(fname, host_folder)
    tell application "Finder"
        if not (exists folder fname of host_folder) then
            return make new folder at host_folder with properties {name:fname}
        else
            return folder fname of host_folder
        end if
    end tell
end checkFolderExists

HTH