首页 > 解决方案 > 无法更改 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

标签: permissionsapplescriptprivileges

解决方案


您需要 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

推荐阅读