Auto-copy SMS verification codes from messages app

Would it be possible to program something in bettertouchtool so that when a message is received in the native messages app that contains the words "verification, code, pin", it will automatically copy that code to clipboard? I know safari on mac gives the option to autofill codes, but its not as efficient in chrome.

I don't think this is possible as everything iMessage related is encrypted and can't easily be accessed by third parts apps. The autofill only works because it is deeply integrated in the system.

1 Like

Is there any possible creative way this? E.g., display the message in menubar or HUD or floating menu?

This AppleScript extracts the code from the last incoming message and copies it to the clipboard. The code must be 4-6 characters long and the text (before the code) must contain Code, code, Pin or pin and immediately before the code there must be a colon or the word is or ist or lautet followed by a space.

Examples:

  • Your Apple Account code is: 561583. Do not share it with anyone.

  • PayPal: Your security-code is: 370626. Do not give this code to anyone.

  • Amazon: Your code is 458319, do not share it. You did not request it? Decline here: ...

  • Authorize the purchase of 0.0 EUR with your card ***5588. Enter the following code followed by your 3D-Secure code: 7654

  • Your pin is 458319.

  • PayPal: Ihr Sicherheitscode lautet: 686747. Geben Sie diesen Code nicht weiter.

  • Telekom Login: Ihr angeforderter BestƤtigungscode lautet 534703. Bitte geben Sie diesen in das Eingabefeld ein. Ihre Telekom

use framework "Foundation"
use scripting additions

property NSData : a reference to current application's NSData
property NSArray : a reference to current application's NSArray
property NSString : a reference to current application's NSString
property NSUnarchiver : a reference to current application's NSUnarchiver
property NSRegularExpression : a reference to current application's NSRegularExpression

set hexString to do shell script "sqlite3 ~/Library/Messages/chat.db 'SELECT HEX(attributedBody) FROM message WHERE is_from_me = 0 ORDER BY date DESC LIMIT 1'"
set theData to (NSArray's arrayWithObject:(run script "«data rdat" & hexString & "»"))'s firstObject()'s |data|()
set {theMessage, theError} to (NSUnarchiver's alloc's initForReadingWithData:theData)'s decodeTopLevelObjectAndReturnError:(reference)
if theError is missing value then
	set theString to theMessage's |string|
	set thePattern to "(?:(?:Code|code|Pin|pin).*(?:\\:|is|ist|lautet)\\s)(\\d{4,6})"
	set theRegEx to NSRegularExpression's regularExpressionWithPattern:thePattern options:0 |error|:(missing value)
	set regExResults to theRegEx's matchesInString:theString options:0 range:{location:0, |length|:theString's |length|()}
	if regExResults's |count|() > 0 then
		set aMatch to regExResults's objectAtIndex:0
		if aMatch's numberOfRanges as integer > 1 then
			set theRange to (aMatch's rangeAtIndex:1) -- Group 1
			if theRange's |length| > 0 then
				set theResult to (theString's substringWithRange:theRange) as string
				set the clipboard to {text:(theResult as string), Unicode text:theResult}
				tell application "BetterTouchTool"
					display_notification "Code: " & theResult & " copied." with title "Verification Code"
				end tell
				return
			end if
		end if
	end if
end if
tell application "BetterTouchTool"
	display_notification "No code found." with title "Verification Code"
end tell

Use the code in a "Run AppleScript" action (e.g. as a keyboard shortcut).

Note: The regex pattern could probably be more optimized, but it works. It's not necessarily my thing... :wink:

2 Likes

oh that's interesting, then my info that the database is encrypted is wrong. I'll have a look, could make an interesting trigger.

1 Like

No. The database is not encrypted. Sample Script
If you want, you can also monitor the file via LaunchAgent and automate the whole process. However, many providers (e.g. banks) are moving away from sending SMS messages in favor of notifications and this database is unfortunately encrypted and scripts cannot easily access the content of the message. You can also find out more about this in the topic.

A nice trigger would perhaps be ā€œIncoming messageā€. Then everyone can do what they want with the message.

3 Likes

Would love for this to be a thing - either as an HUD overlay or a temporary display in the menubar!