Applescript:仅在满足特定条件时如何创建文件夹 - 脚本调整

时间:2015-10-27 18:26:36

标签: applescript

我需要帮助调整我制作的以下脚本。我的目标是当选择项目1a和/或4d时,它们各自的.ai文件将被复制到打开的最前面查找器窗口的根文件夹。当选择项目2b和/或3c时,它将查看为名为“TargetFolder”的文件夹打开的最前面的查找器窗口。如果存在“TargetFolder”,则将2b.ai和/或3c.ai复制到此文件夹。 ELSE,创建文件夹“TargetFolder”,然后将2b.ai和/或3c.ai复制到文件夹“TargetFolder”。

property TargetFolder : "TargetFolder"

tell application "Finder"
    tell application "Finder" to set the this_folder to (folder of the front window) as alias

    try
        set mylist to (choose from list {"1a", "2b", "3c", "4d"} with multiple selections allowed)
    end try

    --This should be applied ONLY if item 2b and/or 3c is chosen
    if not (exists folder TargetFolder of this_folder) then
        make new folder at this_folder with properties {name:TargetFolder}
    end if

    try
        if mylist contains "1a" then
            duplicate file "1a.ai" of folder "Source Folder" of folder "Desktop" of folder "user" of folder "Users" of startup disk to this_folder
        end if
        if mylist contains "2b" then
            duplicate file "2b.ai" of folder "Source Folder" of folder "Desktop" of folder "user" of folder "Users" of startup disk to folder "TargetFolder" of folder this_folder
        end if
        if mylist contains "3c" then
            duplicate file "3c.ai" of folder "Source Folder" of folder "Desktop" of folder "user" of folder "Users" of startup disk to folder "TargetFolder" of folder this_folder
        end if
if mylist contains "4d" then
            duplicate file "4d.ai" of folder "Source Folder" of folder "Desktop" of folder "user" of folder "Users" of startup disk to this_folder
        end if
    end try
end tell

提前非常感谢!有一个伟大的: - )

1 个答案:

答案 0 :(得分:0)

以下代码:

  • 确保存在特定的目标文件夹,但仅当第二和/或第三项列表项用户选择的项目中时才存在。

    • 也就是说,如果目标文件夹尚不存在,则会创建它。
  • 然后在循环中处理所有选定的项目。

property TargetFolderName : "TargetFolder"

tell application "Finder"

    # Define input list.
    set inputList to {"1", "2", "3", "4"}

    # Prompt user for a potentially multiple-item selection from the list.
    set chosenList to choose from list inputList with multiple selections allowed
    # Exit, if nothing was chosen.
    if chosenList is false then return

    # Get the active folder from Finder's front window.
    set this_folder to (folder of the front window) as alias

    # Test if the 2nd item from the input list is among the selected items,
    # and only then create / ensure the existence of the target folder.
    set targetFolder to missing value
    if chosenList contains item 2 of inputList ¬
        or ¬
        chosenList contains item 3 of inputList then

        # See if the target folder already exists there.
        try
            set targetFolder to folder TargetFolderName of this_folder
        end try
        # If it doesn't exist yet, create it now.
        # Note: This could fail.
        if targetFolder is missing value then
            set targetFolder to make new folder at this_folder with properties {name:TargetFolderName}
        end if

    end if

    # Process all chosen items in a loop.
    # Note that the loop variable receives a *reference* to each list item.
    repeat with chosenItemRef in chosenList

        # Resolve the list-item *reference* to get the actual *value*.
        set chosenItem to contents of chosenItemRef

        # Get the source file matching the value at hand:
        set sourceFile to alias ("Macintosh HD:Users:user:Desktop:Source Files:" & chosenItem & ".ai")

        # Now test the value and take action accordingly.
        if chosenItem is item 2 of inputList ¬
            or chosenItem is item 3 of inputList then # 2nd or 3rd item: copy to targetFolder
            # Note: `with replacing` blindly replaces an existing target file.
            duplicate sourceFile to targetFolder with replacing
        else # all others: copy to this_folder
            duplicate sourceFile to this_folder with replacing
        end if

    end repeat


end tell