Applescript:如何仅在满足特定条件时创建文件夹 - Script Tweaking

Applescript: how to create a folder only if a certain condition is met - Script Tweaking

我需要帮助来调整我制作的以下脚本。我的目标是当项目 1a and/or 4d 被选中时,它们各自的 .ai 文件将被复制到最前面的 finder window 打开的根文件夹中。当项目 2b and/or 3c 被选中时,它将查看最前面的查找器 window 为名为 "TargetFolder" 的文件夹打开。如果 "TargetFolder" 存在,则将 2b.ai and/or 3c.ai 复制到此文件夹。否则,创建文件夹 "TargetFolder",然后将 2b.ai and/or 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

在此先感谢您!玩得开心:-)

以下代码:

  • 确保特定目标文件夹存在,但前提是第 2 个 and/or 第 3 个项目列表项目 选择的项目中用户。

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

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