A Better Way to Copy Two-Factor Codes on macOS

Uncategorized Dec 18, 2021

Back in June, I posted a completely un-serious post that described a ridiculous Rube Goldberg approach to grabbing two-factor authentication codes from your text messages on macOS using Keyboard Maestro (for those of us who don’t use Safari).

How dumb was it? Let’s just say that it relied on taking a screenshot of Notifcation Center and parsing the code out of the image.

A joke, yes, but also a fun distraction one evening.

To my surprise, very nice reader @ajorpheus provided a real solution in the comment section earlier today.

Their solution is to grab the most recent text from’s actual SQLite database and parse the token from that. Not only is this way, way faster – it’s much more error-proof as well.

Here’s the shell script they provided:

sqlite3 "$HOME/Library/Messages/chat.db"   "select text from message order by date desc limit 1" | perl -lpe "s/.* (\d+) .*/\1/g"

With that, it’s just a matter of dropping the script into a Keyboard Maestro macro like this:

Two-factor auth token Keyboard Maestro macro screenshot

Besides the script itself, all I added was a step to copy the extracted 2FA token to the system clipboard and another to display the token in a system notification so you can confirm the macro ran (and worked?) before pasting.

You can download the macro here.

Please note: The database is a protected file on your system. For Keyboard Maestro to access it, you’ll need to grant the app Full Disk Access in System Preferences → Security & Privacy → Privacy.

(Remember to scroll slowly through that list when looking for Keyboard Maestro. It’s been how many years and still none of the Privacy sections sort their lists alphabetically?)

And big thanks to @ajorpheus for coming up with a better way to turn a dumb idea into something useful and reliable.

1 Comment

Comments are closed.