开发者

Changing file permissions with Applescript when you do not have read permission

Occasionally employees at my company will get a hold of a file that for some reason, they do not have read or write permission to. An example is downloading a file from a client FTP server.

I'm looking to create something very simple, like an applescript droplet, that will change the permissions for the files dropped on it:

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

The problem is that since the user does not have read permissions, the droplet fails instantly at the on开发者_如何学编程 open filelist line.

Removing everything between the on open block:

on open filelist
end open

still results in the script failing. By failing, I mean it produces a file permission error.

Thanks in advance.


The problem could be that you are trying to work with files your user can not read - this will result in the Droplet not working, because it depends on being able to read! You will need to write a proper application using a file chooser. The problem here is that you can choose multiple files, but only either files or folders. Anyway, I´ve written something similar in the past, so feel free to use this:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜