简体   繁体   English

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

[英]AppleScript to add sub-folder if missing. Used in Folder Action Scripts

The attached script was created by merging two script ideas. 附件脚本是通过合并两个脚本构想而创建的。 The concept is to move all Airdrop files to a sub-fold "AirDrop" in the Downloads folder automatically using the 'Folder Action' function. 其概念是使用“文件夹操作”功能自动将所有Airdrop文件移动到“下载”文件夹中的子文件夹“ AirDrop”。 However, the 'Airdrop' folder must be present for that part of the script to function and sometimes, while cleaning up, I delete it along with all other downloads. 但是,必须存在“ Airdrop”文件夹,脚本的该部分才能正常运行,有时在清理时,我将其与所有其他下载内容一起删除。

So, I added a little more code (mind you I'm an amateur at coding) to have it check for the "AirDrop" folder and create it if missing. 因此,我添加了一些代码(请注意,我是编码方面的业余爱好者),以检查“ AirDrop”文件夹并在缺少时创建它。

This part works when I test it in Script Editor and lets me know if the 'Airdrop' folder exists and if not it is created. 当我在脚本编辑器中对其进行测试时,该部分将起作用,并让我知道“ Airdrop”文件夹是否存在,如果不存在,则会创建该文件夹。

When attached to the download folder as an action script the recent added code appears to be skipped. 当作为操作脚本附加到下载文件夹时,最近添加的代码似乎被跳过。 All air drop files go into the download folder or airdrop sub-folder if present but the script does appear to check for the AirDrop folder or not "say" whether there is a "AirDrop" folder or not. 所有空投文件都会进入下载文件夹或空投子文件夹(如果存在),但是该脚本的确会检查AirDrop文件夹是否“说”是否存在“ AirDrop”文件夹。 It will not create it if it is missing. 如果丢失,它将不会创建它。

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'"

------ Added to check for and create missing sub-folder--------- ------已添加以检查并创建缺少的子文件夹---------

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

On adding folder items to this_folder after receiving added_items 在接收到add_items之后将文件夹项目添加到this_folder

    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

I would like it to run the part of the script to create the missing folder when required. 我希望它在需要时运行脚本的一部分以创建丢失的文件夹。

This was my resolve and gives what I wanted. 这是我的决心,并提供了我想要的。 Again thanks everyone that responded. 再次感谢大家的回应。 This is now shown as formatted correctly. 现在,它显示为正确格式。 I didn't read the first time how to enter the code block on this website, so it appeared I had not formatted it correctly. 我没有第一次在该网站上阅读如何输入代码块,因此看来我没有正确格式化它。 That was not the case. 事实并非如此。

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

i have mojave 10.14.6 for me line 我有莫哈韦河10.14.6为我线

moveFile(current_item, **alias** NEW_AIRDROP_FOLDER)

doesn't work. 不起作用。 no errors, just quit script. 没有错误,只是退出脚本。 i just removed word "alias" and now it works: 我刚刚删除了“别名”一词,现在它起作用了:

moveFile(current_item, NEW_AIRDROP_FOLDER)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM