iMessage Apple Script

Posted on
Sun Oct 06, 2019 10:01 am
JaceJenkins offline
Posts: 51
Joined: Oct 16, 2015

iMessage Apple Script

Hello All,

This should be pretty simple. I had a generic apple script that read 2 indigo variables and created an imessage off of that.

Code: Select all
set varContact to value of variable "iMessage_Contact"
set varMessageBody to value of variable "iMessage_Body"

tell application "Messages"
   
   activate
   
   set theBuddy1 to buddy varContact
   set thisChat to make new text chat with properties {participants:{theBuddy1}}
   set varMessageBody to send varMessageBody to thisChat
   
end tell


Thanks for the help!

Posted on
Sun Oct 06, 2019 12:05 pm
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: iMessage Apple Script

Try the following:

Code: Select all
import applescript
pyContact = indigo.variables["iMessage_Contact"].value
pyBody = indigo.variables["iMessage_Body"].value

# First half of the AppleScript to handle the python variable value substitutions (could also define an AppleScript func and pass arguments)
script_source = '''
tell application "Messages"
   set asContact to "{}"
   set asBody to "{}"'''.format(pyContact, pyBody)

# Second half of the script to construct and send the chat message
script_source += '''
   set newChat to make new text chat with properties {participants:{buddy asContact}}
   send asBody to newChat
end tell'''

# Create and execute the AppleScript:
script = applescript.AppleScript(source=script_source)
script.run()

Image

Posted on
Sun Oct 06, 2019 6:36 pm
JaceJenkins offline
Posts: 51
Joined: Oct 16, 2015

Re: iMessage Apple Script

Thanks, it worked like a charm. I also edited my Group iMessage Script

Single Buddy iMessage
Code: Select all
import applescript
pyBuddy = indigo.variables["iMessage_Buddy1"].value
pyMessage = indigo.variables["iMessage_Message"].value

# First half of the AppleScript to handle the python variable value substitutions (could also define an AppleScript func and pass arguments)
script_source = '''

tell application "Messages"
   set asBuddy to "{}"
   set asMessage to "{}"

'''.format(pyBuddy, pyMessage)

# Second half of the script to construct and send the chat message
script_source += '''
   set newChat to make new text chat with properties {participants:{buddy asBuddy}}
   send asMessage to newChat
end tell'''

# Create and execute the AppleScript:
script = applescript.AppleScript(source=script_source)
script.run()

Two Buddy iMessage
Code: Select all
import applescript
pyBuddy1 = indigo.variables["iMessage_Buddy1"].value
pyBuddy2 = indigo.variables["iMessage_Buddy2"].value
pyMessage = indigo.variables["iMessage_Message"].value

# First half of the AppleScript to handle the python variable value substitutions (could also define an AppleScript func and pass arguments)
script_source = '''

tell application "Messages"
   set asBuddy1 to "{}"
   set asBuddy2 to "{}"
   set asMessage to "{}"

'''.format(pyBuddy1, pyBuddy2, pyMessage)

# Second half of the script to construct and send the chat message
script_source += '''
   set newChat to make new text chat with properties {participants:{buddy asBuddy1, buddy asBuddy2}}
   send asMessage to newChat
end tell'''

# Create and execute the AppleScript:
script = applescript.AppleScript(source=script_source)
script.run()

If you have any ideas on how I can dynamically pass either one or more contacts to an Indigo Variable, and have this script read and detect how many buddy's needed to be added to the iMessage? No biggie if you don't. I have it all working now.

Thanks a bunch

Posted on
Mon Oct 07, 2019 10:21 am
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: iMessage Apple Script

Pretty sure you can do it by making your AppleScript more complex (probably easier as an AppleScript file rather than imbedding it in the Python script). Check out the Calling Handlers section of the wiki article about py-applescript. I believe you can pass a Python list (in this case the list of buddies) to a handler that you write in AppleScript that would then construct the message with the buddies and send the message.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests