Labels

tools (7) programming (6) cluj (4) productivity (4) rails (4) misc (3) startups (3) internet (2) software (2) emberjs (1) hack (1) meetup (1) os x (1) science (1)

Monday, March 12, 2018

OS X: How to programatically access your SMS messages (and a short bookkeeping story)

I tried various ways of keeping closer watch of my finances; at one point I had a quite interesting solution which I thought will evolve into something smooth and heavily automated. It involved parsing bank statement PDFs and scanning receipts with Wave Receipts.




But dealing with the various grey areas took an considerable amount of time. The scanning app doesn't do a very good job at categorisation, it occasionally misreads values and all receipts must be manually approved from the web interface, so in the end it didn't save me time.

Later, I realised I am already receiving most of my transactions details through SMS from my bank. And they have a somewhat homogenous structure which would make them easy to parse. I am also receiving them with the Messages app, through iCloud.


Surprisingly, they were easy to find and they are stored unencrypted. They can be found in
~/Library/Containers/com.apple.iChat/Data/Library/Messages/Archive/
There is one directory for each date and one *.ichat file per contact. The files do contain some formatting information so they are not exclusively plain text.

Here's a quick script for fetching all messages from the previous month sent from My Bank.

find ~/Library/Containers/com.apple.iChat/Data/Library/Messages/Archive/ \
-type d -name "$(date -v -1m "+%Y-%m")-[01-31]?" | \
xargs -I DIR \
find DIR -name "My Bank*" | \
xargs -I FILE strings -- FILE

I'm using the strings utility for extracting only the readable data from the files. I've left out a couple of more lines dealing with parsing the actual data using grep and sed.