Scripting iMessages

Hi all, so today i tried to create an Applescript that sends an iMessage from Selected Text but i cannot figure out how to access the right ID to which i want to send…

So e.g. i send the message to me, i pass my Name to the Applescript but i have stored 3 Numbers in my Adress book for me, one is home, the mobile Number etc…

But Messages.app takes always the first one, HOME resulting in an Error " This Person is not registered for iMessage…


set question to display dialog "Enter Buddy Name:" default answer ¬
	"Serge Sander" buttons {"Send", "Cancel"} default button 1
set answer to button returned of question
set Bud to text returned of question

tell application "Messages"
	
	set msg to ""
	set isAppRunning to false
	
	tell application "System Events"
		if (exists process "Messages") then
			set isAppRunning to true
		end if
	end tell
	
	if isAppRunning is false then
		activate
	end if
	set tmp to id of Bud
	set tmp2 to item 1 of tmp
	set tmp3 to sendto of buddy Bud
	--send Bud to buddy tmp2 of service 1
end tell


So the best solution would be to get all possible Numbers and to offer a Drop Down Box to select one, but Messages.app does not return the values, if i try to request something like “phone” it returns an error like “Cannot convert PHONE to type specific”, anyonne an idea how to fix this or any suggestion what values are available from Messages.app to check for availability?

thx for help

Model: iMac
Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)

Hi,

try something like this.

On my machine the identifier of the iMessage service is “E:” following by my Apple ID
I noticed that the iMessage buddies aren’t online permanently, so this might be a problem


property serviceName : "E:[myAppleID]"

set question to display dialog "Enter Buddy Name:" default answer ¬
	"Serge Sander" buttons {"Send", "Cancel"} default button 1
set Bud to text returned of question

set msg to "Hello !"

tell application "Messages"
	activate
	set iMessageService to service serviceName
	set buddyList to handle of buddies of iMessageService whose name is Bud
	set chosenBuddy to choose from list buddyList
	if chosenBuddy is false then return
	set buddyToSend to 1st buddy of iMessageService whose handle is (item 1 of chosenBuddy)
	send msg to buddyToSend
end tell

Nope sorry, this doesnt work, somehow it cannot perform the Service Name, i added my AppleID but i get an Error from Applescript Editor telling me: “Service Name couldnt be read” or something of this kind in gem an ^^

How do you obtain this Service Nae? Maybe mine is defined other than yours ^^

this is supposed to display the name of your iMessage service


tell application "Messages"
	set iMessageServiceName to name of 1st service whose name starts with "E:"
end tell
display dialog iMessageServiceName buttons {"OK"} default button "OK"


Wahoo thx, this worked, i needed to remove the [ ] brackets and voila it worked ^^

thx so far, one final Question, how can i combine the two Scripts to auto read the Service Name inside the Script? If i read the Service Name and try to store it in a variable i get the Error ž«class icsv»" Not allowed to set this value…

Can i combine the two parts into one to make this more universal?

thx a lot again

as the service name does probably not change,
just replace “E:[myAppleID]” in the property line with the literal name string (in double quotes)

Sorry wrone description from my Side:

If i want to share this Script, so noone needs to detect its own Apple ID , so the script does it for him, this should look like this:


tell application "Messages"
   set iMessageServiceName to name of 1st service whose name starts with "E:"
end tell

property serviceName :iMessageServiceName


But currently it tells me Variable iMessageService unknown or if i try to pass it to a global variable it returns the error cannot be set, not allowed


property serviceName : ""

tell application "Messages"
	set serviceName to name of 1st service whose name starts with "E:"
end tell