Can't access NSPasteboard using PyObjC

邮差的信 提交于 2019-12-25 12:00:29

问题


I'm trying to read OSX Clipboard using PyObjC.

Inside python shell

import AppKit
>>> clip = AppKit.NSPasteboard.generalPasteboard()
>>> dir(clip)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

Many pasteboard attributes are missing. So clip.stringForType_(AppKit.NSStringPboardType) results in AttributeError.


回答1:


Here's some python which will read in plain text from the clipboard. If you want to include other types, then you add them to the array myFavouriteTypes (and use dataForType).

from AppKit import NSPasteboard, NSStringPboardType 

myFavoriteTypes = [NSStringPboardType]
pb = NSPasteboard.generalPasteboard()
best_type = pb.availableTypeFromArray_(myFavoriteTypes)
if best_type:
    clipString = pb.stringForType_(best_type)
    if clipString:
        print (clipString)  
else:
    print ("No clipboard image data was retrieved.")
    print ("These types were available:")
    print (pb.types())


来源:https://stackoverflow.com/questions/25692010/cant-access-nspasteboard-using-pyobjc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!