Benvenuto Visitatore ( Log In | Registrati )
![]() ![]() |
3 Sep 2008, 13:57
Messaggio
#1
|
|
|
Level 2/11 ![]() ![]() Gruppo: Forum User Messaggi: 84 Iscritto il: 4-December 07 Utente Nr.: 9.204 |
Ciao a tutti.
Volevo creare un programma che da button IB faccia partire il terminale con una stringa di comando che decido io. Come posso fare? Come faccio a far andare le stringhe su terminale da un button sulla mia interfaccia grafica? grazie a tutti saluti! |
|
|
|
|
|
|
3 Sep 2008, 14:22
Messaggio
#2
|
|
|
In cosa stai programmando? Obj-C? Non sono ferrato ma potresti associare lázione di un bottone al comando che vuoi eseguire.
-------------------- Emanuele Personale | Blog | Facebook | LinkedIn
Supporta Tevac! "The competent programmer is fully aware of the limited size of his own skull. He therefore approaches his task with full humility, and avoids clever tricks like the plague." - Edsger W. Dijkstra |
|
|
|
|
3 Sep 2008, 18:16
Messaggio
#3
|
|
|
Level 2/11 ![]() ![]() Gruppo: Forum User Messaggi: 84 Iscritto il: 4-December 07 Utente Nr.: 9.204 |
in Cocoa...lo so che devo associare la cosa...ma ilproblema è come dò comandi di tipo terminale...
|
|
|
|
4 Sep 2008, 14:12
Messaggio
#4
|
|
|
Non è così immediata la cosa.
Comunque per eseguire un comando devi usare la classe NSTask, se invece vuoi che si apra direttamente il Terminale ed esegua un comando m sa che ti conviene passare per AppleScript, poi naturalmente il collegamento al button devi gestirlo tu. -------------------- |
|
|
|
|
5 Sep 2008, 15:47
Messaggio
#5
|
|
|
Level 3/11 ![]() ![]() ![]() Gruppo: Forum User + Messaggi: 119 Iscritto il: 6-April 05 Da: Pinerolo Utente Nr.: 3.498 |
Qui un inizio da cui partire ...
Nella definizione della classe (.h) CODE /* theController */ #import <Cocoa/Cocoa.h> #import "FGBseInfo.h" @interface theController : NSObject { NSWindow *theWindow; FGBseInfo *bseInfo; //Preferenze del programma NSString *strCodiceUtente; //Codice Utente NSString *strPWUtente; //Password Utente NSString *strIPServer; //IP del server NSString *strValori; //Visualizza Finestra con risultato dati e non esce automaticamente se VALORI=SI NSString *strTimerValue; //Timer per innescare la lettura del profilo NSTimer *aTimer; NSTask *theTask; //task da eseguire NSPipe *outPutPipe; //Pipe su cui reindirizzare lo stdout NSTextView *textView; //OutPut per debug, ma anche per comodita' NSString *strPathDestination; NSMutableString *strProfile; } - (void)addAppToLoginItems; //Aggiungo l'item per autorun - (void)removeAppFromLoginItems; //Rimuovo item per uutorun - (void)_runAppleScriptWithCommand:(NSString *)commandName inScriptNamed:(NSString *)scriptName withParameterString:(NSString *)paramString; //apple script -(void) startTimer:(NSTimeInterval)interval; -(void) timerFireMethod:(NSTimer*)theTimer; -(void) stopTimer; -(BOOL) mountVolume; -(void) writeProfileData; -(void)unmountVolume; -(void)startTask:(NSString *) strPathCommand withArgs:(NSArray*) args writeTo:(NSString *) strOutPathFile; -(void)dataReady:(NSNotification*) notification; //Chiamta in call back quando ci sono dei dati disponibili nella pipe di output -(void)taskTerminated:(NSNotification*) notification; //Chiamta in call back quando il task e' stato completamente eseguito -(void)appendData:(NSData *)datiIn; //Dati letti dalla pipe -(void)cleanUp; //Rilascio allocazioni e rimuovo notifiche -(void)savePrefs; @end Nell'implementazione della classe (.m) CODE -(void)startTask:(NSString *) strPathCommand withArgs:(NSArray*) args writeTo:(NSString *) strOutPathFile{ if([strOutPathFile isEqualToString:@""]) strPathDestination=[[NSString alloc] initWithString:@""]; else strPathDestination=[[NSString alloc] initWithFormat:@"%@/%@.spx",strOutPathFile,strCodiceUtente]; //strPathDestination=[[NSString alloc] initWithFormat:@"%@/%@/%@.spx",strOutPathFile,NSUserName(),strCodiceUtente]; //versione per debug strProfile=[[NSMutableString alloc] init]; theTask =[[NSTask alloc] init]; outPutPipe=[[NSPipe alloc]init]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataReady:) name:NSFileHandleReadCompletionNotification object:[outPutPipe fileHandleForReading]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskTerminated:) name:NSTaskDidTerminateNotification object:theTask]; [theTask setLaunchPath:strPathCommand]; [theTask setCurrentDirectoryPath:[strPathCommand stringByDeletingLastPathComponent]]; [theTask setArguments:args]; [theTask setStandardOutput:outPutPipe]; [theTask setStandardError:outPutPipe]; [theTask launch]; [strProfile setString:@""]; // [[outPutPipe fileHandleForReading] readInBackgroundAndNotify]; NSData *dataInPipe; dataInPipe=[[outPutPipe fileHandleForReading] readDataToEndOfFile]; if(dataInPipe){ [self appendData:dataInPipe]; } [self cleanUp]; } -(void)dataReady:(NSNotification*) notification{ NSData *dataInPipe=[[notification userInfo] valueForKey:NSFileHandleNotificationDataItem]; if(dataInPipe){ [self appendData:dataInPipe]; } [[outPutPipe fileHandleForReading] readInBackgroundAndNotify]; } -(void)taskTerminated:(NSNotification*) notification{ NSData *dataInPipe; dataInPipe=[[outPutPipe fileHandleForReading] readDataToEndOfFile]; if(dataInPipe){ [self appendData:dataInPipe]; } [self cleanUp]; } -(void)appendData:(NSData *)datiIn{ NSString *string=[[NSString alloc] initWithData:datiIn encoding:NSUTF8StringEncoding]; [strProfile appendString:string]; [string release]; } -(void)cleanUp{ NSError *err=nil; [theTask release]; theTask=nil; [outPutPipe release]; outPutPipe=nil; [[NSNotificationCenter defaultCenter] removeObserver:self]; if(![strPathDestination isEqualToString:@""]){ [strProfile writeToFile:strPathDestination atomically:YES encoding:NSUTF8StringEncoding error:&err]; if(err !=nil){ NSString *newDesc = [[err localizedDescription] stringByAppendingString:([err localizedFailureReason] ? [err localizedFailureReason] : @"")]; NSRange endRange=NSMakeRange([[textView string] length],0); [textView replaceCharactersInRange:endRange withString:newDesc]; }else{ if(![strValori isEqualToString:@"SI"]) [textView setString:@"Salvataggio profilo completato."]; else [textView setString:strProfile]; } [self unmountVolume]; }else{ if(![strValori isEqualToString:@"SI"]) [textView setString:@"Comando eseguito."]; } [textView setNeedsDisplay:YES]; [strPathDestination release]; if(![strValori isEqualToString:@"SI"]){ [self savePrefs]; [NSApp terminate:self]; } } Esempio di chiamata del metodo // [self startTask:@"/usr/sbin/system_profiler" withArgs:[NSArray arrayWithObjects:@"-xml",@"-detailLevel",@"full",nil] writeTo:@"/Users"]; // [self startTask:@"/usr/sbin/traceroute" withArgs:[NSArray arrayWithObjects:@"www.germinara.it",nil] writeTo:@"/Users/pippo.txt"]; Ciao. |
|
|
|
![]() ![]() |
| Titolo discussione | Risposte | Autore discussione | Visite | Ultima azione | |||
|---|---|---|---|---|---|---|---|
![]() |
2 | fede86 | 40 | 27 November 2008 - 15:08 Ultimo messaggio di: fede86 |
|||
![]() |
16 | alebopp | 10.583 | 23 October 2008 - 18:12 Ultimo messaggio di: cesri |
|||
![]() |
33 | SoHo | 2.313 | 22 October 2008 - 22:46 Ultimo messaggio di: gpz500 |
|||
![]() |
3 | fede86 | 114 | 12 October 2008 - 11:08 Ultimo messaggio di: sirguich_ |
|||
![]() |
8 | Emanuele Guicciardi | 423 | 23 July 2008 - 21:11 Ultimo messaggio di: the_devil_88 |
|||
![]() |
6 | joesqualo | 155 | 10 July 2008 - 18:11 Ultimo messaggio di: joesqualo |
|||
![]() |
10 | fede86 | 178 | 26 May 2008 - 19:01 Ultimo messaggio di: Baco |
|||
![]() |
1 | macdvd | 242 | 2 May 2008 - 18:59 Ultimo messaggio di: poweruser |
|||
![]() |
9 | muppa | 358 | 2 May 2008 - 12:29 Ultimo messaggio di: poweruser |
|||
![]() |
7 | lorentini | 527 | 6 April 2008 - 13:48 Ultimo messaggio di: lorentini |
|||
![]() |
1 | fac | 314 | 6 February 2008 - 23:37 Ultimo messaggio di: poweruser |
|||
![]() |
9 | amstaff | 463 | 14 January 2008 - 01:29 Ultimo messaggio di: poweruser |
|||
|
Versione Lo-Fi | Oggi è il: 2 December 2008 - 00:21 |
| IP.Board Skin Developed By Creative Networks | ||