AppleScript写的droplet修改文件夹权限时无法修改root文件夹权限如何解决
问题根因
你遇到的问题核心是chmod命令对末尾带斜杠的路径有特殊处理逻辑:当传入的路径末尾带/时,chmod -R只会修改该路径指向目录下的所有内容,不会修改目录本身。AppleScript获取文件夹的POSIX路径时默认会携带末尾斜杠,这就是根文件夹权限没有被修改的原因。
另外你的代码中同时使用了sudo和with administrator privileges,二者功能重复,可以移除冗余的sudo。
修改方案
调整代码对POSIX路径的处理,先去除路径末尾的斜杠再传给chmod命令即可,修改后的完整代码如下:
property user_name : "suser_root" property pass_word : "**********" on open theDroppedItems repeat with a from 1 to length of theDroppedItems set theCurrentDroppedItem to item a of theDroppedItems -- 去除POSIX路径末尾的斜杠,确保根目录本身也会被修改权限 set posixPath to POSIX path of theCurrentDroppedItem if posixPath ends with "/" then set posixPath to text 1 thru -2 of posixPath do shell script "chmod -R 777 " & quoted form of posixPath user name user_name password pass_word with administrator privileges end repeat end open
如果想要彻底避免遗漏,也可以显式先修改根文件夹权限,再递归修改内部内容,对应do shell script行替换为以下内容即可:
do shell script "chmod 777 " & quoted form of posixPath & " && chmod -R 777 " & quoted form of posixPath user name user_name password pass_word with administrator privileges
内容的提问来源于stack exchange,提问作者Denis Andreev
相关产品推荐
相关产品推荐

