Re: Being notified of changes to a file
Re: Being notified of changes to a file
- Subject: Re: Being notified of changes to a file
- From: Yann Disser <email@hidden>
- Date: Tue, 22 Jul 2008 12:08:11 +0200
Ok, thank you all guys! I got it working somehow ^^
Here is my code (if anyone is interested - I am probably doing a lot
of crazy things, please correct me):
#import <Cocoa/Cocoa.h>
@interface Observer : NSObject
{}
- (void) outputStuff:(NSNotification*)aNotification;
@end
@implementation Observer
- (void) outputStuff:(NSNotification*)aNotification
{
id obj = [aNotification object];
NSData* data = [[aNotification userInfo]
objectForKey:NSFileHandleNotificationDataItem];
if(data && [data length] > 0)
{
NSString* str = [[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding];
NSLog(@" -- outputting stuff -- ");
NSLog(str);
[obj readInBackgroundAndNotify];
}
}
@end
int main(int argc, char *argv[])
{
Observer* obs = [[Observer alloc] retain];
NSTask* task = [[NSTask alloc] init];
[task setLaunchPath:@"numbers.rb"];
NSPipe* output = [NSPipe pipe];
[task setStandardOutput:output];
NSFileHandle* file = [output fileHandleForReading];
[[NSNotificationCenter defaultCenter] addObserver:obs
selector:@selector(outputStuff:)
name:NSFileHandleReadCompletionNotification
object:file];
[file readInBackgroundAndNotify];
[task launch];
return NSApplicationMain(argc, (const char **) argv);
}
And here is the ruby script:
#!/usr/bin/env ruby
STDOUT.sync = true
100.times do |i|
puts i
sleep 0.05
end
Thanks again,
Yann
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden