I have the following function:
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
}
}
This function basically runs a command with admin privileges. Now I try to execute commands like echo Hello World, the output parameter does have a string that looks like Hello World, which is outputted into stdout. But when I tried to execute commands that output data into stderr, the output cannot retrieve the data. So is there a way to retrieve data in stderr without using NSTask? Thanks.
commandtocommand 2>&1so it returns both stdout and stderr.