问题描述
我是 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