2016-04-25 25 views
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を使用

+0

'command 2>&1'に' command'を設定すると、stdoutとstderrの両方を返すことがあります。 –

+0

@ MarkSetchellありがとう、あなたは答えとして今投稿することができます –

+0

確かに、私はやった - それはあなたのためにうまくいった。 –

答えて

2

は、

command 2>&1 

でそれを交換してください(2)うち標準ストリームと(1)、その後必要になりますそれらを両方ともあなたに返してください。