具有可选管理员权限的 Applescript
我需要运行一个脚本作为应用程序首次运行步骤的一部分,以从以前的版本中删除一些旧组件。通常,这些组件安装在 $HOME/Library 或 /Library 中,脚本可以简单地将它们移至垃圾箱。
问题是,如果用户不是管理员,并且组件位于全局 /Library 中,则脚本需要以管理员身份进行身份验证,否则将会失败。我当前的脚本使用 AppleScript,但令我惊讶的是,我找不到在脚本中间成为管理员用户的方法。
有“具有管理员权限的 do shell script 'foo'”命令,但它总是进行身份验证,并且在 AppleScript 中表达我想要的步骤比 shell 更容易,尽管并非不可能。
目前,我面临着尝试在 AppleScript 中移动文件,捕获移动失败,然后使用身份验证 shell 脚本通过“mv”移动文件。这看起来很麻烦,那么我该怎么做才能让事情变得更整洁呢?
I need to run a script as part of an application first-run step, to remove some legacy components from a previous version. Typically these components are installed in either $HOME/Library or /Library, and the script can simply move them to the trash.
The problem is, in the case where the user is not an administrator, and the components are in the global /Library, the script needs to authenticate as an administrator, or it will fail. My current script uses AppleScript, but to my surprise, I can't find a way to become an admin user in the middle of a script.
There's the 'do shell script 'foo' with administrator privileges' command, but it always authenticates, and it's easier to express the steps I want in AppleScript than shell, though not impossible.
At the moment I'm faced with trying to move the files in AppleScript, catching a failure of the move, and then using an authenticating shell-script to move the files using 'mv'. This seems very cumbersome, so what can I do to make things neater?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会编写它以使用“do shell script”。首先,我会在没有管理员权限的情况下尝试 - 如果返回错误,我会在管理员权限下尝试。这样,如果不需要,则不会提示用户输入管理员凭据。
I'd write it to use "do shell script". First I'd try it without admin privileges—if that returns an error, I'd try it with admin privileges. That way the user isn't prompted for admin credentials if they are not needed.
我认为您可以使用该命令的 sudo 前缀,该命令在您上次使用后会进行 1 分钟的重新身份验证。因此,例如, mv /something /something 将是 sudo mv /something /something
I think you could use the sudo prefix for the command, which has a 1 minute re-authentication after you last used it. So, for example, mv /something /something would be sudo mv /something /something