当您没有读取权限时,使用Applescript更改文件权限

时间:2011-01-21 22:18:42

标签: applescript

我公司的员工偶尔会收到一份文件,由于某种原因,他们没有读取或写入权限。一个例子是从客户端FTP服务器下载文件。

我正在寻找一个非常简单的AppleScript Droplet,它将改变掉落在其上的文件的权限:

on open filelist
 repeat with i in filelist
  do shell script "chmod -R +wr " & quoted form of POSIX path of i with administrator privileges
 end repeat
end open

问题在于,由于用户没有读取权限,因此Droplet会立即在on open filelist行失败。

删除on open块之间的所有内容:

on open filelist
end open

仍会导致脚本失败。失败,我的意思是它会产生文件权限错误。

提前致谢。

1 个答案:

答案 0 :(得分:1)

问题可能是您正在尝试使用您的用户无法读取的文件 - 这将导致Droplet无法正常工作,因为它取决于能够阅读! 您需要使用文件选择器编写适当的应用程序。这里的问题是您可以选择多个文件,但只能选择文件或文件夹。 无论如何,我过去曾写过类似的内容,所以请随意使用:

set myUsername to (short user name of (system info))
set myPassword to ""

set fileOrFolder to button returned of (display dialog "Would you like to change permissions on files of folders?" buttons {"Cancel", "Files", "Folders"} default button "Cancel")

if fileOrFolder is "Files" then
set theSelection to choose file with multiple selections allowed without invisibles

repeat with oneFile in theSelection

do shell script "sudo chmod -R u+rwX,go+rX " & quoted form of POSIX path of oneFile password myPassword with administrator privileges
do shell script "sudo chown -R " & myUsername & ":staff " & quoted form of POSIX path of oneFile password myPassword with administrator privileges

end repeat
else if fileOrFolder is "Folders" then
set theSelection to choose folder with multiple selections allowed without invisibles

repeat with oneFile in theSelection

do shell script "sudo chmod -R u+rwX,go+rX " & quoted form of POSIX path of oneFile password myPassword with administrator privileges
do shell script "sudo chown -R " & myUsername & ":staff " & quoted form of POSIX path of oneFile password myPassword with administrator privileges

end repeat
else
display dialog "Error"
end if