Messages, on message received

Hi,

I would like to have a script that will run when I receive an iMessage from a person who is part of a specific group in the Contacts.app
I will set it up as an alert for the event Message Received in the preferences for Messages.app
Anyone?

at this point I have this script:

using terms from application "Messages"
	
	on message received theMessage from theBuddy for theChat
		tell application "Contacts"
			if (theBuddy's name as string) is in group "Tobias iCloud-kontakter" then
				say "hello"
			end if
		end tell
	end message received
	
end using terms from

I’m not surprised it doesn’t work since I have very limited skills on AppleScript.
The error code is:
An error occurred while executing an AppleScript event handler.

Event: Message Received
File: on message received TEST 1.scpt
Error: Can’t make «class azf5» ID “A93B8CAC-5A98-41CD-9FA0-47C35B89687A:ABGroup” of application “Contacts” into type list, record or text.

Hi,

the error message says, that a group object could not be coerced to list, record or text.
You have to retrieve the names of all members of the group


using terms from application "Messages"
	
	on message received theMessage from theBuddy for theChat
		tell application "Contacts" to set groupMemberNames to name of people of group "Tobias iCloud-kontakter"
		if theBuddy's name is in groupMemberNames then
			say "hello"
		end if
	end message received
	
end using terms from


MANY thanks to you Stefan!

next thing is that I want to connect the message received (to Messages.app) to my FileMaker database - either just Set Field or Do Script. I want to transfer the text content of the message into the FileMaker database.

I tried this but it doesn’t work:

using terms from application "Messages"
	
	on message received theMessage from theBuddy for theChat
		tell application "Contacts" to set groupMemberNames to name of people of group "Tobias iCloud-kontakter"
		if theBuddy's name is in groupMemberNames then
			say "yes"
			tell application "FileMaker Pro Advanced"
				tell database "sjungarna"
					tell table "SETUP"
						set field "applescript_test" to theMessage
					end tell
				end tell
			end tell
		else
			say "no"
		end if
	end message received
	
end using terms from

oh, I just tried this:

using terms from application "Messages"
	
	on message received theMessage from theBuddy for theChat
		tell application "Contacts" to set groupMemberNames to name of people of group "Tobias iCloud-kontakter"
		if theBuddy's name is in groupMemberNames then
			say "yes"
			tell application "FileMaker Pro Advanced"
				tell database "sjungarna"
					tell table "SETUP"
						set field "applescript_test" to (theMessage as string)
					end tell
				end tell
			end tell
		else
			say "no"
		end if
	end message received
	
end using terms from

And it works - is this the best way to do it?

also, I would like to set up a corresponding script for On Message Sent - here’s what I have so far:

using terms from application "Messages"
	
	on message sent theMessage for theBuddy
		tell application "Contacts" to set groupMemberNames to phone numbers of people of group "Tobias iCloud-kontakter"
		
		if theBuddy's name is in groupMemberNames then
			say "yes"
		else
			say "no"
		end if
		
	end message sent
	
end using terms from

It doesn’t work.
The thing is that “theBuddy” is, in my case, a mobile phone number (“+46709123456”) so it has to look into Contacts.app for phone numbers instead of names.

the error code is:
An error occurred while executing an AppleScript event handler.
Event: Message Sent
File: on message sent NY test.scpt
Error: Contacts got an error: Can’t get every number.

You need a loop to gather the phone numbers


tell application "Contacts"
	set groupMemberPhoneList to value of phones of people of group "Tobias iCloud-kontakter"
	set phoneNumbers to {}
	repeat with aPerson in groupMemberPhoneList
		repeat with aPhone in aPerson
			set end of phoneNumbers to contents of aPhone
		end repeat
	end repeat
end tell
if theBuddy's name is in phoneNumbers then
	-- .
end if

Thanks again Stefan.
I sometimes have mobile phone numbers in Contacts.app in the format +46709123456 but more often as 0709123456.
The format of the Buddy name is +46709123456, and the script can’t translate between the two formats.
Is there a way to adjust the script so that I can continue to have my phone numbers in the 0709123456 format and still have it work?

The script so far is:

using terms from application "Messages"
	
	on message sent theMessage for theBuddy
		tell application "Contacts"
			set groupMemberPhoneList to value of phones of people of group "Tobias iCloud-kontakter"
			set phoneNumbers to {}
			repeat with aPerson in groupMemberPhoneList
				repeat with aPhone in aPerson
					set end of phoneNumbers to contents of aPhone
				end repeat
			end repeat
		end tell
		if theBuddy's name is in phoneNumbers then
			say "yes"
		else
			say "no"
		end if
	end message sent
	
end using terms from

Hello.

Hopefully this works.

And .ski-Ã¥kning er moro nÃ¥ for tiden… :wink:

using terms from application "Messages"

on message sent theMessage for theBuddy
tell application "Contacts"
   set groupMemberPhoneList to value of phones of people of group "Tobias iCloud-kontakter"
   set phoneNumbers to {}
   repeat with aPerson in groupMemberPhoneList
       repeat with aPhone in aPerson
           tell contents of aPhone
               if it starts with "+" then
                   set end of phoneNumbers to it's text
               else
                   set toUse to text 2 thru -1 of it's text
                   set end of phoneNumbers to "+46" & toUse
               end if
           end tell
       end repeat
   end repeat
end tell
if theBuddy's name is in phoneNumbers then
   say "yes"
else
   say "no"
end if
end message sent

end using terms from

Edit

I changed it to just use “+”, so it won’t touch phonenumbers with other contry codes, say +“47”. :slight_smile:

Hey McUsrII,

Have just tried a little bit but the difference between the formats are not only that “+46” is a prefix, also when the prefix is there the first “0” is removed, these are the “same” numbers:
+46709123456
0709123456

Could you work that into the script as well?

more moro :wink:

EDIT: Confirmed, it doesn’t work since the “0” has to be removed…

Hello.

Ouch, I edited in the change in the script in post #10.

Det er lenge siden jeg ringte med svenske tjeier. :slight_smile:

when I put your corrected code from post#10 it says:

“Syntax Error
Expected expression but found unknown token.”

Could you check that as well?

Hello.

I think it should work now.

the dash of “1 is not recognized as minus sign

Edit: easier


repeat with aPhone in aPerson
	if aPhone starts with "+" then
		set end of phoneNumbers to contents of aPhone
	else
		set end of phoneNumbers to "+46" & (text 2 thru -1 of aPhone)
	end if
end repeat

the explicit dereferencing of the list item is only needed for equation test

Thank you for finding that one Stefan! :slight_smile:

I won’t write code in the editor window again, (or until I forget it.)

Hello.

It should really work now, even if it isn’t formatted properly,
since I can’t compile it, because I haven’t got Messages.

Hey, I got it working - thanks alot to you both for the help!

I found out I need multiple contacts groups instead of one

set groupMemberPhoneList to value of phones of people of group "Tobias iCloud-kontakter"

tried some alternatives incl. this one with no success:

set groupMemberPhoneList to (value of phones of people of group "test_1" and value of phones of people of group "test_2")

try


using terms from application "Messages"
	
	on message sent theMessage for theBuddy
		tell application "Contacts"
			set groupMemberPhoneList to value of phones of people of (every group whose name is "test_1" or name is "test_2")
			set phoneNumbers to {}
			repeat with aGroup in groupMemberPhoneList
				repeat with aPerson in aGroup
					repeat with aPhone in aPerson
						if aPhone starts with "+" then
							set end of phoneNumbers to contents of aPhone
						else
							set end of phoneNumbers to "+46" & (text 2 thru -1 of aPhone)
						end if
					end repeat
				end repeat
			end repeat
		end tell
		if theBuddy's name is in phoneNumbers then
			say "yes"
		else
			say "no"
		end if
	end message sent
	
end using terms from