macOS Ventura更新后如何读取iMessage数据库attributedBody列明文?
提取macOS Ventura中Messages数据库attributedBody列的明文内容
macOS Ventura更新后,Messages已发送消息的内容存储在attributedBody列的二进制blob中,这个blob是归档后的NSAttributedString对象,以下是两种可行的明文提取方法:
Python + PyObjC 解析
先安装依赖:pip install pyobjc用以下代码解析blob数据:
import objc from Foundation import NSData, NSKeyedUnarchiver def get_plain_text_from_blob(blob_bytes): ns_data = NSData.dataWithBytes_length_(blob_bytes, len(blob_bytes)) unarchiver = NSKeyedUnarchiver.unarchiverForReadingWithData_(ns_data) attributed_str = unarchiver.decodeObjectForKey_("NS.objects") unarchiver.finishDecoding() return attributed_str.string() if attributed_str else None # 使用示例:从数据库读取attributedBody二进制数据后调用 # blob = your_database_query_result # print(get_plain_text_from_blob(blob))AppleScript 命令行解析
将blob内容保存到本地文件后,执行以下脚本提取:set blobFilePath to "/Users/yourname/attributed_body.blob" set blobData to read blobFilePath as «class blob» set attrString to current application's NSKeyedUnarchiver's unarchivedObjectOfClass:(current application's NSAttributedString) fromData:blobData |error|:(missing value) return attrString's string() as text执行命令:
osascript -e '上面的AppleScript代码'
核心逻辑:attributedBody存储的是经NSKeyedArchiver归档的NSAttributedString,通过解归档工具还原对象后,调用string()方法即可提取纯文本内容。
内容的提问来源于stack exchange,提问作者stephancasas
相关产品推荐
相关产品推荐

