首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果丢失,AppleScript将添加子文件夹.在文件夹操作脚本中使用

如果丢失,AppleScript将添加子文件夹.在文件夹操作脚本中使用
EN

Stack Overflow用户
提问于 2019-04-29 22:53:08
回答 2查看 124关注 0票数 0

附加的脚本是通过合并两个脚本想法创建的。其概念是使用‘文件夹操作’功能将所有Airdrop文件移动到下载文件夹中的子折叠"AirDrop“中。然而,“Airdrop”文件夹必须存在,以便脚本的这一部分发挥作用,有时,在清理时,我删除它与所有其他下载。

因此,我增加了一些代码(请注意,我在编码方面是业余的),让它检查"AirDrop“文件夹,如果丢失了就创建它。

当我在脚本编辑器中测试它时,这个部分可以工作,并让我知道'Airdrop‘文件夹是否存在,如果不存在,它将被创建。

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

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

添加以检查和创建缺失子文件夹

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

关于在收到this_folder后向added_items添加文件夹项

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

我希望它能在需要时运行脚本的一部分来创建缺少的文件夹。

EN

回答 2

Stack Overflow用户

发布于 2019-05-02 02:36:59

这是我的决心,给了我想要的。再次感谢所有回应的人。这现在显示为格式正确。我第一次没有读到如何在这个网站上输入代码块,所以我似乎没有正确地格式化它。但事实并非如此。

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

Stack Overflow用户

发布于 2019-10-24 20:08:24

我有莫哈韦10.14.6给我的电话

moveFile(current_item, **alias** NEW_AIRDROP_FOLDER)

不起作用。没有错误,就退出脚本。我刚删除了“别名”这个词,现在起作用了:

moveFile(current_item, NEW_AIRDROP_FOLDER)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55911779

复制
相关文章

相似问题

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