gpt4 book ai didi

macos - 通过AppleScript调用Cocoa函数removeItemAtPath

转载 作者:行者123 更新时间:2023-12-03 17:40:02 24 4
gpt4 key购买 nike

我想使用 Cocoa 函数removeItemAtPath 来删除 AppleScript 部分中指定路径的文件夹,但不知道如何通过 AppleScriptObjC 来完成此操作。

你能帮我举个例子吗?

最佳答案

为什么不直接使用 AppleScript 来删除文件夹?

set theFile to "path:to:folder:"
tell application "System Events"
delete disk item theFile
end tell

编辑:

根据评论,您需要在管理员级别执行此操作,并且您可以提示用户输入凭据。您可以将所有这些放入 shell 脚本中。

如果您只需要密码,请使用当前用户的用户名 this superuser question显示 shell 命令是:

echo <password> | sudo -S <command>

这使得你的代码:

set pass to text returned of (display dialog "Enter your password:" default answer "password" with hidden answer)
do shell script "echo " & quoted form of pass & " | sudo -S rm 'path/to/file'"

如果您需要在单独的管理员用户名下运行它,您可以添加一个对话框来获取用户名并在 shell 脚本中使用 -u 标志:

set username to text returned of (display dialog "Enter your username:" default answer "username")
set pass to text returned of (display dialog "Enter your password:" default answer "password" with hidden answer)
set shellscript to "echo " & quoted form of pass & " | sudo -S -u " & quoted form of username & " rm 'path/to/file'"

关于macos - 通过AppleScript调用Cocoa函数removeItemAtPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19712300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com