user1438586
user1438586

Reputation:

NSTask or AScript?

I am new to objetive-c as I came from Java (Android) and then a little of AppleScript.

So my app is making a git commit. BUT as you know the terminal has output that the user may want to see. So should I continue to use my NSTask for behind the scenes stuff, or should I just use AppleScript and let the user carry on from the terminal. Mainly my push.m looks like this :

#import "Push.h"

@implementation Push

@synthesize test;
@synthesize dirPath;


-(IBAction)chooseFolder:(id)sender{

    dirPath = [self get];
    NSArray *array = [dirPath componentsSeparatedByString:@"/"];
    NSString *pub = [array lastObject];
    [projectName setStringValue:pub];

    BOOL fileyn = [self check:dirPath];

    if(fileyn) {

    } else {

    }

}
-(IBAction)pushAction:(id)sender {
    [self push];
    [self push];
}

-(void)push{ 

    if(dirPath == nil || dirPath == @"") {
        [self chooseFolder:nil];
    }

    NSString *commitText = [commitMessage stringValue];
    [commitMessage setStringValue:@""];
    commitText = [NSString stringWithFormat:@"%@",commitText];

    if (i == 1) {

    } else {
        [self runScript:dirPath:@"add" :@"*" :nil];
        [self runScript:dirPath:@"commit" :@"-m" :commitText];
        [self runScript:dirPath:@"push" :@"origin" :@"HEAD"];
    }

}

-(void) runScript:(NSString *) path:(NSString* )cmd1:(NSString *) cmd2:(NSString *) cmd3{

    NSTask *aTask = [[NSTask alloc] init];

    NSPipe *pipe;
    pipe = [NSPipe pipe];
    [aTask setStandardOutput: pipe];

    NSFileHandle *file;
    file = [pipe fileHandleForReading];

    NSArray* args = [NSArray arrayWithObjects:cmd1,cmd2,cmd3, nil];
    [aTask setArguments:args];
    [aTask setCurrentDirectoryPath:path];
    [aTask setLaunchPath:@"/usr/local/git/bin/git"];
    [aTask setArguments:args];
    [aTask launch];

    [finished setStringValue:@"finished"];

}

-(IBAction)back:(id)sender{

    test = [[NSWindowController alloc] initWithWindowNibName:@"POP"];
    [test showWindow:self];
    [window close];

}


-(BOOL)check:(NSString *) pow{
    BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:pow isDirectory:NO];

    return isFile;
}


-(NSString *)get {  

    NSOpenPanel *panel = [NSOpenPanel openPanel];
    [panel setAllowsMultipleSelection:NO];
    [panel setCanChooseDirectories:YES];
    [panel setCanChooseFiles:NO];
    if ([panel runModal] != NSFileHandlingPanelOKButton) return nil;
    return  [[panel directoryURL] path];
}

@end

So what should I do? Any thing to improve? Thanks in advance!

Upvotes: 0

Views: 204

Answers (1)

Itai Ferber
Itai Ferber

Reputation: 29918

This sort of application design is really up to you; there's no right answer here. It depends on what you're trying to accomplish, and what you'd like to allow the user to do. If you simply want to show users the output of whatever commands you're running, you can simply get the output out of the NSTask (using the output pipe you set) and display it in a text view for the user to look through.

However, if you want to run interactive git commands, that gets a little bit more complicated. I'm going on a limb here since your question wasn't very specific about what 'let the user carry on from the terminal' means, so if this isn't what you meant, then let me know. In terms of good user interface and user experience, unless you have no other choice, it's almost never a good idea to force someone to go to a different app to keep using your own. If an app wants to display results to a user, it's best to do it in-app (with a custom view, or a web view, for instance), not to drive the user elsewhere. If you want the commands to be interactive, its far better design to come up with an interface for handling that in your app versus running an AppleScript and directing users to the terminal to see and do things.

Anyway, at a quick glance, your code seems like it should work (except for the aforementioned problems in the comments, which you should fix) — and since you hadn't mentioned any problems with it, I assume it does. This isn't a programming problem; it's a design one, and one you'll have to consider the answers to it yourself (and, that being said, I'm voting to close this question since it doesn't really fit into the StackOverflow guidelines — the answer is a matter of opinion, not of facts).

Upvotes: 1

Related Questions