Trigger an event when clipboard content changes

前端 未结 2 1239
轮回少年
轮回少年 2020-12-13 01:03

I\'m trying to get the clipboard content using a Python script on my Mac Lion.

I\'m searching for an event or something similar, because if I use a loop, my applicat

2条回答
  •  渐次进展
    2020-12-13 01:48

    Looking at pyperclip the meat of it on Macosx is :

    import os
    def macSetClipboard(text):
        outf = os.popen('pbcopy', 'w')
        outf.write(text)
        outf.close()
    
    def macGetClipboard():
        outf = os.popen('pbpaste', 'r')
        content = outf.read()
        outf.close()
        return content
    

    These work for me how do you get on?

    I don't quite follow your comment on being in a loop.


    EDIT Added 'orrid polling example that shows how changeCount() bumps up on each copy to the pasteboard. It's still not what the OP wants as there seems no event or notification for modifications to the NSPasteboard.

    from LaunchServices import *
    from AppKit import *
    import os
    
    from threading import Timer
    
    def poll_clipboard():
        pasteboard = NSPasteboard.generalPasteboard()
        print pasteboard.changeCount()
    
    def main():
        while True:
            t = Timer(1, poll_clipboard)
            t.start()
            t.join()
    
    if __name__ == "__main__":
        main()
    

提交回复
热议问题