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

发布于 2024-10-13 06:57:57 字数 536 浏览 2 评论 0原文

有时,我公司的员工会得到一个文件,但由于某种原因,他们没有读取或写入权限。一个示例是从客户端 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 处立即失败打开 filelist 行。

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

on open filelist
end open

仍然会导致脚本失败。我所说的失败是指它会产生文件权限错误。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2024-10-20 06:57:57

问题可能是您正在尝试使用用户无法读取的文件 - 这将导致 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文