Mac Messages – check if recipient has Apple ID

Hello fellow scripters,

I’ve got a list of emails and phone numbers and would like to filter them to remove the ones that cannot receive a scripted message using Mac Messages. To manually test a recipient, I would enter the number or email into the “To:” field in Messages and it will go blue if it’s linked to an Apple ID, otherwise it will be coloured green.

The problem is, I have 15,000 recipients with multiple phone numbers and email addresses! So it would be easier to do this programmatically if possible.

I’ve tried this subroutine:


on iMessageCheck(MessengerContact)
	tell application "Messages"
		set targetService to (1st service whose service type = iMessage)
		try
			set targetBuddy to buddy MessengerContact of targetService
			set BuddyServiceType to service type of service of targetBuddy
			set BuddyStatus to status of service of targetBuddy
		on error ErrorMessage
			return ErrorMessage
		end try
		return {BuddyServiceType, BuddyStatus}
	end tell
end iMessageCheck

but it seems a “buddy” is only someone that I have communicated previously with using the Messages app. If the MessengerContact is a new number or email, this routine always returns an error.

Best regards,
David

Hi KniazidisR,

I liked your first answer, did you delete it? It was about having the buddies already in the Contacts app. I made the following test that goes through all my Contacts and creates two lists – contacts linked to an Apple ID, and contacts that are not. The results seem to be correct and I double checked that not all the positive results have existing conversations in the Messages app.


set ServiceTypeAndStatus to {}
set ServiceNames to {}
set ServiceNumbers to {}
set ServiceErrorNumbers to {}
set ServiceErrorNames to {}

tell application "Contacts"
	set AllContacts to every person
	repeat with i from 1 to (count AllContacts)
		set ThisContact to item i of AllContacts
		set PhonesList to value of every phone of ThisContact
		set ThisName to name of ThisContact
		repeat with j from 1 to (count PhonesList)
			set ThisNumber to item j of PhonesList
			set IMC to my iMessageCheck(ThisNumber)
			if IMC is not false then
				set the end of ServiceTypeAndStatus to IMC
				set the end of ServiceNames to ThisName
				set the end of ServiceNumbers to ThisNumber
				exit repeat
			else
				set the end of ServiceErrorNumbers to ThisNumber
				set the end of ServiceErrorNames to ThisName
			end if
		end repeat
	end repeat
end tell

on iMessageCheck(MessengerContact)
	tell application "Messages"
		set targetService to (1st service whose service type = iMessage)
		try
			set targetBuddy to buddy MessengerContact of targetService
			set BuddyServiceType to service type of service of targetBuddy
			set BuddyStatus to status of service of targetBuddy
		on error
			return false
		end try
		return {BuddyServiceType, BuddyStatus}
	end tell
end iMessageCheck

Thanks :slight_smile: It’s not a buddy existence check but a check of 15,000 records to see which ones are available on iMessage. It looks like I will have to add them to the Contacts app first to make each one a buddy, before iMessage can tell me. After I make the list I will delete them from Contacts again.

Great idea but it didn’t actually work. I entered a bunch of new contacts into the Contacts app but they still did not show up as Buddies in the Messages app.

I had another idea to get the script to use keystroke commands to paste the new contact in as a new message in the Messages app, then using regular AppleScript, the Messages app was able to return info about that Buddy. That info however, did not include whether the contact is registered for iMessages (as indicated by the name being blue in the “To:” field). I would have thought it was the value of “status” for that Buddy but this always comes back as “unknown”.

I even tried using UI Browser (after pasting the name) to click the arrow next to the name of the contact inside the “To:” field, to activate the contextual menu. The items in this menu vary depending on whether or not the contact is registered for iMessage. For example, if it is registered, the menu option of “FaceTime Video” becomes available. If the script could get a list of these options, it would know whether this contact was registered with iMessage. Sadly however, this list does not seem to be accessible to System Events because of a mismatch in the UI hierarchy.

Common sense tells me to give up now, but surely there must be a way…

To summarise: I’m looking for a way to tell if a given email address or phone number is registered for iMessage as indicated by it turning blue when manually entered as a new recipient of a message in the Messages app.

That code will check if the email or phone is an existing buddy but that’s not what I need. As I’ve found out, a Buddy is someone that I have previously messaged using the Messages app. I need to find out whether any new email or phone number is registered with iMessage.