dgelessus / python-typedstream

A pure Python, cross-platform library/tool for reading Mac OS X and NeXTSTEP typedstream files
GNU General Public License v3.0
23 stars 3 forks source link

How to access content once decoded #2

Closed forthrin closed 1 year ago

forthrin commented 1 year ago

Once you have decoded a typestream, how do you access content (ie. "bar")?

foo = typedstream.unarchive_from_file("typedstream")
# <-- What comes here?
type b'@': object of class NSMutableAttributedString v0, extends NSAttributedString v0, extends NSObject v0:
        super object: <NSObject>
        type b'@': NSMutableString('bar')
dgelessus commented 1 year ago

It depends on the data stored in the typedstream. Some common classes like NSString are translated to Python objects with proper attributes - for example, string.value to get the contents of a NSString. But if a class is not recognized by the library (like NSMutableAttributedString here), it gets translated to a generic representation where you have to access the fields by index - for example attstr.contents[0].value.

So in your example it should be:

foo = typedstream.unarchive_from_file("typedstream")
string = foo.contents[0].value # NSMutableString('bar')
print(string.value) # bar