【问题标题】:Moving and Consolidating Folders in AppleScript在 AppleScript 中移动和合并文件夹
【发布时间】:2013-08-20 23:14:21
【问题描述】:

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

  • 检测子文件夹是否有一个或多个子文件夹
  • 如果“Student”只有一个子文件夹,请将其移动到父目录中名为“Bad”的新文件夹中
  • 如果“Student”有多个子文件夹,请将其移动到父目录中名为“Good”的新文件夹中

显然,因此,我需要“目录”文件夹中的两个文件夹:一个名为“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

【问题讨论】:

    标签: macos vba applescript taxonomy finder


    【解决方案1】:

    在此之前,它使用了一些您将来可以使用的核心 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

    【讨论】:

    • 为什么使用 POSIX 而不是 AS 别名?
    • @Josh,只是因为问题使用了 POSIX,我想使用提问者已经知道的内容。此外,HFS 路径有点老派,对于更广泛的 xplat 和网络感知用户群来说不太容易理解。
    • 我实际上不知道 Applescript 会因为它自己的功能而接受 POSIX。我一直将所有内容都转换为 POSIX,根据需要进行更改,然后在需要使用时返回 HFS。很高兴知道所有这些麻烦都是徒劳的。 +1 的好答案
    • 另外,我很感谢您使用 POSIX。否则我会很困惑。
    猜你喜欢
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多