Skip navigation

AppleScript to send new iMessage

12814 Views 13 Replies Latest reply: Jan 6, 2014 6:46 PM by I\'m confused at this point RSS
iMavy Calculating status...
Currently Being Moderated
Sep 21, 2012 2:01 PM

Hey guys - I'm trying to create an apple script to send a new iMessage. But I'm stuck. Can anybody help?

 

tell application "System Events"

          tell application "Messages" to activate

          tell process "Messages"

                    click menu item "New Message" of menu "file" of menu bar 1

          end tell


...


end tell

 

... and thats it. How Do I tell Messages to fill in an email adress or phone number now and to send it?

 

Thank you!

Mac Pro (Early 2008), OS X Mountain Lion (10.8.2)
  • Camelot Level 8 Level 8 (45,680 points)
    Currently Being Moderated
    Sep 21, 2012 8:18 PM (in response to iMavy)

    The best way is to not use UI Events at all - they're cumbersome at best. Instead consider the direct approach - tell Messages what you want it to do (send a message) not necessarily how you would do it (click here, type this, etc.)

     

    For example, to send a message to a buddy you can:

     

    tell application "Messages"

              set theBuddy to buddy "someone@mac.com" of service "iCloud"

              send "Hi there" to theBuddy

    end tell

     

    (adjust for your appropriate service name, of course.

  • Camelot Level 8 Level 8 (45,680 points)
    Currently Being Moderated
    Sep 22, 2012 4:07 PM (in response to iMavy)

    just run:

     

    tell application "Messages"

    get every service

    end tell

     

    This will return a list of all the services that Messages is configured with. It should be clear what your service name is from this list.

  • epi_jimenez Level 1 Level 1 (0 points)
    Currently Being Moderated
    Oct 1, 2012 12:54 PM (in response to Camelot)

    thanks for the help Camelot

    i followed your steps and got a service id for icloud which looks like this

     

    tell application "Messages"

              set theBuddy to buddy "+15555555555" of service id "F7273233-267B-4EB6-851D-49B440B18411"

              send "hi there" to theBuddy

    end tell

     

    would that service id work on all computers that the script is run?

    or is that unique by computer? is so, how can i make it universal?

    i can not get it to work by naming my service "iCloud" only.

     

    note* im using the phone number instead of the email, i did noticed that when using messages, the conversation has to be started already  in order for the "buddy" to be recognized

  • Camelot Level 8 Level 8 (45,680 points)
    Currently Being Moderated
    Oct 2, 2012 9:21 AM (in response to epi_jimenez)

    would that service id work on all computers that the script is run?

    or is that unique by computer?

    Service IDs are unique per computer, so that's not going to work on any other system.

    is so, how can i make it universal?

    Well, don't use ID .

    If you want a script that will work on ANY machine then you have a problem since there is no persistent identifier. You can reference any service by a number of parameters (e.g. ID, name, index value, etc.) but there's no guarantee that any value will be the same on any other machine - even the 'name' may be different, and the index value may be different depending on how many accounts the user has setup.

    i can not get it to work by naming my service "iCloud" only.

    Is that the name of your service? I'm guessing not. Amend my earlier script to 'get name of every service' if you want to see the names you can use. This still may not be consistent across machines, though.

    i did noticed that when using messages, the conversation has to be started already  in order for the "buddy" to be recognized

    That shouldn't be the case. Is the remote user in question in your buddy list? If he isn't then you can't reference a buddy by name since he doesn't exist on your system until a conversation is in progress. If a buddy is in your buddy list then you should be able to reference them by name without an active conversation.

  • epi_jimenez Level 1 Level 1 (0 points)
    Currently Being Moderated
    Oct 3, 2012 2:32 PM (in response to Camelot)

    This worked out. My service name is something like "E:myemail@provider.com".

     

    Thanks again

  • an.ke Level 1 Level 1 (0 points)
    Currently Being Moderated
    Oct 11, 2012 1:42 PM (in response to iMavy)

    for me this is working very well .. on all my macs with different accounts(apple-IDs), and without manually handling the ID

     

    tell application "Messages"

              set myid to get id of first service

              set theBuddy to buddy "+15555555555" of service id myid

              send "Hi there" to theBuddy

    end tell

  • CharlesLCSo Level 1 Level 1 (0 points)
    Currently Being Moderated
    Dec 3, 2012 10:19 AM (in response to iMavy)

    Is it possible to ask iMessage to check if buddy's phone number is iMessage, or just SMS?

  • epi_jimenez Level 1 Level 1 (0 points)
    Currently Being Moderated
    Dec 3, 2012 10:30 AM (in response to CharlesLCSo)

    As far as I know: no.

     

    Through the scripts it will only see a buddy if a previous conversation has been started.

     

    Therefore, if the conversation has not been started at all, the script won't work.

  • SU Level 1 Level 1 (105 points)
    Currently Being Moderated
    Jan 3, 2013 8:06 PM (in response to epi_jimenez)

    Is there a way to send a group message?

  • joshpetty Level 1 Level 1 (0 points)
    Currently Being Moderated
    Sep 17, 2013 4:49 PM (in response to SU)

    I am hoping to find a script that will send a group as well! Any help with this?

  • Mark Lauren Level 1 Level 1 (0 points)
    Currently Being Moderated
    Oct 30, 2013 10:48 PM (in response to iMavy)

    tell application "Messages"

              send "Text of Message" to buddy "+61mobilenumber" of service "E:*your icloud account email address *"

    end tell

     

    I had same problem and tested it works with above.

  • I\'m confused at this point Level 1 Level 1 (0 points)
    Currently Being Moderated
    Jan 6, 2014 6:46 PM (in response to iMavy)

    I am trying to make a script that comes up and asks you for who you want to send it to and what you want it to say and send it, how could i make this script incorporate my predefined variables with this info?

Actions

More Like This

  • Retrieving data ...

Bookmarked By (1)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.