I have a QTMovieView, how will I be able to call a method when the movie has finished playing?
I just ended up using:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(method:) name:#"QTMovieDidEndNotification" object:nil ];
Related
use this code
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(textChanged:) name:NSTextDidChangeNotification object:self.text3];
I can not get the notification I wanted.
But if I change the name or object to nil, it works. I wonder whether these two arguments can not be set non-nil simultaneously?
I'm trying to make a plugin, and the idea is that after you write something like
-(void)thisMethodIsSomethingIWantToBeExposed
Then I can press a keycombo like alt+c and then it'll write what is one the current line in the .h file so I don't have to copy paste it over myself.
Anyone have any idea on how to get the path of the file I'm currently writing in? Then I can just change the m to an h, and then I'll just have to figure out how to write to the h file, but that's another thing.
I found a solution, but I think it might be a bit hacky.
In my xcode plugin i put this in my init
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationListener:) name:nil object:nil];
And I made a method called
-(void)notificationListener:(NSNotification *)notification{
if (![notification.name isKindOfClass:[NSString class]]) {
if ([[notification name] isEqualToString:#"IDESourceCodeDocumentDidUpdateSourceModelNotification"] ) {
NSString *path = [ NSString stringWithFormat :#"%#",notification.object ];
path = [path substringFromIndex:49];
}
You'll get ALOT of notifications, and it took me a while to find one with information on the path, but this one has it.
Same method with
NSLog(#"Not recv: %#", notification.name);
will write out all the names of all the Notifications.
Any body have idea about getting notification from NSTask while NSTask is executed. I am unzipping a zip file using NSTask and need to show the unzip data progress in a NSProgressBar.
I don't found any idea for doing such task.So that i show the value in progress bar.
Need help for doing this task.
Thanks in advance.
Use NSFileHandleReadCompletionNotification, NSTaskDidTerminateNotification notifications.
task=[[NSTask alloc] init];
[task setLaunchPath:Path];
NSPipe *outputpipe=[[NSPipe alloc]init];
NSPipe *errorpipe=[[NSPipe alloc]init];
NSFileHandle *output,*error;
[task setArguments: arguments];
[task setStandardOutput:outputpipe];
[task setStandardError:errorpipe];
output=[outputpipe fileHandleForReading];
error=[errorpipe fileHandleForReading];
[task launch];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receivedData:) name: NSFileHandleReadCompletionNotification object:output];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receivedError:) name: NSFileHandleReadCompletionNotification object:error];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(TaskCompletion:) name: NSTaskDidTerminateNotification object:task];
//[input writeData:[NSMutableData initWithString:#"test"]];
[output readInBackgroundAndNotify];
[error readInBackgroundAndNotify];
[task waitUntilExit];
[outputpipe release];
[errorpipe release];
[task release];
[pool release];
/* Called when there is some data in the output pipe */
-(void) receivedData:(NSNotification*) rec_not
{
NSData *dataOutput=[[rec_not userInfo] objectForKey:NSFileHandleNotificationDataItem];
[[rec_not object] readInBackgroundAndNotify];
[strfromdata release];
}
/* Called when there is some data in the error pipe */
-(void) receivedError:(NSNotification*) rec_not
{
NSData *dataOutput=[[rec_not userInfo] objectForKey:NSFileHandleNotificationDataItem];
if( !dataOutput)
NSLog(#">>>>>>>>>>>>>>Empty Data");
[[rec_not object] readInBackgroundAndNotify];
}
/* Called when the task is complete */
-(void) TaskCompletion :(NSNotification*) rec_not
{
}
In order to show progress, you need to find out two things:
How many files there are in the archive, or how many bytes they will occupy after unzipping is complete
How many files or bytes you have unzipped so far
You'll find these out by reading the output from the unzip task. Parag Bafna's answer is a start; in receivedData:, you'll need to parse the output to determine what progress has just happened, and then add that progress to your running count of progress so far (e.g., ++_filesUnzippedSoFar).
The first part, finding out the total size of the job, is trickier. You basically need to run unzip before you run unzip: the first, with -l (that's a lowercase L), is to List the contents of the archive; the second is to unzip it. The first one, you read the output to determine how many files/bytes the archive contains; the second one, you read the output to determine the value to advance the progress bar to.
Setting the properties of the progress bar is the easy part; those are literally just doubleValue and maxValue. Working out where you are in the job is the hard part, and is very domain-specific—you need to read unzip's output (twice, in different forms), understand what it's telling you, and translate that into progress information.
There is nothing in NSTask that can help you with that. NSTask's part of this begins and ends at the standardOutput property. It has no knowledge of zip files, archives, contents of archives, or even progress, since none of that applies to most tasks. It's all specific to your task, which means you have to write the code to do it.
I want to call not just methods from NSNotification but the method's arguments
Something along the lines of
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playerinCombat:YES) name:#"PlayerinCombat" object:nil];
and not just
#selector(playerinCombat:)
just to use with
+ (BOOL)playerinCombat:(BOOL)flag {return flag; if (flag){NSLog(#"Player in Combat.");} if (!flag){NSLog(#"Player not in Combat.");}}
But it won't work. Any ideas?
On second thought userInfo can be used (I think)
I am working on a GUI (Cocoa) for a command-line tool to make it more accessible to people. Despite it being a GUI, I would like to display the output to an NSTextView. The problem is that the output is large and the analysis the tool carries out can take hours/days.
Normally, when working with NSTask and NSPipe, the output is displayed only after the task is completely finished (which can take a long time). What I want to do is split the output up and display it gradually (updating every minute for example).
So far I have placed the processing of the data in a separate thread:
[NSThread detachNewThreadSelector:#selector(processData:) toTarget:self withObject:raxmlHandle];
- (void)processData:(id)sender {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *startString = [results string];
NSString *newString = [[NSString alloc] initWithData:[raxmlHandle readDataToEndOfFile] encoding:NSASCIIStringEncoding];
[results setString:[startString stringByAppendingString:newString]];
[startString release];
[newString release];
[pool release];
}
All this is still a bit of voodoo to me and I am not exactly sure how to deal with this challenge.
Do you have any suggestions or recommendations?
Thanks!
You need to use a notification provided by NSFileHandle.
First, add yourself as an observer to the NSFileHandleReadCompletionNotification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(outputReceived:)
name:NSFileHandleReadCompletionNotification
object:nil];
Then, prepare a task, call readInBackgrounAndNotify of the file handle of the pipe, and launch the task.
NSTask*task=[[NSTask alloc] init];
[task setLaunchPath:...];
NSPipe*pipe=[NSPipe pipe];
[task setStandardOutput:pipe];
[task setStandardError:pipe];
// this causes the notification to be fired when the data is available
[[pipe fileHandleForReading] readInBackgroundAndNotify];
[task launch];
Now, to actually receive the data, you need to define a method
-(void)outputReceived:(NSNotification*)notification{
NSFileHandle*fh=[notification object];
// it might be good to check that this file handle is the one you want to read
...
NSData*d=[[aNotification userInfo] objectForKey:#"NSFileHandleNotificationDataItem"];
... do something with data ...
}
You might want to read Notification Programming Topics to understand what is going on.