Apple Intelligence is now available on iPhone, iPad, and Mac!

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Question about AppleScript to automate messages

Hi, I'm trying to modify the following code that posted by @Camelot to the community with no luck with my current knowledge.


Instead of fixed message, I'm trying to add the following line to the code so the script will read column c (or multiple columns) in excel that contain the actual message however I'm not able to return the value and add this to the message body. Also I'm not able to figure out how to use iMessage service only.


Thank you for your advise


set _msg1 to value of cell ("C" & id)
return {name:_name, number:_number, message:_msg1}

send "Hello " & name of member & ", " & message to participant (number of member) of account "iMessage"



Original Code:


on get_member(id)
	tell application "Microsoft Excel"
		-- assumes column 1 for the name and column 2 for the phone number
		set _name to value of cell ("A" & id)
		set _number to value of cell ("B" & id)
		
		return {name:_name, number:_number}
	end tell
end get_member


on send_message_to(member)
	
	tell application "Messages"
		try
			send "Hello, " & name of member & ", This is text" to participant (number of member) of account "SMS"
		end try
	end tell
	
end send_message_to


set i to 2
repeat
	-- go get the data for the member
	set _member to get_member(i)
	if name of _member = "" then
		-- break on the first empty/missing name
		exit repeat
	end if
	
	send_message_to(_member)
	-- increment the counter
	set i to i + 1
end repeat

MacBook Air

Posted on Apr 5, 2021 7:01 PM

Reply
Question marked as Top-ranking reply

Posted on Apr 6, 2021 10:24 AM

Since I wrote the original script, I guess it falls to me to update it :)


> Instead of fixed message, I'm trying to add the following line to the code so the script will read column c (or multiple columns) in excel that contain the actual message however I'm not able to return the value and add this to the message body.


The way the original script works is that there is one handler, get_member() that extracts the data from Excel.

You just need to expand this handler with any other commands to get the additional data.

In this case since you also want the content in column C, add something like:


		set _messageText to value of cell ("C" & id)


etc., for as many cells as you need - just create a variable name and the appropriate column reference.


Once you have this, you just need to include it in the data that the handler returns to the calling script:


		return {name:_name, number:_number, msgText:_messageText}


Now the script will pass back three pieces of data instead of the original two.


Now the second handler that actually sends the message, send_message_to() can reference the additional values since they're passed into the handler in the member variable:


	tell application "Messages"
		try
			send ("Hello, " & name of member & ", " & msgText of member) to participant (number of member) of account "SMS"
		end try
	end tell


> Also I'm not able to figure out how to use iMessage service only.


Do you mean you only want to use iMessage and not SMS. That's a matter of changing the 'account' parameter of the send command to use another account.


If you don't know the names of the accounts you can use, run the following script:


tell application "Messages"
	get description of every account
end tell


now you'll see a list of the names of configured accounts ,Just substitute the one you want to use.


2 replies
Question marked as Top-ranking reply

Apr 6, 2021 10:24 AM in response to kuzeyguldal

Since I wrote the original script, I guess it falls to me to update it :)


> Instead of fixed message, I'm trying to add the following line to the code so the script will read column c (or multiple columns) in excel that contain the actual message however I'm not able to return the value and add this to the message body.


The way the original script works is that there is one handler, get_member() that extracts the data from Excel.

You just need to expand this handler with any other commands to get the additional data.

In this case since you also want the content in column C, add something like:


		set _messageText to value of cell ("C" & id)


etc., for as many cells as you need - just create a variable name and the appropriate column reference.


Once you have this, you just need to include it in the data that the handler returns to the calling script:


		return {name:_name, number:_number, msgText:_messageText}


Now the script will pass back three pieces of data instead of the original two.


Now the second handler that actually sends the message, send_message_to() can reference the additional values since they're passed into the handler in the member variable:


	tell application "Messages"
		try
			send ("Hello, " & name of member & ", " & msgText of member) to participant (number of member) of account "SMS"
		end try
	end tell


> Also I'm not able to figure out how to use iMessage service only.


Do you mean you only want to use iMessage and not SMS. That's a matter of changing the 'account' parameter of the send command to use another account.


If you don't know the names of the accounts you can use, run the following script:


tell application "Messages"
	get description of every account
end tell


now you'll see a list of the names of configured accounts ,Just substitute the one you want to use.


Question about AppleScript to automate messages

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.