微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法更改 AppleScript 中文件夹的根权限

如何解决无法更改 AppleScript 中文件夹的根权限

我是 AppleScript 的新手,我尝试制作 Droplet,它可以递归地更改对放置的文件夹和其中文件的权限。我写了这段代码,它适用于根文件夹中的所有项目,但它无法更改根文件夹本身的权限。我应该在此代码中更改哪些内容

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

        do shell script "sudo chmod -R 777 " & quoted form of POSIX path of theCurrentDroppedItem user name user_name password pass_word with administrator privileges
    end repeat

end open

解决方法

您需要 HOT FOLDER 概念而不是 DROPPLET:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

property user_name : "suser_root"
property pass_word : "**********"

on adding folder items to this_folder after receiving theDroppedItems
    
    repeat with a from 1 to length of theDroppedItems
        set theCurrentDroppedItem to item a of theDroppedItems
        
        -- make sure dropped item is folder
        if (class of item theCurrentDroppedItem of application "Finder") as text is "folder" then
            
            -- unlock dropped folder and its entire contents
            tell application "Finder" to set entireContents to (entire contents of folder (theCurrentDroppedItem as text)) as alias list
            set end of entireContents to theCurrentDroppedItem
            repeat with anAlias in entireContents
                set theURL to (current application's class "NSURL"'s fileURLWithPath:(POSIX path of anAlias))
                set {theResult,theError} to (theURL's setResourceValue:(false) forKey:(current application's NSURLIsUserImmutableKey) |error|:(reference))
                do shell script "chmod -R 777 " & quoted form of POSIX path of anAlias user name user_name password pass_word
            end repeat
            
        end if
        
    end repeat
    
end adding folder items to

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。