Messages, on message received

great - you’ve changed a few things, you consider this to be a correct/better version?

Stefan, i get this error from your latest version:

Can’t get text 2 thru -1 of “0”

here we go again - this is the script that I have now, it doesn’t work:

delay 1

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 "sjungarna - kontakter" or name is "sjungarna - beställare" or name is "sjungarna - beställare, förfallna")
			--set groupMemberPhoneList to value of phones of people of group "sjungarna - kontakter"
			set phoneNumbers to {}
			repeat with aPerson in groupMemberPhoneList
				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 tell
		if theBuddy's name is in phoneNumbers then
			say "sent, yes"
			tell application "FileMaker Pro Advanced"
				tell database "sjungarna"
					tell table "SETUP"
						set field "imessage_sent_theMessage" to (theMessage as string)
						set field "imessage_sent_theBuddy" to (theBuddy's name as string)
					end tell
					delay 0.5
					--do script "skapa aktivitet från utgående iMessage"
					delay 0.5
					tell table "SETUP"
						set field "imessage_sent_theMessage" to ""
						set field "imessage_sent_theBuddy" to ""
					end tell
				end tell
			end tell
		else
			say "sent, no"
			
		end if
		
		--detta nedan är till för när man skickar iMessage från FileMaker:
		tell application "FileMaker Pro Advanced"
			tell database "sjungarna"
				tell table "SETUP"
				end tell
				delay 0.5
				--detta FM-script utför endast saker om FM-variabler är på rätt sätt:
				--do script "iMessage - on message sent från Messages.app"
			end tell
		end tell
		
	end message sent
	
end using terms from

BUT if I change this:

set groupMemberPhoneList to value of phones of people of (every group whose name is "sjungarna - kontakter" or name is "sjungarna - beställare" or name is "sjungarna - beställare, förfallna")

into this, it works:

--set groupMemberPhoneList to value of phones of people of group "sjungarna - kontakter"

I don’t really know what happened before but this is the result I keep getting now.

The error code from Messages.app is:
Event: Message Sent
File: on message sent NY .scpt
Error: Can’t make text 2 thru -1 of item 1 of item 1 of {{{“0709123456”}, {“0709654321”},}}} into type Unicode text.

Hello.

I can only guess, as I don’t have that app yet.

Maybe your filterclause is to complex, then you can concatenate the different results with the & operator.

set grouplist to filter-like you have it now.

set grouplist to grouplist & filter after first or

set grouplist to grouplist & filter after second or.

The runtime error, or the result from the error has to do with the phonenumbers, and not the filter clause

But try to change the line:

set end of phoneNumbers to "+46" & (text 2 thru -1 of aPhone)

into this

set end of phoneNumbers to "+46" & (text 2 thru -1 of (contents of aPhone))

I hope that works. But I am not sure whether it is problems with the coercion, or it is so that you have several items in “aPhone”.

thanks, but no, doesn’t work…

Hello please insert

log "" & contents of aPhone

under this statement

repeat with aPhone in aPerson

And please post what it looks like. Both for a person having just one number, and a person having two or more numbers.

It also wouldn’t harm to change the line:

repeat with aPhone in aPerson

into

repeat with aPhone in contents of aPerson

i don’t know what that does - the displayed error i Messages.app does look the same.
A person with only one number and one with two numbers - I don’t get what you mean here, sorry…

It is more about Contacts, than Messages.

I want you to log the content of aPhone of aPerson, both for a person with one phone-number, and one with two, since I suspect, what was just a text field on that level, is turned into a list, with the phone-numbers inside.

(Please do look away from the coalescing of the different filters for now.)

obviously you don’t use exactly my script.
Mine has 3 repeat loops, first the group, then person, then phone number

It wouldn’t harm, but it’s also useless.
Explicit dereferencing of a list item is only necessary in case you’re going to “write” the value into something

Hello.

What Stefan said above may explain a lot of it.

I’ll be back in a few, in this post, for coalescing your filters.

That makes sense, but I wasn’t sure of what was going on, since I missed the inner repeat loop for phone.

set groupMemberPhoneList to value of people of (every group whose name is "sjungarna - kontakter")

set tmpGroupMemberList to value of people of (every group whose name is "sjungarna - beställare")
 
repeat with itm in tmpGroupMemberlist
	if itm is not in groupMemberPhoneList then 
		set end of groupMemberPhoneList to (contents of itm)
	end if 
end
 
set tmpGroupMemberList to value of people of (every group whose name is "sjungarna - beställare, förfallna")
repeat with itm in tmpGroupMemberlist
	if itm is not in groupMemberPhoneList then 
		set end of groupMemberPhoneList to (contents of itm)
	end if 
endset tmpGroupMemberList to missing value

I now have persons here, and not phones, since reading Stefan’s script I think that is what you should use.

Stefan,

I tried it and it gave me error which I wrote about in post #22 in this thread.

I guess one of your contacts has a phone number containing only one zero

Hello ToBeJazz

If you look at your GroupMemberList, you’ll see that you have just entered phone-numbers into it,
not Persons.

major confusion here right now…

I’ve been testing all day to send iMessage from Messages.app to my own mobile number and I have tested to Display Dialog to find out that “theBuddy’s name” is my mobile phone number like this: “+46709251840”
This number has been a part of the Contacts.app group that I’m referring to in the
“set groupMemberPhoneList to value of phones of people of group “XXX””

Just now I tested to send an iMessage to another phone number that is also in the Contacts.app group but it fails to work. When I check: display dialog (theBuddy’s name as string) - it says “missing value”.
The number is connected to iMessage and it is in Contacts.app, and like I said, it’s in the Contacts.app group, but it still doesn’t work.

Then I tried to remove my first mobile number, that one that works, from the Contacts.app group and the script work anyway, which is strange. So the “set groupMemberPhoneList” is not “active”…

so, pröblems indeed

Hello.

In contacts you have people that are members of different groups.

Those people have one or more phone numbers.

That is why Stefan’s way of getting at the phone-numbers is the right one.

That is also how you’ll have to create the GroupMember list. (but there should be a check there if the person is in the list already, I’ll go back and edit that part in post #31

Edit

I have assembled Stefan’s script with yours, please try this:

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 "sjungarna - kontakter" or name is "sjungarna - beställare" or name is "sjungarna - beställare, förfallna")

           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
							if  (class of aPhone is text) and ((length of aPhone's text) > 1 ) then 
                           		set end of phoneNumbers to "+46" & (text 2 thru -1 of aPhone)
							end if 
                       end if
                   end repeat
               end repeat
           end repeat
       end tell
       if theBuddy's name is in phoneNumbers then
           say "sent, yes"
           tell application "FileMaker Pro Advanced"
               tell database "sjungarna"
                   tell table "SETUP"
                       set field "imessage_sent_theMessage" to (theMessage as string)
                       set field "imessage_sent_theBuddy" to (theBuddy's name as string)
                   end tell
                   delay 0.5
                   --do script "skapa aktivitet från utgående iMessage"
                   delay 0.5
                   tell table "SETUP"
                       set field "imessage_sent_theMessage" to ""
                       set field "imessage_sent_theBuddy" to ""
                   end tell
               end tell
           end tell
       else
           say "sent, no"
           
       end if
       
       --detta nedan är till för när man skickar iMessage från FileMaker:
       tell application "FileMaker Pro Advanced"
           tell database "sjungarna"
               tell table "SETUP"
               end tell
               delay 0.5
               --detta FM-script utför endast saker om FM-variabler är på rätt sätt:
               --do script "iMessage - on message sent från Messages.app"
           end tell
       end tell
       
   end message sent
   
end using terms from

thanks McUsrII, more moro today:)

Stefan, I went back to your script in post#20, and (of course) it doesn’t give me any error now.
I signed up for a new iCloud account and now I have only two contacts in Contacts.app which makes troubleshooting easier.

Here’s the basic script I’m trying. The problem now is that even though I have my two contacts in the groups I get a NO. I did set up a display dialog (phoneNumbers as string) - and there I can see the two phone numbers so everything seems to be OK, but I get a NO anyhow.

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 "sjungarna - kontakter" or name is "sjungarna - beställare")
			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

EDIT:
When I put “display dialog (theBuddy’s name as string)” (just before: if theBuddy’s name is in phoneNumbers then) - it says “missing value”.
Should it be like that?

I’m rephrasing my problem here.

Here’s the script I’m using:

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 "sjungarna - kontakter" or name is "sjungarna - beställare" or name is "sjungarna - beställare, förfallna")
			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

What is not working is this line:

if theBuddy's name is in phoneNumbers then

Even though the phone number I’m sending to is part of one the Contacts.app groups there’s no match, the script says “NO”.

When I insert this line:

display dialog (phoneNumbers as string)

The dialog shows this: +46709123456+46709987654
These are the two numbers in the Contacts.app groups

When I insert this line:

	display dialog (theBuddy's name as string)

It says “missing value” - could this point at the problem?..
To me, even though I don’t know this subject at all, it should say “+46709123456”, and that is why the script doesn’t work.

Is that correct Stefan?

One more thing:
My script for On Message Received has this line in the script:

on message received theMessage from theBuddy

When I write

display dialog (theBuddy's handle as string)

It gets the phone number

When I write

display dialog (theBuddy's name as string)

It gets the name of the Contact (in Contacts.app).

My On Message Sent script has this line in the beginning:

on message sent theMessage for theBuddy

It seems that it doesn’t get the phone number with saying “for theBuddy” since the line

if theBuddy's name is in phoneNumbers then

doesn’t work and because

display dialog (theBuddy's handle as string)

says “missing value”


So maybe the wording should be another than “on message sent theMessage for theBuddy” - and the script would work?..

Here’s something I finally got to work:

using terms from application "Messages"
	
	on message sent theMessage for theChat
		set ServiceAndHandle to (get id of theChat)
		set handleOnly to (text 12 thru -1 of ServiceAndHandle)
		tell application "Contacts"
			set groupMemberPhoneList to value of phones of people of (every group whose name is "sjungarna - kontakter" or name is "sjungarna - beställare" or name is "sjungarna - beställare, förfallna")
			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 handleOnly is in phoneNumbers then
			say "yes"
		else
			say "no"
		end if
	end message sent
	
end using terms from

Now I wonder if this code can be written in an easier way.
The ID (of theChat) looks like this “iMessage;-;+46709123456”
And to extract the phone number out of this I use “text 12 thru -1”
Probably not the best way to do it?