【发布时间】:2011-01-25 08:44:57
【问题描述】:
我正在运行一个通过终端运行 shellscript 的 applscript。
我希望能够使用“以管理员权限执行 shell 脚本”,但终端以 root 身份运行我的脚本,而不是以普通用户(即管理员)身份运行。
我不想以 root 身份运行的原因(除了明显的原因)是我使用 ~ 符号,因此任何用户都可以登录并在本地运行脚本(例如,我将日志文件直接写入用户的桌面) .
我不想以 root 身份运行的另一个原因是我在我的 shell 脚本中使用的 ViseX 安装程序不能以 root 身份运行。
这是我使用的 applescript(感谢 regulus6633 提供的 applescript):
set theFile to "myscriptname.sh"
-- launch the application with admin privileges and get the pid of it
set thePID to (do shell script "/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal > /dev/null 2>&1 & echo $!" with administrator privileges) as integer
-- get the bundle identifier of that pid so we can do something with the application
delay 0.2
tell application "System Events"
set theProcess to first process whose unix id is thePID
set bi to bundle identifier of theProcess
end tell
-- runs the Terminal with the shellscript
set theFileAlias to (POSIX file theFile) as alias
tell application id bi
activate
open theFileAlias
end tell
【问题讨论】:
-
为什么一定要在终端里面运行脚本???您可以使用
do shell script... 运行脚本本身 -
我尝试在 applescript 中运行脚本,但没有成功。它只是冻结了。脚本本身需要一些用户输入,它还会向屏幕输出大量数据。
标签: macos shell applescript