Re: Being notified of changes to a file

2008-07-22 Thread Yann Disser

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 

@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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Being notified of changes to a file

2008-07-21 Thread Finlay Dobbie
On Mon, Jul 21, 2008 at 8:56 AM, Yann Disser <[EMAIL PROTECTED]> wrote:
> What I am trying to figure out, is how to respond right away to every single
> line that is written on stdout without waiting for the process to be
> finished.

There are many examples of this. See
 for one
of them. You need to get an NSPipe, create a corresponding
NSFileHandle and use -readInBackgroundAndNotify.

 -- Finlay
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Being notified of changes to a file

2008-07-21 Thread Dmitri Goutnik

On Jul 20, 2008, at 8:37 PM, Yann Disser wrote:


Hi everyone.

I am trying to execute a ruby script from within a Cocoa  
application. I want to use NSTask (any better ideas?).


My scripts outputs its progress by printing single lines on standard  
out containing percentages (e.g. "15%"). How can I update my  
application continuously depending on that progress? (for progress  
bars and other output)


Thank you a lot,
Yann


Take a look at AMShellWrapper (http://www.harmless.de/cocoa-code.php).

Another problem is that by default stdout is buffered, so you'll need  
to turn that off in your Ruby code to get line-by-line output  
(STDOUT.sync = true).


- Dmitri

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Being notified of changes to a file

2008-07-21 Thread Vitaly Ovchinnikov
I don't think that you can capture stdout. You need to create a pipe
and redirect your script's output there. And then read from that pipe,
parse and update your progress indicator. A good starting point is
here:
http://www.cocoadev.com/index.pl?NSPipe

On Mon, Jul 21, 2008 at 11:56 AM, Yann Disser <[EMAIL PROTECTED]> wrote:
> Thanks for your reply.
>
> Hmm, the subject is rather ill chosen. (I started to write the mail and then
> looked a little more into NSPipe before finishing it) My script actually
> writes to stdout.
>
> What I am trying to figure out, is how to respond right away to every single
> line that is written on stdout without waiting for the process to be
> finished.
>
> Perhaps I got something wrong...
>
> Thanks,
> Yann
>
> On 21. Jul 2008, at 0:38, Jens Alfke wrote:
>
>>
>> On 20 Jul '08, at 9:37 AM, Yann Disser wrote:
>>
>>> I am trying to execute a ruby script from within a Cocoa application. I
>>> want to use NSTask (any better ideas?).
>>
>> You can use the Ruby C API to run Ruby inside your process, but that's
>> definitely more work.
>>
>>> My scripts outputs its progress by printing single lines on standard out
>>> containing percentages (e.g. "15%"). How can I update my application
>>> continuously depending on that progress? (for progress bars and other
>>> output)
>>
>> Your subject implies you're writing to a file; don't do that. The Ruby
>> script should write to stdout, and you app should hook up an NSPipe to the
>> NSTask to read its output directly. (This can be a bit tricky, but there's
>> Apple sample code that shows how to do it.)
>>
>> —Jens
>
> ___
>
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
>
> 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:
> http://lists.apple.com/mailman/options/cocoa-dev/vitaly.ovchinnikov%40gmail.com
>
> This email sent to [EMAIL PROTECTED]
>
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Being notified of changes to a file

2008-07-21 Thread Michael Ash
On Mon, Jul 21, 2008 at 3:56 AM, Yann Disser <[EMAIL PROTECTED]> wrote:
> Thanks for your reply.
>
> Hmm, the subject is rather ill chosen. (I started to write the mail and then
> looked a little more into NSPipe before finishing it) My script actually
> writes to stdout.
>
> What I am trying to figure out, is how to respond right away to every single
> line that is written on stdout without waiting for the process to be
> finished.

If the problem is that you get big chunks all at once instead of line
by line, then the problem is in your ruby program, not your Cocoa
program. You need to flush stdout after you write each line, otherwise
the system will buffer the output for better performance, which will
lead to chunking. There is nothing special to do on the Cocoa side
besides sign up for notifications or block on -availableData in a
thread. But you won't get chunks smaller than what the other side is
writing.

Mike
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Being notified of changes to a file

2008-07-21 Thread Yann Disser

Thanks for your reply.

Hmm, the subject is rather ill chosen. (I started to write the mail  
and then looked a little more into NSPipe before finishing it) My  
script actually writes to stdout.


What I am trying to figure out, is how to respond right away to every  
single line that is written on stdout without waiting for the process  
to be finished.


Perhaps I got something wrong...

Thanks,
Yann

On 21. Jul 2008, at 0:38, Jens Alfke wrote:



On 20 Jul '08, at 9:37 AM, Yann Disser wrote:

I am trying to execute a ruby script from within a Cocoa  
application. I want to use NSTask (any better ideas?).


You can use the Ruby C API to run Ruby inside your process, but  
that's definitely more work.


My scripts outputs its progress by printing single lines on  
standard out containing percentages (e.g. "15%"). How can I update  
my application continuously depending on that progress? (for  
progress bars and other output)


Your subject implies you're writing to a file; don't do that. The  
Ruby script should write to stdout, and you app should hook up an  
NSPipe to the NSTask to read its output directly. (This can be a bit  
tricky, but there's Apple sample code that shows how to do it.)


—Jens


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Being notified of changes to a file

2008-07-20 Thread Jens Alfke


On 20 Jul '08, at 9:37 AM, Yann Disser wrote:

I am trying to execute a ruby script from within a Cocoa  
application. I want to use NSTask (any better ideas?).


You can use the Ruby C API to run Ruby inside your process, but that's  
definitely more work.


My scripts outputs its progress by printing single lines on standard  
out containing percentages (e.g. "15%"). How can I update my  
application continuously depending on that progress? (for progress  
bars and other output)


Your subject implies you're writing to a file; don't do that. The Ruby  
script should write to stdout, and you app should hook up an NSPipe to  
the NSTask to read its output directly. (This can be a bit tricky, but  
there's Apple sample code that shows how to do it.)


—Jens

smime.p7s
Description: S/MIME cryptographic signature
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Being notified of changes to a file

2008-07-20 Thread Yann Disser

Hi everyone.

I am trying to execute a ruby script from within a Cocoa application.  
I want to use NSTask (any better ideas?).


My scripts outputs its progress by printing single lines on standard  
out containing percentages (e.g. "15%"). How can I update my  
application continuously depending on that progress? (for progress  
bars and other output)


Thank you a lot,
Yann
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]