get buddy Messages <> Contacts

I want to get the name of a person in Contacts.app (Address Book) that I’m sending an iMessage to from Messages.app (iChat). This is the script I’m trying, but it doesn’t do the trick.

using terms from application "Messages"
	
	on message sent theMessage for theChat
		display dialog (get buddy's name of theChat)
	end message sent
	
end using terms from

Hello.

Some general advice:

In AppleScript editor, there is something called a Library, under the windows menu of it.

When you open the Dictionary for Messages there, you can search for chat? or Message, and look at which one, that has buddy as a property.

Then you structure your code in accordance with what properties you want to extract.

I hope this helps on a general basis, and it is well meant!

Good luck!

Hi,

chat has a property participants which contains a list of buddy objects

Hi!

this script:

using terms from application "Messages"
	
	on message sent theMessage for theChat
		display dialog (get participants of theChat)
	end message sent
	
end using terms from

gives me this error code:
Error: Can’t make {«class pres» id “0E8CA222-66C6-4A72-B3BC-116FEA8A73A4:+46709123456”} into type string.

while this script:

using terms from application "Messages"
	
	on message sent theMessage for theChat
		display dialog (get id of theChat)
	end message sent
	
end using terms from

gives me this dialog data:
iMessage;-;+46709123456

Hello.

When you try to obtain information about “something” in AppleScript, it is pretty much like sending a database query.

So, you may have to refine the query in Applescript to get what you want.

Objects and properties in an application like Messages, are pretty much organized like a datamodel.

So and object, has properties, that may individually be viewed as objects, that has properties again, and so on.

It is a nice trick, to get familiar with the dictonary of an app, and try to understand how it all are joined together, it is also of great general value, because there is a lot of commonality in different apps, so when you have learned about one, then you know a whole lot about the others. This is of course most valid when it comes to the Standard Suites of a Dictionary that every app has. But there are still transferable knowledge and experience in the other Suites of a Dictionary as well.

In your code examples, you are really asking fro two different things, in the first you ask for the participants, which may be a kind of intermediary object, that contains a list or something with buddies, aka something that is to complex to be coerced into a string, so you’ll have to look at what a participant contains, and try to extract those values. (Try that.)

In the second example you ask for the id, which is simple enough to be coerced to a string.

A composite object, can in general not be coerced to a string, since it resembles a record.

as mentioned in my first post, the content of participants are buddy objects which cannot be coerced to string (what display dialog expects)

After I somehow managed to extract the phone number from the On Message Sent script with “get id of theChat” - what is left is to extract the full name (as it appears in the Contacts card) of the person I’m sending the iMessage to.
I’m hitting my head bloody on “get XXX of theChat” where XXX is handle, participants, name, full name etc. in all different combinations as read from the dictionary, but it doesn’t work. It’s totally obvious I’m faking my way through this, but I don’t have the time to really learn it right now. I just want this script to work, sorry.

Hello.

At least my experience with AppleScript has learned me that it is faster to learn it, than to fake it, when you start banging your head against the wall. I have never regretted that approach, but your approach I have regretted several times. :slight_smile: And I have never thought hindsightly when I took the time to learn it, that it was waisted time. It is not like you are going to study for dissertation, but say invest one hour, so you don’t have to get agonized, and bang your head in the wall for three, which IMHO is a bad experience. Of course, sometimes, you have to guess, but not before you are sure there are absolutely no references or sources available upon that you can leverage on.

As long as you need it, keep a copy of AppleScript Language Guide handy, (in PDF), so you can search it too. (Downloadable from developer.apple.com).

What StefanK told you, is that a chat object, has a particpants object, that has at least two buddy objects, which each has phonenumbers.

The below is not correct AppleScript, it just shows you how to get the phonenumbers out of a chat object, at least how I would have attacted the problem. This is how I would use what StefanK told you in a couple of posts above.

I’d look in the log pane to see how it executes, and where I might have to adjust, also on what is the result of the different operations.

set phoneList to {}
repeat with tBuddy in (get participants of theChat)
	repeat with aPhone in (get phonenumbers of tBuddy)
		set end of phoneList to aPhone
	end repeat
end repeat

By the way: you are so fastidious, I start to think I am dealing with a “closet-Norwegian”. :wink:

Here is a little “hacker-tool” for you, to learn how to address or see what is inside the different Messages objects
from within the log pane of AppleScript Editor.

This is just an example, probably not working, but make it working and then you can see the hierarchy, as you add objects to the left

Start with this:

set a to every property of of theChat

Then you can expand that to this:

set a to every property of participants of theChat

And further to this:

set a to every property of aBuddy in participants of theChat

You must of course have tell application “Messages” block around it.

I also think you should download a free trial copy of Script Debugger, since I think that may be of help to you.
(The sooner you get it, the more time you’ll save).

Thanks for your patience McUsrII. And hey I’m from Sweden!

Here’s something I tried:

using terms from application "Messages"
	
	on message sent theMessage for theChat
		
		set phoneList to {}
		repeat with tBuddy in (get participants of theChat)
			repeat with aPhone in (get phoneNumbers of tBuddy)
				set end of phoneList to aPhone
			end repeat
		end repeat
		
	end message sent
	
end using terms from

Result:
Error: Can’t make phoneNumbers of «class pres» id “0E8CA222-66C6-4A72-B3BC-116FEA8A73A4:+46709123456” into type reference.

Folks, please read the dictionary

There is no property/element phoneNumbers in the buddy class.
And even if there was any, according to Apple’s own naming convention it would be phone numbers

Hey Stefan, would you care to ease the pain by writing how this should be done - and I’ll send you a link to my version of Bach’s Die Kunst Der Fuge, Contrapunctus 4 ? :cool:

it depends on which property do you need.
This is a version creating three lists for handle, id and name.

The identification of the buddies in Messages is not consistent, because iMessage and AIM use different protocols


using terms from application "Messages"
	
	on message sent theMessage for theChat
		
		set nameList to {}
		set handleList to {}
		set idList to {}
		repeat with tBuddy in (get participants of theChat)
			set end of handleList to handle of tBuddy
			set end of idList to id of tBuddy
			set end of nameList to name of tBuddy
		end repeat
		
	end message sent
	
end using terms from

What can I say? THANKS - it’s looks so easy!

BTW here’s the link if you care for a listen:
http://tobiassjogren.com/mp3/BWV_1080_4.mp3

Neat, well done.

I wonder if there’s a way to extract data about whether or not a specific phone number or email address is connected to iMessage via applescript.

One can do it manually via going to Messages.app and New Message and then write the phone number or email address, and if it goes blue it’s connected to iMessage, and if it turns red it’s not - but I need to extract it automatically, if possible.

Maybe there’s a way to do this?

Is it possible to use the “on av chat started” event?

And maybe there’s another way to connect to Apples database to retrieve data about it as well?

as far as i know there is no way

Ok

is there a way for applescript to retrieve data about what color a certain spot on the computer screen at a certain point in time?
The script should be able to differentiate between red and blue.

I guess I first need a script to take screenshots where I can specify in pixels where on the screen the shot should be of (sorry for bad english). Then put it into Photoshop and analyse it there…

With this script I’m gathering phones of people, I found out that I want to gather emails as well since iMessages also can be sent to email addresses.

Stefan, or anyone else, how should I code to gather both phones and emails?
Should I set the groupMemberPhoneList to phones AND emails, and have the line “if handleList is in phoneNumbers then” untouched - or should I gather phones and emails in separate repeat loops and then write something like “if handleList is in phoneNumbers or if handleList is in emailAddresses then” ?

using terms from application "Messages"
	
	on message sent theMessage for theChat
		try
			set handleList to {}
			set nameList to {}
			repeat with theBuddy in (get participants of theChat)
				set end of handleList to handle of theBuddy
				set end of nameList to name of theBuddy
			end repeat
			if variabel is "" then
				tell application "Contacts"
					set groupMemberPhoneList to value of (phones whose label is "mobil") of people of (every group whose name is "sjungarna - kontakter" or name is "sjungarna - beställare" or name is "sjungarna - beställare, förfallna" or name is "sjungarna - beställare, betalda")
					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
					set theOrganization to (get organization of people whose name is (nameList as string))
				end tell
				if handleList is in phoneNumbers then
					--do something
				end if
			end if
		end try
	end message sent
	
end using terms from