{"id":4618,"date":"2026-11-19T20:35:21","date_gmt":"2026-11-20T02:35:21","guid":{"rendered":"https:\/\/8stars.org\/a\/?p=4618"},"modified":"2026-11-20T08:42:17","modified_gmt":"2026-11-20T14:42:17","slug":"mailwidget-a-mailbox-counter-widget-for-terminalwidget","status":"publish","type":"post","link":"https:\/\/8stars.org\/a\/2026\/11\/19\/mailwidget-a-mailbox-counter-widget-for-terminalwidget\/","title":{"rendered":"Mailwidget, a mailbox-counter widget for TerminalWidget"},"content":{"rendered":"<p>Once upon a time there was a Mac utility called Mail Badger that would add extra badges to the Mail.app dock icon to show the unread count in other mailboxes of your choosing. I relied on this a lot&#8211;I filter my incoming mail aggressively, and there are some high-priority mailboxes that aren&#8217;t my inbox, so I want to know when there&#8217;s something to look at in them.<\/p>\n<p>Eventually that went away and was replaced by the SmallCubed MailSuite. Eventually Apple made it impossible for that to keep working, so the developer wound up creating <a href=\"https:\/\/mailmaven.app\">his own mail app<\/a> entirely, but I didn&#8217;t really bond with that.<\/p>\n<p>However, it is possible to obtain unread counts for arbitrary mailboxes through AppleScript, and I realized that <a href=\"https:\/\/terminalwidget.app\">TerminalWidget<\/a> could provide me with an approximation of that.<\/p>\n<h2>Setup<\/h2>\n<ul>\n<li>Run <strong>TerminalWidget<\/strong> if you&#8217;re not running it already.<\/li>\n<li>Create a widget and name it <strong>mailwidget<\/strong>.<\/li>\n<li>Copy the following script and save it as <strong>mailcounter.sh<\/strong> to <code>\/usr\/local\/bin<\/code>.<\/li>\n<li>This script includes a configuration section where you list the mailboxes you want to include and how you want their names to be displayed in the widget.\n<ul>\n<li>I like using emoji as mailbox names to keep things compact. I ran into an edge case with these, and found that for emoji to display in the widget correctly, you need to go to System Settings\/Desktop &amp; Dock, and set <strong>Dim widgets on desktop<\/strong> to <strong>Never<\/strong>.<\/li>\n<li>The Inbox is special, and can be identified only as <strong>Inbox<\/strong>, but for other mailboxes, you need to give the account name and path, as shown in the example configuration below.<\/li>\n<li>To enter your account name, open Mail&#8217;s settings, click the <strong>Accounts<\/strong> tab, and click on the account in question. Use the exact text shown in the <strong>Description<\/strong> field. <\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/8stars.org\/a\/content\/Email-Account-Settings-1024x736.png\" alt=\"\" width=\"1024\" height=\"736\" class=\"alignnone size-large wp-image-4619\" srcset=\"https:\/\/8stars.org\/a\/content\/Email-Account-Settings-1024x736.png 1024w, https:\/\/8stars.org\/a\/content\/Email-Account-Settings-300x215.png 300w, https:\/\/8stars.org\/a\/content\/Email-Account-Settings-768x552.png 768w, https:\/\/8stars.org\/a\/content\/Email-Account-Settings-1536x1103.png 1536w, https:\/\/8stars.org\/a\/content\/Email-Account-Settings.png 1768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<pre><code>- You can play around with the values for the `--fg` and `--bg` flags to change the foreground and background colors as you see fit. Or anything else, if you know what you're doing.\n<\/code><\/pre>\n<hr \/>\n<pre><code class=\"language-shell\">#!\/bin\/zsh\n\n# ==========================================\n# CONFIGURATION SECTION\n# Format: \"Mailbox Path, Display Name\"\n# Combined inbox is special, and can be referred to as \"Inbox\", \n# but other mailboxes need your account name preceding the mailbox path\n# ==========================================\nMAILBOXES=(\n    \"Inbox,\ud83d\udced\"\n    \"myname@email.com email\/Family,Fam\"\n    \"myname@email.com email\/Work\/Felcher account,Felcher\"\n)\n# ==========================================\n\nif command -v terminal-widget &gt;\/dev\/null 2&gt;&amp;1; then\n    TW_CMD=\"terminal-widget\"\nelif [[ -x \"\/usr\/local\/bin\/terminal-widget\" ]]; then\n    TW_CMD=\"\/usr\/local\/bin\/terminal-widget\"\nelif [[ -x \"$HOME\/bin\/terminal-widget\" ]]; then\n    TW_CMD=\"$HOME\/bin\/terminal-widget\"\nelif [[ -x \"\/opt\/homebrew\/bin\/terminal-widget\" ]]; then\n    TW_CMD=\"\/opt\/homebrew\/bin\/terminal-widget\"\nelse\n    echo \"Error: terminal-widget CLI not found.\"\n    exit 1\nfi\n\ncsv_output=\"\"\nmessage_count=0\n\nfor line in \"${MAILBOXES[@]}\"; do\n    mailbox=$(echo \"$line\" | awk -F',' '{print $1}' | xargs)\n    display_name=$(echo \"$line\" | awk -F',' '{print $2}' | xargs)\n\n    [[ -z \"$display_name\" ]] &amp;&amp; display_name=\"${mailbox##*\/}\"\n    [[ -z \"$mailbox\" ]] &amp;&amp; continue\n\n    unread_count=$(osascript \\\n        -e \"tell application \\\"Mail\\\"\" \\\n        -e \"set defaultDelimiters to AppleScript's text item delimiters\" \\\n        -e \"set fullPath to \\\"$mailbox\\\"\" \\\n        -e \"set resultCount to \\\"Not Found\\\"\" \\\n        -e \"if fullPath is \\\"INBOX\\\" or fullPath is \\\"Inbox\\\" then\" \\\n        -e \"set resultCount to unread count of inbox\" \\\n        -e \"else if fullPath contains \\\"\/\\\" then\" \\\n        -e \"set AppleScript's text item delimiters to \\\"\/\\\"\" \\\n        -e \"set acctName to text item 1 of fullPath\" \\\n        -e \"set boxPath to text items 2 thru -1 of fullPath as string\" \\\n        -e \"if exists account acctName then\" \\\n        -e \"if boxPath is \\\"INBOX\\\" or boxPath is \\\"Inbox\\\" then\" \\\n        -e \"set resultCount to unread count of (mailbox \\\"INBOX\\\" of account acctName)\" \\\n        -e \"else if exists mailbox boxPath of account acctName then\" \\\n        -e \"set resultCount to unread count of (mailbox boxPath of account acctName)\" \\\n        -e \"end if\" \\\n        -e \"end if\" \\\n        -e \"else if exists mailbox fullPath then\" \\\n        -e \"set resultCount to unread count of fullPath\" \\\n        -e \"end if\" \\\n        -e \"set AppleScript's text item delimiters to defaultDelimiters\" \\\n        -e \"return resultCount\" \\\n        -e \"end tell\")\n\n    [[ \"$unread_count\" == \"0\" || \"$unread_count\" == \"Not Found\" ]] &amp;&amp; continue\n\n    csv_output+=\"$display_name,$unread_count\"$'\\n'\n    ((message_count++))\ndone\n\nif [[ $message_count -gt 0 ]]; then\n    echo \"$csv_output\" | \"$TW_CMD\" --target mailwidget --table - --grid horizontal --no-header --table-layout fill --fg \"#999999\" --bg \"#333333\" --fit-text\nelse\n    echo \"No unread messages\" | \"$TW_CMD\" --target mailwidget --text - --fg \"#999999\" --bg \"#333333\" --font-size 16\nfi\n\n<\/code><\/pre>\n<hr \/>\n<ul>\n<li>Make the new script executable: <code>chmod +x mailcounter.sh<\/code><\/li>\n<li>Make sure \/usr\/local\/bin is in your $PATH: <code>echo -e ${PATH\/\/:\/\\\\n}<\/code>. If it is not there, edit either your .bash_profile or .zshrc and add the line: <code>export PATH=$PATH:\/usr\/local\/bin<\/code><\/li>\n<\/ul>\n<h2>Triggers<\/h2>\n<p>In a perfect world, this script would be run A. whenever you receive new mail, and B. whenever the read state of a message in one of your listed mailboxes changes. Unfortunately, we do not live in that world. There are three ways we can trigger the script, and these get us pretty close. The script has little enough overhead that you can do all three.<\/p>\n<ul>\n<li>Whenever new mail is received;<\/li>\n<li>Whenever Mail.app becomes inactive;<\/li>\n<li>On a regular interval.<\/li>\n<\/ul>\n<h3>Whenever new mail is received<\/h3>\n<ul>\n<li>Open Script Editor and paste in the following:<\/li>\n<\/ul>\n<hr \/>\n<pre><code class=\"language-applescript\">using terms from application \"Mail\"\n    on perform mail action with messages theMessages for rule theRule\n        do shell script \"\/bin\/zsh \/usr\/local\/bin\/mailcounter.sh\"\n    end perform mail action with messages\nend using terms from\n<\/code><\/pre>\n<hr \/>\n<ul>\n<li>Save this as <code>mailcounter_trigger.scpt<\/code> to <code>~\/Library\/Application Scripts\/com.apple.mail<\/code><\/li>\n<li>Go into Mail.app settings, click the <strong>Rules<\/strong> tab, and create a new rule as shown below.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/8stars.org\/a\/content\/mail-rule-1024x365.png\" alt=\"\" width=\"1024\" height=\"365\" class=\"alignnone size-large wp-image-4620\" srcset=\"https:\/\/8stars.org\/a\/content\/mail-rule-1024x365.png 1024w, https:\/\/8stars.org\/a\/content\/mail-rule-300x107.png 300w, https:\/\/8stars.org\/a\/content\/mail-rule-768x274.png 768w, https:\/\/8stars.org\/a\/content\/mail-rule.png 1194w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<ul>\n<li>Now, go into <strong>System Settings<\/strong> and click on <strong>Privacy &amp; Security<\/strong>, then <strong>Full Disk Access<\/strong>. Enabled this for Mail. You will need to quit and restart mail. This step is needed to allow the script to run.<\/li>\n<\/ul>\n<h3>Whenever Mail.app becomes inactive<\/h3>\n<p>This doesn&#8217;t let us update read counts on a live basis, but it&#8217;s the next best thing. This will activate when you click away from Mail. There may be more than one way to do this, but the way I&#8217;ve found is using <a href=\"https:\/\/folivora.ai\">BetterTouchTool<\/a>, an amazing utility that I recommend. The interface is a little confusing at first, and it has capabilities you&#8217;ll probably never make use of, but it&#8217;s very worthwhile.<\/p>\n<ul>\n<li>Open the BTT <strong>Configuration<\/strong> window. <\/li>\n<li>Click on the <strong>Automations &amp; Named &amp; Other Triggers<\/strong> tab, which appears in the horizontal tab bar as &#8220;traffic light&#8221; buttons.<\/li>\n<li>Add a new trigger by clicking the big &#8220;+&#8221; in the left column and choose <strong>Specific App Did Deactivate<\/strong> (the search box helps\u2014there are a lot of triggers).<\/li>\n<li>Select <strong>Mail<\/strong> as the specific app in the rightmost column. Leave the other trigger options alone.<\/li>\n<li>In the second-rightmost column, under <strong>Action executed on trigger<\/strong>, select <strong>Execute Shell Script\/Task<\/strong> (again, there&#8217;s a search box that will speed things up here), then in the rightmost column, enter &#8220;\/usr\/local\/bin\/mailcounter.sh&#8221; in the <strong>Script:<\/strong> field.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/8stars.org\/a\/content\/btt-1024x699.png\" alt=\"\" width=\"1024\" height=\"699\" class=\"alignnone size-large wp-image-4621\" srcset=\"https:\/\/8stars.org\/a\/content\/btt-1024x699.png 1024w, https:\/\/8stars.org\/a\/content\/btt-300x205.png 300w, https:\/\/8stars.org\/a\/content\/btt-768x524.png 768w, https:\/\/8stars.org\/a\/content\/btt-1536x1048.png 1536w, https:\/\/8stars.org\/a\/content\/btt-2048x1397.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h3>On a regular interval<\/h3>\n<p>There are numerous desktop apps that can perform cron-like duties. <a href=\"https:\/\/www.peterborgapps.com\/lingon\/\">Lingon<\/a>, <a href=\"https:\/\/www.stairways.com\/main\/\">Keyboard Maestro<\/a>, and the aforementioned BetterTouchTool. Or you can run with the big dogs and create a <code>launchd<\/code> agent, and if you&#8217;re doing that, you can tell me all the mistakes I&#8217;ve made here.<\/p>\n<p>I&#8217;m already using BTT, so I&#8217;m using that here too. I&#8217;ve set my interval to every five minutes.<\/p>\n<ul>\n<li>Again in the <strong>Automations &amp; Named &amp; Other Triggers<\/strong> tab, create a new <strong>Repeating or Time Based Trigger<\/strong> and set your interval to whatever you want. BTT measures everything in seconds, so I use 300.<\/li>\n<li>The action taken is identical to the previous one: executing the script.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Once upon a time there was a Mac utility called Mail Badger that would add extra badges to the Mail.app dock icon to show the unread count in other mailboxes of your choosing. I relied on this a lot&#8211;I filter my incoming mail aggressively, and there are some high-priority mailboxes that aren&#8217;t my inbox, so [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"webmentions_disabled_pings":false,"webmentions_disabled":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"federated","footnotes":""},"categories":[23],"tags":[],"class_list":["post-4618","post","type-post","status-publish","format-standard","hentry","category-technology"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/8stars.org\/a\/wp-json\/wp\/v2\/posts\/4618","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/8stars.org\/a\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/8stars.org\/a\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/8stars.org\/a\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/8stars.org\/a\/wp-json\/wp\/v2\/comments?post=4618"}],"version-history":[{"count":5,"href":"https:\/\/8stars.org\/a\/wp-json\/wp\/v2\/posts\/4618\/revisions"}],"predecessor-version":[{"id":4628,"href":"https:\/\/8stars.org\/a\/wp-json\/wp\/v2\/posts\/4618\/revisions\/4628"}],"wp:attachment":[{"href":"https:\/\/8stars.org\/a\/wp-json\/wp\/v2\/media?parent=4618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/8stars.org\/a\/wp-json\/wp\/v2\/categories?post=4618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/8stars.org\/a\/wp-json\/wp\/v2\/tags?post=4618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}