AppleScript如果缺少则添加子文件夹。用于文件夹操作脚本

时间:2019-04-29 22:53:08

标签: macos applescript

附件脚本是通过合并两个脚本构想而创建的。其概念是使用“文件夹操作”功能自动将所有Airdrop文件移动到“下载”文件夹中的子文件夹“ AirDrop”。但是,必须存在“ Airdrop”文件夹,脚本的该部分才能正常运行,有时在清理时,我会将其与所有其他下载内容一起删除。

因此,我添加了一些代码(请注意,我是一名编码爱好者),以便检查“ AirDrop”文件夹并在缺少时创建它。

当我在脚本编辑器中对其进行测试时,此部分将起作用,并让我知道“ Airdrop”文件夹是否存在,是否已创建。

当作为操作脚本附加到下载文件夹时,最近添加的代码似乎被跳过。所有空投文件都会进入下载文件夹或空投子文件夹(如果存在),但是该脚本的确会检查AirDrop文件夹是否“说”是否存在“ AirDrop”文件夹。如果丢失,它将不会创建。

property ORIGINAL_AIRDROP_FOLDER : "macOS SSD:Users:USerName:Downloads"
property NEW_AIRDROP_FOLDER : "macOS SSD:Users:UserName:Downloads:AirDrop"
property QUARANTINE_KEY : "59"

property GET_QUARANTINE_COMMAND_START : "ls -l -@ '"
property GET_QUARANTINE_COMMAND_END : "' | tr '\\n' ' ' | sed 's/.*com\\.apple\\.quarantine\\s*\\(\\d*\\)/ \\1/' | awk '{$1=$1};1'"

------已添加以检查并创建缺少的子文件夹---------

tell application "Finder"
    set inputFolder to (ORIGINAL_AIRDROP_FOLDER) as Unicode text
    set convertedFolderPath to (ORIGINAL_AIRDROP_FOLDER) & ":" & ("AirDrop")
    if (exists (folder convertedFolderPath)) then
        say "Air Drop folder exists"
    else
        say "Air Drop folder does not exist"
        make new folder at inputFolder with properties {name:"AirDrop"}
        say "Created Air Drop folder"
    end if
end tell

在收到添加项后将文件夹项添加到此文件夹中

    repeat with i from 1 to length of added_items
        set current_item to item i of added_items
        set quarantine_type to getQuarantineType(POSIX path of current_item)
        if quarantine_type is equal to QUARANTINE_KEY then
            moveFile(current_item, alias NEW_AIRDROP_FOLDER)
        end if
    end repeat
end adding folder items to

on moveFile(move_file, destination_dir)
    tell application "Finder"
        move move_file to destination_dir with replacing
    end tell
end moveFile

on getQuarantineType(file_path)
    return do shell script GET_QUARANTINE_COMMAND_START & file_path & GET_QUARANTINE_COMMAND_END
end getQuarantineType

我希望它在需要时运行脚本的一部分以创建丢失的文件夹。

2 个答案:

答案 0 :(得分:0)

这是我的决心,并给出了我想要的。再次感谢大家的回应。现在,它显示为正确格式。我没有第一次在该网站上阅读如何输入代码块,因此看来我没有正确格式化它。事实并非如此。

property ORIGINAL_AIRDROP_FOLDER : "macOS SSD:Users:KerryVogt:Downloads"
property NEW_AIRDROP_FOLDER : "macOS SSD:Users:KerryVogt:Downloads:AirDrop"
property QUARANTINE_KEY : "59"

property GET_QUARANTINE_COMMAND_START : "ls -l -@ '"
property GET_QUARANTINE_COMMAND_END : "' | tr '\\n' ' ' | sed 's/.*com\\.apple\\.quarantine\\s*\\(\\d*\\)/ \\1/' | awk '{$1=$1};1'"

on adding folder items to this_folder after receiving added_items
    tell application "Finder"
        set inputFolder to (ORIGINAL_AIRDROP_FOLDER) as Unicode text
        set convertedFolderPath to (ORIGINAL_AIRDROP_FOLDER) & ":" & ("AirDrop")
        if (exists (folder convertedFolderPath)) then
            say "Files have been saved in the folder AirDrop, in the Download folder."
        else
            say "An Air Drop folder will be added to the download folder to complete this action."
            make new folder at inputFolder with properties {name:"AirDrop"}
            say "Air Drop folder has been added"
        end if
    end tell

    repeat with i from 1 to length of added_items
        set current_item to item i of added_items
        set quarantine_type to getQuarantineType(POSIX path of current_item)
        if quarantine_type is equal to QUARANTINE_KEY then
            moveFile(current_item, alias NEW_AIRDROP_FOLDER)
        end if
    end repeat
end adding folder items to

on moveFile(move_file, destination_dir)
    tell application "Finder"
        move move_file to destination_dir with replacing
    end tell
end moveFile

on getQuarantineType(file_path)
    return do shell script GET_QUARANTINE_COMMAND_START & file_path & GET_QUARANTINE_COMMAND_END
end getQuarantineType

答案 1 :(得分:0)

我有莫哈韦沙漠10.14.6 对我来说行

moveFile(current_item, **alias** NEW_AIRDROP_FOLDER)

不起作用。没有错误,只是退出脚本。我刚刚删除了“别名”一词,现在它可以工作了:

moveFile(current_item, NEW_AIRDROP_FOLDER)