首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Applescript:如何只在满足特定条件的情况下创建文件夹-脚本调整

Applescript:如何只在满足特定条件的情况下创建文件夹-脚本调整
EN

Stack Overflow用户
提问于 2015-10-27 18:26:35
回答 1查看 278关注 0票数 0

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

代码语言:javascript
复制
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

提前谢谢你!有一个伟大的:-)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-27 18:40:20

以下代码:

  • 确保特定目标文件夹的存在,但前提是第二项和/或第三项列表项目是用户选择的项目之一。
代码语言:javascript
复制
- That is, if the target folder doesn't already exist, it is created.

  • 随后在循环中处理所有选定的项。
代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33375785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档