Hi everyone, Is anyone aware of a script/app that can automatically copy the code from a 2FA text message on MacOS? On iOS, if you receive a code via text/iMessage, a horizontal menu appears with the code, and with an easy finger click, it will automatically populate the code field on the given app. Is there something similar on MacOS? Thanks!
… will automatically populate the code field on the given app
Maybe I’m missing something, but doesn’t this already automatically happen on MacOS too?
You probably need to have Text Message Forwarding enabled on your iPhone (Settings → Messages → Text Message Forwarding), and your Mac will need to be signed into the same AppleID account, but then you’re set.
My Mac is set up with the forwarding and I can see the message. Does it only work in Safari?
Following up on this discussion, I would like Applescript to capture the code from iMessage, copy it to the clipboard and insert the result into Claris’ Sign in for Claris ID to access FileMaker Pro. Currently, the Claris ID although sent to my cell is never captured by the Claris Sign In verification code field.
I would like to replace my current practice of switching applications to iMessages in OSX, finding the last message, copying the six digit verification code to clipboard and then pasting the code into Claris’ Sign In on its “Sign in With Claris ID” page.
I do this by capturing the macOS system notification. I have the script assigned to Cmd-Opt-P. It has worked very reliably for me for about ~5 years.
The current version runs on Sonoma (and Ventura, I believe). I believe Monterey required a slightly different specifier for the notification UI element.
-- For security purposes, execute all potentially-sensitive code within a main handler.
return main()
-- Define the main handler.
to main()
set {notification_message:s} to getUserNotificationInfo()
set s_words to extractAllIntegers(s)
set s_match to ""
repeat with i from 1 to length of s_words
set s_word to item i of s_words
if length of s_word > 4 then
set s_match to s_word
exit repeat
end if
end repeat
if s_match is "" then
display dialog "Failed to find SMS OTP in string:" with title (my name as string) with icon caution buttons {"OK"} default button 1
else
setClipboard(s_match)
end if
return
end main
### HANDLERS ###
to getUserNotificationInfo()
tell application "System Events"
tell process "NotificationCenter"
try
set notification_window to first window
on error
return missing value
end try
set static_texts to value of static texts of group 1 of UI element 1 of scroll area 1 of group 1 of notification_window
set static_texts_length to length of static_texts
set application_name to item 1 of static_texts
set notification_message to last item of static_texts
if static_texts_length > 3 then
set notification_title to item 3 of static_texts
else
set notification_title to missing value
end if
if static_texts_length > 4 then
set notification_subtitle to item 4 of static_texts
else
set notification_subtitle to missing value
end if
end tell
end tell
return {application_name:application_name, notification_title:notification_title, notification_subtitle:notification_subtitle, notification_message:notification_message}
end getUserNotificationInfo
to extractAllIntegers(s)
set L to {}
set previous_character_is_digit to none
repeat with char in s
set char to contents of char
set current_character_is_digit to char is in "0123456789"
if current_character_is_digit then
if current_character_is_digit is previous_character_is_digit then
-- Add to current substring.
set last item of L to last item of L & char
else
-- Start new substring.
set the end of L to char
end if
end if
set previous_character_is_digit to current_character_is_digit
end repeat
return L
end extractAllIntegers
to setClipboard(s)
do shell script "printf -- %s " & quoted form of s & " | LANG=$(defaults read NSGlobalDomain AppleLocale).UTF-8 pbcopy" without altering line endings
return
end setClipboard
Do note the following:
- You must run the script while the notification is still visible
- It relies on GUI scripting, so has the habit of breaking with major macOS updates
- It does not mark the message as read
tree_frog, thanks for your reply.
The script you submitted however errs on my mac running OS 14.2.1 .
set static_texts to value of static texts of group 1 of UI element 1 of scroll area 1 of group 1 of notification_window
--> System Events got an error: Can’t get scroll area 1 of group 1 of window 1 of application process "NotificationCenter". Invalid index.
Is there another way to script System Notifications to gather the data contained in that application’s most recent window?
It works for me on macOS 14.2.1.
You can check the following:
- The script must be run while the notification is displayed
- Since it’s UI scripting, whatever’s running the script must have Accessibility access granted through System Settings
For reasons that are unclear to me, I cannot get the script to work.
Notifications show temporarily on my Mac, but close as soon as I attempt to run the script in Script Debugger.
Script Debugger in System Settings, has accessibility to control both the computer and the full disk.
What else can I change or what script do I need to write to allow Applescript the ability to capture notifications, such as six digit codes sent by web sources?
You can add the following 2 lines to the start of the main() handler definition for easy debugging:
display notification "Testing OTP code with 123456."
delay 1
You can also try toggling the System Settings > Privacy & Security > Accessibilty > Script Debugger permission, and then re-launching Script Debugger in case it’s a permissions bug.
The script doesn’t do anything to close the notification. The OTP should be in your clipboard after the script is run.
Have you previously done anything that affects how long notifications are displayed by the system? You can check the following key (by default, this won’t exist).
defaults read com.apple.notificationcenterui bannerTime
Do you have updated version for 14.4?
When I try your script I get : error “The variable none is not defined.” number -2753 from “none”
I did set the accessibility and I am running while notification is present.
Thanks in advance!
macOS 14.4 hasn’t been released yet. The script is working normally on 14.3.1.
It’s possible there’s some weird kind of terminology clash (because of 14.4, or because of a scripting addition you have installed) with the following line:
set previous_character_is_digit to none
You could try changing this to:
set previous_character_is_digit to missing value
This is exactly why we built flowtext.io.
We got super frustrated with only being able to use iMessage + Safari for this on MacOS so this will automatically copy 2fa codes from iMessage to your clipboard for use in any browser/place you see fit!
Disclosure: It’s $5 for a lifetime license and I’m one of the developers.