How do I parse lsappinfo output? There's a nice lsappinfo tool that lets you do things like monitor for app switching. I'd like to write a program that runs lsappinfo listen and takes actions when the foreground app changes (specifically, change LED colors on my new keyboard).

Does anyone know what the serialization format that lsappinfo uses is? It looks like this:

$ lsappinfo listen +becameFrontmost forever
Notification: kLSNotifyBecameFrontmost time=10/18/2017 06:58:18.843 dataRef={ "ApplicationType"="Foreground", "CFBundleIdentifier"="com.google.Chrome", "LSASN"=ASN:0x0-0x10010:, "LSFrontApplicationSeed"=3367, "LSMenuBarOwnerApplicationSeed"=3270, "LSMenuBarOwnerASN"=ASN:0x0-0x10010:, "LSOtherASN"=ASN:0x0-0x2f52f5: } affectedASN="Google Chrome" ASN:0x0-0x10010:  context=0x0 sessionID=186a6 notificationID=0x7fb529d00a90
Notification: kLSNotifyBecameFrontmost time=+0.835684s  dataRef={ "ApplicationType"="Foreground", "CFBundleIdentifier"="org.gnu.Emacs", "LSASN"=ASN:0x0-0x2f52f5:, "LSFrontApplicationSeed"=3368, "LSMenuBarOwnerApplicationSeed"=3271, "LSMenuBarOwnerASN"=ASN:0x0-0x2f52f5:, "LSOtherASN"=ASN:0x0-0x10010: } affectedASN="Emacs" ASN:0x0-0x2f52f5:  context=0x0 sessionID=186a6 notificationID=0x7fb529d00a90


I mean, I can parse this with a regex looking for just the field I want, but it would be nicer to be able to just pass it to some library rather than use a hacky regex that might fail if the data looks weird. Is this some sort of standard macOS internals serialization format that I haven't heard of, or is it something they made up just for this tool?
You don't need to - there's an open source clone of it at http://newosxbook.com/tools/lsdtrip.html which can get you the underlying API calls that Apple uses.