Can't Call Salesforce DX from NSTask - bash

From the Terminal, I can call all of the Salesforce DX CLI commands and functions. For example, "sfdx force:doc:commands:list" will show all the commands. BUT from the NSTask code (below) on a mac, I cannot call the Salesforce DX CLI at all. The terminationStatus flag is true. Other commands like "ls" work fine. Is this because of paths? permissions? Thanks in advance...
ptr portcommandlineinterface(char *thecommand)
{
ptr theptr;
NSTask *task;
NSPipe *pipe;
NSData *data;
NSFileHandle *file;
NSArray *arguments;
int thesize, status;
const char *junkptr;
NSString *string, *tempstring;
tempstring = [NSString stringWithUTF8String:thecommand];
if (tempstring == 0) return(0);
task = [[NSTask alloc] init];
[task setLaunchPath: #"/bin/sh"];
[task setCurrentDirectoryPath: #"~"];
arguments = [NSArray arrayWithObjects: #"-c", tempstring, nil];
[task setArguments: arguments];
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
file = [pipe fileHandleForReading];
[task launch];
[task waitUntilExit];
status = [task terminationStatus];
if (status) return(0);
data = [file readDataToEndOfFile];
string = [[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding];
junkptr = [string UTF8String];
thesize = cstrlen((char *)junkptr);
theptr = portnewptr(thesize + 10);
cstrcpy((char *)junkptr, theptr);
return(theptr);
}

Related

Results of NSTask in NSTextView

Thanks for the help. My code below works, returning results in the Console. I want to display the same results in a textView. Can't get it to work. Can anyone explain what I need to do?
Thanks.
-(IBAction)activateTask:(id)sender
{
NSURL *fileURL = [NSURL fileURLWithPath:sourceField.stringValue];
NSString *filePath= [fileURL path];
[soxTask setArguments:[NSArray arrayWithObjects:#"--show-progress", filePath, #"-n", #"stats" , nil]];
NSPipe *pipe;
pipe = [NSPipe pipe];
[soxTask setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[soxTask launch];
[soxTask waitUntilExit];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding];
[textView setString:string];
///
}
Try using -setStandardError: instead of -setStandardOutput:

Mac OSX - Get all running application list with memory usage

I am very new to develop mac osx application using xcode. I am trying to get all running application list with their memory usage.
Can any body help me in this case.
Please help me.
Thanks in advance.
It will help you to get list of running application :
for (NSRunningApplication *app in [[NSWorkspace sharedWorkspace] runningApplications]) {
NSLog(#"%#",[app localizedName]);
}
You may get the cpu usage as:
- (NSString*) get_process_usage:(int) pid
{
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath:#"/bin/ps"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: #"-O",#"%cpu",#"-p",[NSString stringWithFormat:#"%d",pid], nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSString* temp = [string stringByReplacingOccurrencesOfString:#"PID %CPU TT STAT TIME COMMAND" withString:#""];
NSMutableArray* arr =(NSMutableArray*) [temp componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[arr removeObject:#""];
[string release];
[task release];
if([arr count]>=2)
return [arr objectAtIndex:1];
else
return #"unknown";
}

Shell Command output in NSTextView

Thanks for the help. The results of this executed command is displayed in my Xcode Console. What's the best way to get the results of the command to be displayed in an NSTextView?
NSString *commandToRun = #"~/Library/webREF/ffmpeg -nostats -i ~/Desktop/input.wav - filter_complex ebur128 -f null -";
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: #"/bin/sh"];
NSArray *arguments = [NSArray arrayWithObjects:
#"-c" ,
[NSString stringWithFormat:#"%#", commandToRun],
nil];
NSLog(#"run command: %#",commandToRun);
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
Add something like:
…
NSFileHandle *file;
file = [pipe fileHandleForReading];
NSMutableData *data = [[NSMutableData alloc] init];
NSData *inData = nil;
[task setStandardOutput:pipe];
[task launch];
[task waitUntilExit];
while ((inData = [file availableData]) && [inData length]) {
[data appendData:inData];
}
[file closeFile];
[task release];
[pipe release];
NSString *result = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
[data release];
// somewhere we have an NSTextView textView
[textView setString: result];

task processIdentifier cocoa

Ok here is my code..
The return of tid is usually about 2-3 off of the actual pid. It is driving me crazy trying to figure out why it will not give me the exact pid.
So when I try to kill the process, it will not kill, I suppose i could write one to do ps -u | grep ssh vars specific to this process, but i shouldn't have too. Any ideas?
EDIT here is the actual code pasted
- (void)startService {
NSString *temp = #"-D 8080 user#127.0.0.1 -N";
task = [[NSTask alloc] init];
[task setLaunchPath: #"/usr/bin/ssh"];
[task setArguments: [temp componentsSeparatedByString:#" "]];
NSPipe *input = [NSPipe pipe];
[task setStandardInput: input];
[task launch];
int tid = [task processIdentifier];
NSLog(#"Starting task: %i", tid);
}
- (void)stopService {
if ([self isRunning]) {
int tid;
tid = [task processIdentifier];
running = false;
[task terminate];
NSString *killTask = [NSString stringWithFormat:#"/bin/kill -KILL %i", tid];
//NSLog(#"Attepting to KILL: %i", tid);
system([killTask cStringUsingEncoding:NSASCIIStringEncoding]);
//[task ];
}
}

Output text from NSTask to NSTextField OS X - XCode Objective -C

I was wondering how I would output text from NSTask and send it to a NSTextField on OSX.
I googled NSTask and got this...
but here is the code to do what you want.
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: #"/bin/ls"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: #"-l", #"-a", #"-t", nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding];
[myTextField setStringValue: string];

Resources