在 Applescript 中执行 shell 时,有没有办法忽略不允许的访问?

Is there a way to ignore not permitted access when executing shell in Applescript?

所以我想在我的 AppleScript 中使用 shell 中的 find 命令。我想在我的整个磁盘中搜索某种文件类型,所以我做了这样的事情:

set theItems to do shell script "find / -name '*.mp3'" with administrator privileges
set theItems to paragraphs of theItems
repeat with theFile in theItems
    --some code that process every file here
end repeat

问题是,即使我添加了额外的 with administrator privileges,一些文件仍然归系统所有,我无法访问它们。在我的循环中,我有一些代码可以处理这些行。

有没有办法忽略这些?

由于 find 命令在访问某些 files/directories 时出错,它将报告错误并以错误状态退出;当 do shell script 发现该错误时,它会抛出自己的 AppleScript 级错误。

您可以通过在末尾添加 || true 使脚本始终成功。在 shell 语言中,这意味着“如果 find 命令失败(以错误状态退出),运行 true,它总是成功,所以整个脚本成功。

我认为没有必要,但通过添加 2>/dev/null 来抑制 find 打印的错误消息可能也很好(尽管 do shell script 似乎忽略了这些消息只要整体命令成功)。

set theItems to do shell script "find / -name '*.mp3' 2>/dev/null || true" with administrator privileges

我确实有一个警告,但是:这会抑制来自 find 所有 错误,而不仅仅是权限错误,所以如果出现其他问题,您将不会通知。