How to get Apple Mail badge (n. of unread messages) from script?
osascript -l JavaScript -e 'Application("Mail").inbox.unreadCount()'


This uses Mail's unified inbox to display the number of unread messages, thus, it's equal to the number on Mail's red icon badge.