0
を使用して、標準エラー出力にデータを取得する方法私は、次の機能があります。この機能は、基本的には管理者権限でコマンドを実行しますAppleScriptのシェル
func executeCommandAsAdminWithAppleScript(command: String, inout output: String?, inout errorDesc: String?) -> Bool {
var errorInfo: NSDictionary?
let source = "do shell script \"\(command)\" with administrator privileges"
let script = NSAppleScript(source: source)
let eventResult = script?.executeAndReturnError(&errorInfo)
if eventResult == nil {
errorDesc = nil
if let code = errorInfo?.valueForKey(NSAppleScriptErrorNumber) as? NSNumber {
if code.intValue == -128 {
errorDesc = "The administrator password is required to do this.";
}
if errorDesc == nil {
if let message = errorInfo?.valueForKey(NSAppleScriptErrorMessage) as? NSString {
errorDesc = String(message)
}
}
}
return false
} else {
output = (eventResult?.stringValue)!
return true
}
}
を。今度はecho Hello World
のようなコマンドを実行しようとしましたが、output
のパラメータにはHello World
のような文字列があり、それはstdout
に出力されます。しかし、データをstderr
に出力するコマンドを実行しようとすると、出力はデータを取得できません。では、NSTaskを使わずにstderr
のデータを取得する方法はありますか?ありがとう。それは標準エラーストリームを結合しますと、あなたのスクリプトでcommand
を使用
'command 2>&1'に' command'を設定すると、stdoutとstderrの両方を返すことがあります。 –
@ MarkSetchellありがとう、あなたは答えとして今投稿することができます –
確かに、私はやった - それはあなたのためにうまくいった。 –