使用 AppleScript 删除文件以回收站或永久 [英] Delete files with AppleScript to recycle bin or permanently

查看:42
本文介绍了使用 AppleScript 删除文件以回收站或永久的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 AppleScript,我想在其中插入一个子程序来删除指定的文件.我希望通过一个标志来控制给定的文件是移动到回收站还是永久删除.

I'm writing an AppleScript in which I want to insert a subroutine to delete specified files. With a flag I wish to control whether the given file is moved to the recycle bin or deleted permanently.

实际上我的脚本是这样的:

Actually my script looks like this:

on MyDeleteProc(theFile, allowUndo)
  if allowUndo then
    tell application "Finder" to delete POSIX file theFile
  else
    do shell script "rm " & theFile
  end if
end MyDeleteProc

现在我想知道这种情况到目前为止是否正确,或者是否有另一个 Finder 命令或我忽略的删除命令的参数,以便我能够简化上面的脚本?

Now I want to know if this case is correct so far or is there maybe another Finder command or a parameter for the delete command that I overlooked so I will be able to simplify the script above?

推荐答案

AppleScript 是个喜怒无常的野兽,而魔鬼往往藏在细节中.

AppleScript is a temperamental beast, and the devil is often in the details.

虽然 @adayzdone 的回答 提供了关键指针 - 使用 System Events 应用程序的 delete 命令实现永久删除,计算出确切的语法需要反复试验:

While @adayzdone's answer provides the crucial pointer - use of the System Events application's delete command to achieve permanent deletion, working out the exact syntax takes trial and error:

Caveat:此处理程序适用于文件和文件夹 - 针对 allowUndo 设置为 false文件夹因此永久删除该文件夹的整个子树.

Caveat: This handler works with both files and folders - targeting a folder with allowUndo set to false therefore permanently deletes that folder's entire subtree.

on MyDeleteProc(theFile, allowUndo)
  if allowUndo then
    tell application "Finder" to delete theFile as POSIX file
  else
    tell application "System Events" to delete alias theFile
  end if
end MyDeleteProc

在 OS X 10.9.4 上,我必须执行以下操作才能完成这项工作:

On OS X 10.9.4 I had to do the following to make this work:

  • Finder 上下文:必须将 POSIX file theFile 更改为 theFile as POSIX file(后缀形式) - 不要问我为什么.
  • System Events 上下文:使用cast"alias 和提供的 POSIX 路径是唯一对我有用的命令形式.
  • Finder context: Had to change POSIX file theFile to theFile as POSIX file (postfix form) - don't ask me why.
  • System Events context: Using "cast" alias with the POSIX path provided is the only form of the command that worked for me.

也就是说,对您的原始函数稍加调整也可以使其正常工作(除非您逐个删除许多文件,否则性能可能无关紧要):

That said, a little tweak to your original function would make it work, too (and unless you delete many files one by one, performance probably won't matter):

但是请注意,仅使用 rm 仅适用于 files - 如果您也想将其扩展到文件夹,请使用 rm -rf 代替 - 相同的警告重新永久删除整个子树适用.

Note, however, that just using rm only works with files - if you wanted to extend it to folders, too, use rm -rf instead - the same caveat re permanently deleting entire subtrees applies.

on MyDeleteProc(theFile, allowUndo)
  if allowUndo then
    tell application "Finder" to delete theFile as POSIX file
  else
    do shell script "rm " & quoted form of theFile
  end if
end MyDeleteProc

注意引用形式的使用,它可以安全地将文件路径传递给shell,正确编码空格等字符.

Note the use of quoted form of, which safely passes the file path to the shell, encoding characters such as spaces properly.

这篇关于使用 AppleScript 删除文件以回收站或永久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆