【问题标题】:Delete multiple files - Receive only one authentication prompt删除多个文件 - 仅收到一个身份验证提示
【发布时间】:2015-09-11 22:07:50
【问题描述】:

我正在尝试创建一个脚本来删除同一文件夹中的多个文件,而无需进行两次身份验证。我已经尝试了很多东西,但我对 AppleScript 非常陌生,而且我一直在努力。非常感谢任何帮助。

set colorProfiles to (POSIX file "/Library/ColorSync/Profiles")
set coatedToDelete to "Pantone+ Solid Coated.csv"
set uncoatedToDelete to "Pantone+ Solid Uncoated.csv"

try
    --Notification window with info
    display dialog ("This script will automagically delete your outdated Pantone color books. Please make sure an admin is nearby to authenticate (twice) if prompted.") with icon note

    --Deletes both Pantone+ .csv files
    tell application "Finder"
        delete file (coatedToDelete of folder colorProfiles)
        delete file (uncoatedToDelete of folder colorProfiles)

    end tell
    --Successful deletion notification
    display dialog ("The outdated color book deletion was successful!") buttons {"Great!"} with icon note


on error
    --Error message notification
    display dialog ("This script was unable to delete your legacy Pantone color books.") buttons {"OK"} with icon caution
end try

【问题讨论】:

    标签: file applescript directory delete-file


    【解决方案1】:

    使用shell命令rm,一次认证可以一行删除多个文件。与 Finder 不同的是,文件会立即删除,而不是移动到垃圾文件夹。

    set colorProfiles to "/Library/ColorSync/Profiles/"
    set coatedToDelete to colorProfiles & "Pantone+ Solid Coated.csv"
    set uncoatedToDelete to colorProfiles & "Pantone+ Solid Uncoated.csv"
    
    try
      --Notification window with info
      display dialog ("This script will automagically delete your outdated Pantone color books. Please make sure an admin is nearby to authenticate (twice) if prompted.") with icon note
    
      --Deletes both Pantone+ .csv files
      do shell script "/bin/rm " & quoted form of coatedToDelete & space & quoted form of uncoatedToDelete with administrator privileges
    
      --Successful deletion notification
      display dialog ("The outdated color book deletion was successful!") buttons {"Great!"} with icon note
    
    
    on error
      --Error message notification
      display dialog ("This script was unable to delete your legacy Pantone color books.") buttons {"OK"} with icon caution
    end try
    

    【讨论】:

    • 感谢您的提醒。所以绝对没有办法在 shell 脚本之外和 Applescript 中做到这一点?另外 - 如果脚本/支持文件已分发并且您事先不知道 CD 的位置,您是否知道如何从 applescript 调用 shell 脚本。
    • 确实没办法。什么是 CD?
    • 我最终使用了您建议的 bash 脚本。抱歉,CD 是指更改目录。在 bash 和 applescript 中,无论脚本位于何处,导航到脚本的父文件夹对我来说都是一个痛点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2021-06-08
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多