【问题标题】:Can't access NSPasteboard using PyObjC无法使用 PyObjC 访问 NSPasteboard
【发布时间】:2018-02-27 20:25:00
【问题描述】:

我正在尝试使用 PyObjC 读取 OSX 剪贴板。

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__']

缺少许多粘贴板属性。所以clip.stringForType_(AppKit.NSStringPboardType) 会导致 AttributeError。

【问题讨论】:

  • 其他对象有合适的属性吗?您的 PyObjC 和 OS X 版本是什么?
  • 是的,dir(AppKit.NSPasteboard) 显示了很多属性。 OSX 版本 - 10.9.4。如何查看 PyObjC 版本?

标签: python objective-c pyobjc


【解决方案1】:

这里有一些 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())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 2018-02-09
    • 2016-05-16
    • 2015-04-04
    相关资源
    最近更新 更多