I am trying to schedule a shutdown event with pmset
executed via NSTask
in OSX app.
NSCalendar *cal = [NSCalendar new];
NSDate *dateNext = [date dateByAddingTimeInterval:120];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/YYYY HH:mm:ss"];
NSString* dS = [df stringFromDate:dateNext];
//# pmset schedule shutdown "MM/DD/YYYY HH:MM:SS"
NSString* dateString = [NSString stringWithFormat:@"\"%@\"", dS];
NSLog(@"Date to set %@", dateString);
int pid = [[NSProcessInfo processInfo] processIdentifier];
NSPipe *pipe = [NSPipe pipe];
NSFileHandle *file = pipe.fileHandleForReading;
NSTask *task = [[NSTask alloc] init];
task.launchPath = @"/usr/bin/pmset";
NSArray *args = [NSArray arrayWithObjects:@"schedule",@"shutdown", dateString, nil];
[task setArguments:args];
task.standardOutput = pipe;
[task launch];
NSData *data = [file readDataToEndOfFile];
[file closeFile];
NSString *out = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@"Result:\n%@", out);
The log shows as follows:
2019-09-12 12:38:53.470793+0100 TheApp[51008:4925389] Date to set "09/12/2019 12:40:53"
2019-09-12 12:38:53.518372+0100 TheApp[51008:4925389] Result:
Error: Badly formatted date (2)
Error parsing scheduled event.
The dateString
argument seems to be correctly formatted between quotes, as required by pmset
, but I keep getting such error. Is there some parameter missing in NSTask
?
Thank you.