AppleScript 中的用户属性/权限

发布于 2024-08-11 19:17:19 字数 274 浏览 7 评论 0原文

我想编写一个 applescript 程序,首先检查用户是否具有管理员权限,如果没有,则请求重新登录或其他操作。

最终,该脚本需要对我刚刚创建的文件夹执行 sudo chmod...我可以使用 do 脚本和管理员权限来完成此操作。

但是我还没有弄清楚如何为 applescript 命令请求管理员权限,或者甚至只是检查用户是否具有管理员权限。

有人知道吗?或者至少给我指出一个好的 applescript 参考文献? (Apple.com 参考资料对我没有帮助)

谢谢。

I want to write an applescript program that first checks to see if the user has Admin privileges, and if it doesn't then requesting a re-log-in or something.

Eventually the script is going to need to do a sudo chmod of a folder I just created... I can do that with a do script and a with Administrator Priviledges.

However I haven't figured out how to either request admin privs for an applescript command, or even just check if the user HAS admin privs.

Anyone know? or at least point me at a GOOD applescript ref? (Apple.com reference is not helping me)

thanks.

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

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

发布评论

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

评论(5

甚是思念 2024-08-18 19:17:19

来自苹果论坛的解决方案:

if ("80" is not in (do shell script "id -G")) then
   Error....

似乎可以解决问题。它很难阅读,正如 Philip Regan 所说,我是通过命令行完成的,但它似乎给了我所需的保护......

A solution from the Apple forum:

if ("80" is not in (do shell script "id -G")) then
   Error....

seems to do the trick. It's hard to read, and as Philip Regan said, I'm doing it via the command line, but it seems to give me the protection that I need...

梨涡 2024-08-18 19:17:19

只需使用具有管理员权限即可。如果用户没有管理员权限,Applescript 将提示他们输入名称和密码。使用 try ... on error 块,以防用户取消、输入错误密码或只是没有管理员权限。

如果您确实想知道当前用户是否是管理员,请检查该用户是否在 admin 组中:

on amIAdmin()
    set prevDelims to AppleScript's text item delimiters
    set AppleScript's text item delimiters to " "
    set groups to do shell script "id -G -n"
    set groupList to text items of groups
    set isAdmin to "admin" is in groupList
    set AppleScript's text item delimiters to prevDelims
    return isAdmin
end isAdmin

amIAdmin()

Just use the with administrator privileges. If a user doesn't have admin privileges, Applescript will prompt them for name and password. Use a try ... on error block in case the user cancels, enters the wrong password or just plain doesn't have admin rights.

If you really want to know if the current user is an administrator, check that the user is in the admin group:

on amIAdmin()
    set prevDelims to AppleScript's text item delimiters
    set AppleScript's text item delimiters to " "
    set groups to do shell script "id -G -n"
    set groupList to text items of groups
    set isAdmin to "admin" is in groupList
    set AppleScript's text item delimiters to prevDelims
    return isAdmin
end isAdmin

amIAdmin()
Oo萌小芽oO 2024-08-18 19:17:19

这是另一种尚未有人提及的替代解决方案。

dscl 命令允许您执行各种目录服务任务
,其中之一是能够查找用户的帐户类型。

命令:dscl。读取/Groups/admin GroupMembership
将列出 OS X 上的所有 admin
帐户。

因此,如果您想将其合并到 AppleScript 中,您可以执行以下操作:

set userName to "whatever username you wanted to check"
set readAdminGroup to do shell script "dscl . read /Groups/admin GroupMembership"
set AppleScript's text item delimiters to " "
set adminNames to text items of readAdminGroup

--loop through Admin Group to check if username exists
repeat with i in adminNames
 if adminNames does not contain userName then
  set isAdmin to false
 else
  set isAdmin to true
 end if
end repeat

return isAdmin 

一旦确定变量 isAdmin 是 true 还是 false,您就可以
执行多种功能。此外,如果脚本是通过 ARD 部署或发送的,您可以设置 userName 变量(上述脚本中的第一行)以使用 whoami 命令检查当前用户。所以第一行看起来像这样:

set userName to do shell script "whoami"

Here's another alternative solution which no one mentioned yet.

The dscl command allows you to perform a variety of Directory Service tasks
and one of them is the ability to look up a user's account type.

The command: dscl . read /Groups/admin GroupMembership
will list all admin
accounts on OS X.

So if you wanted to incorporate that into an AppleScript you could do the following:

set userName to "whatever username you wanted to check"
set readAdminGroup to do shell script "dscl . read /Groups/admin GroupMembership"
set AppleScript's text item delimiters to " "
set adminNames to text items of readAdminGroup

--loop through Admin Group to check if username exists
repeat with i in adminNames
 if adminNames does not contain userName then
  set isAdmin to false
 else
  set isAdmin to true
 end if
end repeat

return isAdmin 

Once you find out whether the variable isAdmin is true or false you can then
perform a variety of functions. Also, if the script was being deployed or sent through ARD you could set the userName variable (the first line in the above script) to check for the current user with a whoami command. So the first line would then look like this:

set userName to do shell script "whoami"
来日方长 2024-08-18 19:17:19

我有点恼火的是系统事件在用户对象中没有为此的属性,但基于 id 和 dscl 的查询似乎是最好的选择。为了便于阅读,我使用:

set imadmin to " admin " is in (do shell script "groups")

注意 admin 周围的空格。这可以防止它与 lpadmin 等组混淆。

I'm a little annoyed that System Events doesn't have a property in the user object for this, but the id and dscl based queries seem the best bet. For readability I use:

set imadmin to " admin " is in (do shell script "groups")

Note the spaces around admin. This prevents it form being mixed up with groups like lpadmin.

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