Attention: This page contains partner and advertising links. Therefore this page is to be understood entirety as an advertisement!
I was always annoyed that even my iPhone is able to automatically copy sms codes like the one you get from WhatsApp or Google in the app you are currently using, but my MacBook not. So I wrote a small AppleScript to fix this issue and improve my daily workflow. It makes use of the simple feature that iOS can even sync regular sms with the iMessage on macOS and the fact that all messages are stored in a SQLite database located at ~/Library/Messages/chat.db
that can be easily accessed.
The AppleScript is supposed to directly type any code or tan found in the last message in my iMessage app when I press fn + p . To register this Shortcut, I use BetterTouchTool. You may also have to grant BetterTouchTool and the Script Editor"Full Disk Access" in your system preferences in the "Security & Privacy" area since otherwise the apps are not allowed to open the SQLite database. The iMessage app itself can stay closed and is not required to make this script work.
For now my script only supports codes from Fidor and Google. You have to extend this script on your own if you want to support more message types and formats by adding more conditions after the set tan to missing value
block.
So this is my final apple script. You can test it by just adding it in the Script Editor. For a message like "mTAN ABCDEF für Überweisung von 1337,77 Euro an Konto XYZ" it will automatically type "ABCDEF".
# https://stackoverflow.com/questions/38041852/does-applescript-have-a-replace-function
on replaceChars(inputText, search, replace)
set AppleScript's text item delimiters to the search
set the itemList to every text item of inputText
set AppleScript's text item delimiters to the replace
set inputText to the itemList as string
set AppleScript's text item delimiters to ""
return inputText
end replaceChars
# https://macscripter.net/viewtopic.php?id=24473
to split(inputText, delimiter)
set AppleScript's text item delimiters to delimiter
set inputText to inputText's text items
set AppleScript's text item delimiters to {""}
return inputText
end split
#
# if the app has no full disc access, give full disc permissions in the sytem preferences
#
set latestMessageText to do shell script "sqlite3 ~/Library/Messages/chat.db 'SELECT text FROM message ORDER BY date DESC LIMIT 1'"
# Debug
# set latestMessageText to "Ihre mTAN: AAAAAA"
# set latestMessageText to "mTAN AAAAAA für Überweisung von 1337,00 Euro an Konto FooBar"
# set latestMessageText to "G-13377 ist Ihr Google-Bestätigungscode."
set tan to missing value
if {latestMessageText starts with "Ihre mTAN: "} then
set tan to replaceChars(latestMessageText, "Ihre mTAN: ", "")
else if {latestMessageText starts with "mTAN "} then
set splittedText to split(latestMessageText, " ")
set tan to second item of splittedText
else if {latestMessageText ends with " ist Ihr Google-Bestätigungscode."} then
set tan to replaceChars(latestMessageText, " ist Ihr Google-Bestätigungscode.", "")
set tan to replaceChars(tan, "G-", "")
end if
if tan is not equal to missing value then
tell application "System Events"
keystroke tan
end tell
# if you want to copy the tan instead
#set the clipboard to tan
end if