Python script help with iMessage

Posted on
Sun Oct 23, 2016 11:42 am
dz1rfj offline
Posts: 136
Joined: Mar 13, 2016

Python script help with iMessage

HI,

I have this code
Code: Select all
myMessage = indigo.variables[1817154897]

cmd = """osascript<<END
    tell application "Messages"
    send "Please feed the Dogs" to buddy "8045551212" of (service 1 whose service type is iMessage)
    end tell
    END"""
def send_Message():
    os.system(cmd)
send_Message()



I tried adding this to reference a variable, but I am missing something, as it is not working: send (" %s " %myMessage.value) to buddy "8045551212" of (service 1 whose service type is iMessage)
Tried many ways, but I just don't know that much about python to get unstuck...

In this instance, the variable holds the value "Please feed the Dogs"

Is what I want possible using the code for sending an iMessage ?

Many thanks - Brian

Thanks
-Brian

Posted on
Sun Oct 23, 2016 2:17 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python script help with iMessage

try:

Code: Select all
import subprocess

# Create a substitution dictionary
message_dict = {
    "message": indigo.variables[1817154897].value,
    "buddy_id": "8045551212",
    "service": "(service 1 whose service type is iMessage)"
}

# substitute values into the script
apple_script = 'tell application "Messages" to send "%(message)s" to buddy "%(buddy_id)s" of %(service)s' % message_dict

# make a cmd list and pass it to Popen
cmd = ["/usr/bin/osascript", "-e", apple_script]
subprocess.Popen(cmd)


I made a few more things substitutions so you can make the script a bit more generic.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Oct 29, 2016 12:20 pm
dz1rfj offline
Posts: 136
Joined: Mar 13, 2016

Re: Python script help with iMessage

Jay,

Awesome, works great, thanks!

Is there an elegant/ best practice way way to add multiple buddies to be notified ?

Thanks
-Brian

Posted on
Sat Oct 29, 2016 1:22 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python script help with iMessage

No idea - i don't know how to AppleScript Messages beyond what you posted... ;)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 7 guests