我已经四处寻找,但找不到任何适合我的问题。
我想创建一个脚本来复制以下内容:
打开终端
执行以下命令:
sudo kextunload /System/Library/Extensions/AppleHDA.kext
然后让我输入我的OSX管理员密码。
然后执行以下操作:
sudo kextload /System/Library/Extensions/AppleHDA.kext
我对applecript很新,所以希望有人可以帮助我。
谢谢!
答案 0 :(得分:4)
对问题的评论中的提示是正确的(在[Apple]脚本编辑器中,选择File > Open Dictionary...
,选择StandardAdditions.osax
,然后搜索do shell script
以查看完整语法),但重要的是要注意 do shell script
不会打开终端窗口;相反,它将运行shell命令隐藏并返回其结果 - 这通常是可取的:
do shell script
的返回值是shell命令的stdout
输出。stderr
输出。要运行具有管理权限的命令,您有两个选项:
set shCmds to "kextunload /System/Library/Extensions/AppleHDA.kext;
kextload /System/Library/Extensions/AppleHDA.kext"
# This will prompt for an admin password, then execute the commands
# as if they had been run with `sudo`.
do shell script shCmds with administrator privileges
set shCmds to "kextunload /System/Library/Extensions/AppleHDA.kext;
kextload /System/Library/Extensions/AppleHDA.kext"
# Replace `{myPassword}` with your actual password.
# The commands will run as if they had been executed with `sudo`.
do shell script shCmds ¬
user name short user name of (system info) password "{myPassword}" ¬
with administrator privileges
如上所述,如果出现问题 - 无论是由于密码无效还是取消密码对话框还是shell命令返回非零退出代码 - 都会引发运行时错误。
这是一个捕获它并通过display alert
报告它的示例。
try
do shell script shCmds with administrator privileges
on error errMsg number errNo
display alert "Executing '" & shCmds & "' failed with error code " & ¬
errNo & " and the following message: " & errMsg
return
end try