IPB     Il futuro di Tevac dipende da te!  

Benvenuto Visitatore ( Log In | Registrati )

  · · · · · · · · · · · ·
Reply to this topicStart new topic
> Inserire comandi da terminale
stefano1983
messaggio 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!
Go to the top of the page
 
+Quote Post
sirguich_
messaggio 3 Sep 2008, 14:22
Messaggio #2


Più proxymizzato che mai
***********

Gruppo: Team Moderatori
Messaggi: 9.576
Iscritto il: 18-March 04
Da: Ripa (LU)
Utente Nr.: 1.649



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
Go to the top of the page
 
+Quote Post
stefano1983
messaggio 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...
Go to the top of the page
 
+Quote Post
MacMomo
messaggio 4 Sep 2008, 14:12
Messaggio #4


Level 5/11
*****

Gruppo: Forum User +
Messaggi: 728
Iscritto il: 21-June 05
Da: Ostra Vetere (AN)
Utente Nr.: 3.979



Non è così immediata la cosa. icon_neutral.gif
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.


--------------------
Marco *
MacBook Nero 2.2 GHz Intel Core 2 Duo - 4 GB RAM
Go to the top of the page
 
+Quote Post
germinara
messaggio 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.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

 

Collapse

> 

    Titolo discussione Risposte Autore discussione Visite Ultima azione
No New Posts   2 fede86 40 27 November 2008 - 15:08
Ultimo messaggio di: fede86
No new   16 alebopp 10.583 23 October 2008 - 18:12
Ultimo messaggio di: cesri
No new   33 SoHo 2.313 22 October 2008 - 22:46
Ultimo messaggio di: gpz500
No New Posts   3 fede86 114 12 October 2008 - 11:08
Ultimo messaggio di: sirguich_
No New Posts   8 Emanuele Guicciardi 423 23 July 2008 - 21:11
Ultimo messaggio di: the_devil_88
No New Posts   6 joesqualo 155 10 July 2008 - 18:11
Ultimo messaggio di: joesqualo
No new   10 fede86 178 26 May 2008 - 19:01
Ultimo messaggio di: Baco
No New Posts   1 macdvd 242 2 May 2008 - 18:59
Ultimo messaggio di: poweruser
No new   9 muppa 358 2 May 2008 - 12:29
Ultimo messaggio di: poweruser
No New Posts 7 lorentini 527 6 April 2008 - 13:48
Ultimo messaggio di: lorentini
No New Posts   1 fac 314 6 February 2008 - 23:37
Ultimo messaggio di: poweruser
No new   9 amstaff 463 14 January 2008 - 01:29
Ultimo messaggio di: poweruser

Modalità di visualizzazione: Normale · Passa a: Lineare · Passa a: Outline


RSS Versione Lo-Fi Oggi è il: 2 December 2008 - 00:21
IP.Board Skin Developed By Creative Networks