DEV Community

Great Work
Great Work

Posted on Originally published at multimeterapp.com

Show a number from a file in your menu bar

This guide first appeared on multimeterapp.com. Multimeter is a Mac menu bar app that turns any number into a live meter.

A File meter shows the number in a file and updates the moment the file changes. Anything that can write a file can drive it: a cron job, a build tool, a script in any language. Works in every version of Multimeter.

Add the meter

  1. Create the file first, so you can pick it: echo 0 > ~/.meters/inbox (make the folder with mkdir -p ~/.meters).
  2. Choose Add Meter…, pick File and choose the file.
  3. Add a key path if it’s JSON.

How it watches

  • Multimeter is told by macOS when the file is written, so updates are immediate. It also re-reads every 30 seconds.
  • Editors and scripts that replace the file (write a new one, then rename) are followed to the new file.
  • Up to 256 KB is read.

What the file can contain

Web addresses, files and scripts all read the same two formats.

A plain number, or any text with a number in it. Multimeter takes the first number it finds, and drops thousands separators between digits, so 1,284 stars reads as 1284.

JSON. Either a bare number, or an object with value and any of these extras:

meter.json

{
  "value": 38,
  "min": 0,
  "max": 50,
  "label": "DEPLOY",
  "unit": "today",
  "formatted": "38 of 50"
}
Enter fullscreen mode Exit fullscreen mode

Pointing at bigger JSON you don’t control, like an API response? Give the meter a key path. Dots walk into objects and numbers pick array items: data.items.0.count. Numbers inside strings work too ("67012.45"), and true/false count as 1 and 0.

Without min and max the range is 0 to 100, so for bigger numbers set a range in Customize This Meter… or send max.

Examples

Write it from anything:

Terminal or a script

echo 12 > ~/.meters/inbox
Enter fullscreen mode Exit fullscreen mode

Write it safely from a job that might be read mid-write (write a temp file, then move it into place):

safe write

echo '{"value": 12, "label": "INBOX"}' > ~/.meters/inbox.tmp && mv ~/.meters/inbox.tmp ~/.meters/inbox
Enter fullscreen mode Exit fullscreen mode

From Python:

Python

from pathlib import Path
Path.home().joinpath(".meters/queue").write_text('{"value": 17, "max": 100}')
Enter fullscreen mode Exit fullscreen mode

Check it worked

Change the number in the file. The meter should change within a second.

Troubleshooting

What the meter says, why, and what to do

Symptom Cause Fix
Can’t read the file It was deleted or moved, or the folder isn’t readable. Check the path, then pick the file again in Customize This Meter….
No number found in the file The file is empty or the key path doesn’t match. Look at it with cat; make sure your writer doesn’t leave it empty between writes (use the temp-file-then-move pattern).

Next

Top comments (0)