[英]Python get mac clipboard contents
我如何使用 Python (2.7) 獲取 Mac 剪貼板的內容。 有沒有比在 pbpaste 周圍制作包裝更好的方法?
謝謝!
PyObjC 是 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
。 您也可以擴展它以支持其他數據類型,請參閱NSPastboard Class 參考。
如果你已經安裝了pandas,你可以使用pandas中的function如下:
from pandas.io.clipboard import clipboard_get
text = clipboard_get()
你知道PyObjC嗎? 我想你可以用它來編寫一個與NSPasteboard接口的 Py 包裝器。 這可能比使用 pbpaste 更“優雅”。
您可以在 Mac 上使用 PIL/Pillow 抓取剪貼板(和屏幕),如下所示:
from PIL import ImageGrab, Image
# Grab clipboard and save to disk
clip = ImageGrab.grabclipboard()
clip.save("clip.png")
為了完整起見,您可以像這樣抓取屏幕:
screen = ImageGrab.grab()
# That results in this:
# <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=5120x2880 at 0x110BB7748>
# Save to disk
screen.save("screen.png")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.