问题
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