2013-09-06 2 views
1

J'essaie de créer une application Mac OS de base utilisant Xcode, qui permet à l'utilisateur d'éjecter un CD coincé dans son lecteur sans avoir à redémarrer son ordinateur à chaque fois. Fondamentalement, je veux avoir un NSButton sur ma fenêtre étiqueté "Eject" et je suis à la recherche du bouton pour exécuter la commande Terminal "drutil tray eject" pour éjecter avec force le CD du lecteur en ne répondant pas. Je suis un débutant à Xcode et c'est mon premier Mac App.Exécution d'une commande de terminal à partir de NSButton cliquez sur

Merci! :-)

Répondre

0

Utilisez NSTask. Jetez un oeil à documentation et Quartz Composer CommandLineTool.

+ (void) executeShellCommandAsynchronously:(NSString*)command 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    NSTask *task = [[[NSTask alloc] init] autorelease]; 
    [task setLaunchPath: @"/bin/sh"]; //we are launching sh, it is wha will process command for us 
    [task setStandardInput:[NSFileHandle fileHandleWithNullDevice]]; //stdin is directed to /dev/null 
    NSArray *args = [NSArray arrayWithObjects: @"-c", //-c tells sh to execute commands from the next argument 
        command, //sh will read and execute the commands in this string. 
        nil]; 
    [task setArguments: args]; 
    [task launch]; 
    [command release]; 
    [pool release]; 
    return; 
} 
Questions connexes