How can I check that the NSPasteboard is updated?
Asked Answered
S

2

6

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!

Sanctimonious answered 21/5, 2010 at 14:10 Comment(0)
F
12

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;
}
Faugh answered 21/5, 2010 at 14:26 Comment(2)
Please avoid polling anything unless you absolutely positively have to. It is inefficient in all ways.Mitochondrion
I don't disagree that it isn't efficient but how else would you do it?Faugh
R
0

Just copy the same jpeg file from desktop multiple times and you will see not exactly work with

NSLog(@"Pasteboard updated: %@", [pasteboard types]);(

sometimes:

2014-05-25 12:14:20.014 PB1[65771:303] (
    "public.file-url",
    "CorePasteboardFlavorType 0x6675726C",
    "dyn.ah62d4rv4gu8y6y4grf0gn5xbrzw1gydcr7u1e3cytf2gn",
    NSFilenamesPboardType,
    "dyn.ah62d4rv4gu8yc6durvwwaznwmuuha2pxsvw0e55bsmwca7d3sbwu",
    "Apple URL pasteboard type"
)

sometimes:

2014-05-25 12:14:25.482 PB1[65771:303] (
    "public.file-url",
    "CorePasteboardFlavorType 0x6675726C",
    "dyn.ah62d4rv4gu8y6y4grf0gn5xbrzw1gydcr7u1e3cytf2gn",
    NSFilenamesPboardType,
    "dyn.ah62d4rv4gu8yc6durvwwaznwmuuha2pxsvw0e55bsmwca7d3sbwu",
    "Apple URL pasteboard type",
    "com.apple.icns",
    "CorePasteboardFlavorType 0x69636E73",
    fccc,
    "public.utf16-external-plain-text",
    "CorePasteboardFlavorType 0x75743136",
    "public.utf8-plain-text",
    NSStringPboardType,
    "public.tiff",
    "NeXT TIFF v4.0 pasteboard type"
)
Responsible answered 25/5, 2014 at 10:47 Comment(1)
But NSStringPboardType does not always appear. How can i get name JPEG file without parsing NSFilenamesPboardTypeResponsible

© 2022 - 2024 — McMap. All rights reserved.