Applescript创建监视文件夹并在删除的文件和创建的文件夹中移动文件

时间:2017-03-24 13:53:12

标签: applescript

自从我使用Applescript已经有一两年了,所以我有点生疏了。以下是我目前使用的脚本以及我希望脚本执行的一些其他项目所遇到的问题,但无法实现。

问题:

  1. 想要在数据处理文件夹中创建子文件夹,新名称将是{" Data In"," Data Released"};这通常表示我在尝试执行此操作时没有定义变量
  2. 我希望删除的文件夹中的所有文件和文件夹都能在Artwork文件夹下的AGENCY文件夹中移动;如果我尝试不执行上述#1,我仍然会收到错误消息,说明操作无法完成
  3. 以下是我到目前为止的脚本,非常感谢任何和所有的帮助。

        on adding folder items to this_folder after receiving added_items
    -- added_items is a _list_ of the items just added, even when there's only one. Deal with each item in turn.
    repeat with this_item in added_items
        tell application "Finder"
            -- 'item this_item' gets a Finder reference to the item so that its class can be determined.
            if (class of item this_item is folder) then
                try
                    -- Make new folders in this dropped folder.
                    set client_files_folder to (make new folder in this_item with properties {name:"Artwork"})
                    make new folder in client_files_folder with properties {name:"AGENCY"}
                    make new folder in client_files_folder with properties {name:"EXPORT_TO_XMPIE"}
                    make new folder in client_files_folder with properties {name:"XMPIE"}
                    make new folder in this_item with properties {name:"Data Processing"}
                    make new folder in this_item with properties {name:"Imaging_Laser"}
                    make new folder in this_item with properties {name:"Production Report"}
                    make new folder in this_item with properties {name:"Program Plan"}
    
                    -- Move the following items from the dropped folder to the just-created "AGECNY" folder.
                    move {every folder of this_item} to client_files_folder
    
                on error errMsg
                    display dialog errMsg
                end try
            end if
        end tell
    end repeat
    end adding folder items to
    

    ====== UPDATE ====== 我能够创建子文件夹但现在我无法将文件夹移动到正确的子文件夹中。你能就此提供任何指导吗?请参阅下面的最新脚本:

        on adding folder items to this_folder after receiving added_items
    -- added_items is a _list_ of the items just added, even when there's only one. Deal with each item in turn.
    repeat with this_item in added_items
        tell application "Finder"
            -- 'item this_item' gets a Finder reference to the item so that its class can be determined.
            if (class of item this_item is folder) then
                try
                    -- Make new folders in this dropped folder.
                    set ART_Folder to (make new folder in this_item with properties {name:"Artwork"})
                    set Data_Folder to (make new folder in this_item with properties {name:"Data Processing"})
                    set Agency_Folder to (make new folder in ART_Folder with properties {name:"AGENCY"})
                    make new folder in ART_Folder with properties {name:"EXPORT_TO_XMPIE"}
                    make new folder in ART_Folder with properties {name:"XMPIE"}
                    make new folder in Data_Folder with properties {name:"Data In"}
                    make new folder in Data_Folder with properties {name:"Data Released"}
                    make new folder in this_item with properties {name:"Imaging_Laser"}
                    make new folder in this_item with properties {name:"Production Report"}
                    make new folder in this_item with properties {name:"Program Plan"}
    
                    -- Move the items from the dropped folder to the just-created "AGECNY" folder.
                    move folders in this_item to Agency_Folder
    
                on error errMsg
                    display dialog errMsg
                end try
            end if
        end tell
    end repeat
    end adding folder items to
    

    =======在被删除之前由Vadian提供的答案=========

    我在运行macOS Sierra(10.12.2)的系统上尝试了以下操作,但它不会在我的系统上运行。

    *

      

    无法完成操作的主要问题是因为您   将要移动所有项目,包括当前新创建的项目   文件夹而不是仅在创建之前的现有项目   文件夹。 shell命令mkdir可以在一个中创建文件夹层次结构   线。这比使用Finder更有效。试试这个

    *

        on adding folder items to this_folder after receiving added_items
    repeat with this_item in added_items
        tell application "Finder"
            if (class of this_item is folder) then
                set allItems to every item of this_item
                do shell script "mkdir -p " & quoted form of POSIX path of this_item & "{Artwork/{AGENCY,EXPORT_TO_XMPIE,XMPIE},'Data Processing'/{'Data In','Data Released'},Imaging_Laser,'Production Report','Program Plan'}"
                -- Move the following items from the dropped folder to the just-created "AGECNY" folder.
                move allItems to folder "Artwork:AGENCY:" of this_item
            end if
        end tell
    end repeat
    end adding folder items to
    

1 个答案:

答案 0 :(得分:0)

经过对Stack Exchange及其各种贡献者的大量测试和审核后,我能够将以下代码组合在一起,以实现我在帖子中列出的目标

on adding folder items to this_folder after receiving added_items
-- added_items is a _list_ of the items just added, even when there's only one. Deal with each item in turn.
repeat with this_item in added_items
    tell application "Finder"
        -- 'item this_item' gets a Finder reference to the item so that its class can be determined.
        if (class of item this_item is folder) then
            try
                -- Make new folders in this dropped folder. If you want to be able to have subfolders created for any of your structure you would have to list as shown below
                set ART_Folder to (make new folder in this_item with properties {name:"Artwork"})
                set Data_Folder to (make new folder in this_item with properties {name:"Data Processing"})
                set Agency_Folder to (make new folder in ART_Folder with properties {name:"AGENCY"})
                -- The below shows subfolders and how to input if you want additional folders inside of folders
                make new folder in ART_Folder with properties {name:"EXPORT_TO_XMPIE"}
                make new folder in ART_Folder with properties {name:"XMPIE"}
                make new folder in Data_Folder with properties {name:"Data In"}
                make new folder in Data_Folder with properties {name:"Data Released"}
                make new folder in this_item with properties {name:"Imaging_Laser"}
                make new folder in this_item with properties {name:"Production Report"}
                make new folder in this_item with properties {name:"Program Plan"}

                -- Move the items from the dropped folder to the just-created "AGECNY" folder.
                move (every file of this_item) to Agency_Folder
                move (every folder of this_item whose name is in {"Links", "Lowres PDFs"}) to Agency_Folder
                move (every folder of this_item) to Agency_Folder



            on error errMsg
                display dialog errMsg and (say "error")
            end try
        end if
    end tell
end repeat
end adding folder items to