bra*_*sch 3 unix applescript sudo gksudo
我需要使用 sudo 执行命令,并希望显示一个对话框窗口供用户输入其凭据。尝试使用 Applescript 自定义提示简直令人痛苦,并且使用内置的“以管理员权限执行 shell 脚本”不允许自定义窗口,因此用户知道请求来自何处。

当然,有一种方法可以显示一个窗口,让用户输入他们的凭据并将值发送回 sudo 以执行命令?cocoasudo看起来很有前途,但它也在提示窗口中写了“cocoasudo”,我需要用我的应用程序名称替换它。有没有人找到实现这种功能的解决方案?
构建自定义窗口超出了基本 applescript 的范围。如果需要,您将需要扩展您的编程技能。您需要学习如何利用 Apple 提供的工具来创建窗口等。底线是您需要学习 AppleScriptObjC 或 Objective-C/Cocoa API 以及如何在 Xcode 中使用它们。
话虽如此,如果您不喜欢学习新东西,请使用您拥有的工具。像这样的事情会起作用。您可以自定义图标、按钮、文本等。您甚至可以在输入密码时使用隐藏答案来保护用户。无需进一步学习,您可以使用 Applescript 做很多事情......
display dialog "OSAScript will need an Admin User name and password in order to make your changes." & return & return & "Please enter an admin username." default answer "" with icon 2
set username to text returned of result
display dialog "OSAScript will need an Admin User name and password in order to make your changes." & return & return & "Please enter an admin password." default answer "" with icon 2 with hidden answer
set pssword to text returned of result
do shell script "osascript -e \"return 1\"" user name username password pssword with administrator privileges
Run Code Online (Sandbox Code Playgroud)