Python获取Mac剪贴板内容 [英] Python get mac clipboard contents

查看:473
本文介绍了Python获取Mac剪贴板内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Python(2.7)获取Mac剪贴板的内容.有没有比在pbpaste周围包装更好的方法?

How can I, using Python (2.7) get the contents of the Mac clipboard. Is there a better way than making a wrapper around pbpaste?

谢谢!

推荐答案

PyObjC是必经之路:

PyObjC is the way to go:

#!/usr/bin/python

from AppKit import NSPasteboard, NSStringPboardType

pb = NSPasteboard.generalPasteboard()
pbstring = pb.stringForType_(NSStringPboardType)
print u"Pastboard string: %s".encode("utf-8") % repr(pbstring)

这仅支持文本,否则将返回None.您也可以扩展它以支持其他数据类型,请参见

This only supports text and will return None otherwise. You can extend it to support other data types as well, see NSPastboard Class Reference.

这篇关于Python获取Mac剪贴板内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆