macOS下带管理员权限的AppleScript无法删除用户创建文件的问题
macOS AppleScript 带权限删除非Root文件权限问题
问题现象
在macOS Ventura、Sonoma系统中,使用AppleScript结合Shell操作文件时出现权限异常:
- 对路径为
/Users/martin/Documents/Test/test.zip、权限设为777的文件:- 普通用户在终端执行
rm /Users/martin/Documents/Test/test.zip可正常删除 - 终端中通过
sudo rm /Users/martin/Documents/Test/test.zip也可删除 - 但在带
administrator privileges的AppleScript中执行删除,会触发Operation not permitted错误,且仅root创建的文件能正常操作
- 普通用户在终端执行
测试脚本
初始测试脚本:
-- Define the path to the file you want to delete set filePath to (path to documents folder as text) & "/Users/username/Documents/Documents/Test/test.zip" -- Use `do shell script` with `rm` command with admin privileges to delete the file do shell script "rm " & quoted form of POSIX path of filePath with administrator privileges
更新后的测试脚本:
-- Define the path to the file you want to delete set filePath to "/Users/martin/Documents/Test/test.zip" -- Convert file path to POSIX path set posixFilePath to POSIX path of filePath -- Create the shell command to delete the file set shellCommand to "sudo rm " & quoted form of posixFilePath -- Run the shell command with administrator privileges try do shell script shellCommand with administrator privileges display dialog "File deleted successfully." on error errMsg number errNum display dialog "Failed to delete file: " & errMsg end try
执行后报错:Failed to delete file: rm: /Users/martin/Documents/Test/test.zip: Operation not permitted
已尝试的无效操作
- 将文件/文件夹权限设置为
0777 - 修改文件所有者为root
- 移除文件的x属性及不可变属性
- 通过单独Shell脚本或
osascript命令执行rm,仍出现相同错误 - 已为Script Editor及调用该脚本的应用开启全磁盘访问权限,应用二进制文件权限设为
0777 - 推测问题与macOS SIP限制相关,但禁用SIP并非可行解决方案
需求
寻求无需禁用SIP的设置或解决办法,让带管理员权限的AppleScript能够正常删除/覆盖非root创建的文件
内容的提问来源于stack exchange,提问作者Martin
相关产品推荐
相关产品推荐

