无法通过PyObjC访问NSPasteboard
我正在尝试使用PyObjC读取Mac系统的剪贴板。
在Python命令行里
import AppKit
>>> clip = AppKit.NSPasteboard.generalPasteboard()
>>> dir(clip)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
很多剪贴板的属性都缺失。所以当我用clip.stringForType_(AppKit.NSStringPboardType)
时,出现了属性错误。
1 个回答
2
这里有一段Python代码,可以从剪贴板读取纯文本。如果你想添加其他类型的数据,只需要把它们放到数组myFavouriteTypes
里(然后使用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())