【问题标题】:AppleScript to add sub-folder if missing. Used in Folder Action Scripts如果缺少 AppleScript,则添加子文件夹。用于文件夹动作脚本
【发布时间】:2019-10-25 09:21:41
【问题描述】:

附加的脚本是通过合并两个脚本创意创建的。这个概念是使用“文件夹操作”功能自动将所有 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

在收到 added_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

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

【问题讨论】:

  • 至于 comment ------ Added to check for and create missing sub-folder--------- 之后的 tell application "Finder" block,我会删除它,简单地使用:do shell script "mkdir -p \"$HOME/Downloads/AirDrop\"" 这样如果文件夹不存在,则创建它,如果存在,则没有任何变化!不需要所有的繁文缛节和胡言乱语。 :)
  • 您没有运行添加的代码。 adding folder items 处理程序是为文件夹操作调用的,因此您需要添加语句或从那里为您添加的检查调用处理程序。
  • 如果您喜欢的话,来自 User3439894 的答案会清理代码,但是它仍然无法在“文件夹操作”脚本中正确运行,并且在运行操作时不会被调用。就像我当前的脚本一样,它在脚本编辑器中工作,但不是在实际使用中。感谢您提供额外的编程想法。
  • red_menace 您介意详细说明吗?如何运行添加的代码。就像我说的我是编码的业余爱好者。一直在寻找学习。什么样的说法?它去哪儿了?如何使用呼叫处理程序?
  • 再次感谢您的意见。我玩得更多,阅读更多,并将添加的代码放在脚本中的正确位置,以便调用它。 - 问题解决了!

标签: macos applescript


【解决方案1】:

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

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

【讨论】:

  • 如果 getQuarantineType(file_path) handler 的预期 returncom.apple.quarantine 之后显示的 整数值 GET_QUARANTINE_COMMAND_START & file_path输出,那么用于GET_QUARANTINE_COMMAND_END 的复杂复​​合命令 可以简化为:property GET_QUARANTINE_COMMAND_END : "' | awk '/com.apple.quarantine/{print $2}'"。无需使用多个 管道 |trsed
  • 这是一个小问题,除了只是一个挑剔之外,将来可能会在您最不期望的时候派上用场:当写出 HFS 路径(其中包含冒号)时,如果path 指向一个文件夹,严格来说,您应该用最后一个冒号终止字符串,例如property ORIGINAL_AIRDROP_FOLDER : "macOS SSD:Users:KerryVogt:Downloads:"。没有结束冒号的路径通常表示文件而不是文件夹,但这里主要是语义。但是,如果 AppleScript 为您提供 HFS 路径,并且您想要一种快速简便的方法来区分文件和文件夹,请查找冒号。
  • 感谢您的帮助。我会记住你的信息。我现在写的就是我想要的。
【解决方案2】:

我有 mojave 10.14.6 对我来说行

moveFile(current_item, **alias** NEW_AIRDROP_FOLDER)

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

moveFile(current_item, NEW_AIRDROP_FOLDER)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多