Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check that the NSPasteboard is updated?

I'm automating a copy command to place some text on the pasteboard every second or so - unfortunately this is my only way of accessing the text, which is in another application. After copying, I access the pasteboard text and process it.

Sometimes, the copy command will be sent when nothing is selected - for example in textEdit, if the cursor is at the end of a line (instead of highlighting some text) and you hit copy, you get a system beep because there is nothing selected to copy. The pasteboard does not update and retains its previous data.

I can't think of a creative way to identify when this happens. If I send a copy command and the pasteboard doesn't update, is there any kind of time stamp on the pasteboard I can access that will confirm that something has or hasn't been captured?

I was looking at the changeCount, but I'm not sure what that is for exactly, and the documentation didn't help me much - red herring?

Any simple and effective ideas gratefully received!

like image 365
Ben Packard Avatar asked May 21 '10 14:05

Ben Packard


1 Answers

I do not believe there exists a notification for this however you can poll the pasteboard.

pasteboard = [[NSPasteboard generalPasteboard] retain];
[NSTimer scheduledTimerWithTimeInterval:0.25 target:self
                               selector:@selector(pollPasteboard:)
                               userInfo:nil repeats:YES];

- (void)pollPasteboard:(NSTimer *)timer {
    NSInteger currentChangeCount = [pasteboard changeCount];
    if (currentChangeCount == previousChangeCount)
        return;
    NSLog(@"Pasteboard updated: %@", [pasteboard types]);
    previousChangeCount = currentChangeCount;
}
like image 194
Alex Winston Avatar answered Oct 24 '22 05:10

Alex Winston