【问题标题】:How can I check that the NSPasteboard is updated?如何检查 NSPasteboard 是否已更新?
【发布时间】:2010-05-21 14:10:18
【问题描述】:

我正在自动执行复制命令,以每秒左右在粘贴板上放置一些文本 - 不幸的是,这是我访问文本的唯一方法,它位于另一个应用程序中。复制后,我访问粘贴板文本并进行处理。

有时,当没有选择任何内容时会发送复制命令 - 例如在 textEdit 中,如果光标位于行尾(而不是突出显示某些文本)并且您点击复制,您会收到系统哔声,因为那里没有选择复制。粘贴板不会更新并保留其以前的数据。

我想不出一种创造性的方法来确定何时发生这种情况。如果我发送复制命令并且粘贴板没有更新,我可以访问粘贴板上是否有任何时间戳可以确认是否已捕获某些内容?

我正在查看 changeCount,但我不确定它的确切用途,并且文档对我没有多大帮助 - 红鲱鱼?

任何简单有效的想法都非常感谢!

【问题讨论】:

    标签: objective-c cocoa nspasteboard


    【解决方案1】:

    我不认为有这方面的通知,但是您可以轮询粘贴板。

    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;
    }
    

    【讨论】:

    • 除非您绝对必须这样做,否则请避免轮询任何内容。它在各方面都是低效的。
    • 我不反对它效率不高,但你会怎么做呢?
    【解决方案2】:

    只需从桌面多次复制相同的 jpeg 文件,您就会发现无法完全使用

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

    有时:

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

    有时:

    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"
    )
    

    【讨论】:

    • 但是 NSStringPboardType 并不总是出现。如何在不解析 NSFilenamesPboardType 的情况下获取名称 JPEG 文件
    猜你喜欢
    • 2011-12-05
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 2017-05-17
    • 2015-12-01
    相关资源
    最近更新 更多