Can't access NSPasteboard using PyObjC
Asked Answered
S

1

0

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.

Somatology answered 5/9, 2014 at 18:39 Comment(2)
Do other objects have the proper attributes? What are your PyObjC and OS X versions?Whiteside
Yes, dir(AppKit.NSPasteboard) shows alot of attributes. OSX version - 10.9.4. How can I check PyObjC version?Somatology
T
2

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())
Tippler answered 27/2, 2018 at 12:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.