Previous 1 2 Next 19 Replies Latest reply: Jun 17, 2015 12:48 PM by JJRicks
iMavy Level 1 Level 1 (0 points)

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 (46,440 points)

    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.

  • iMavy Level 1 Level 1 (0 points)

    Thank you, Camelot!

     

    I just can't figure out what the name of my iMessage account would be. I tried "iCloud", "iMessage" and my email accounts. In Accounts it says "iMessage" but it doesn't work. That's why I tried to creat a work around...

     

    Any ideas? Thanks

  • Camelot Level 8 Level 8 (46,440 points)

    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)

    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 (46,440 points)

    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)

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

     

    Thanks again

  • an.ke Level 1 Level 1 (0 points)

    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)

    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)

    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)

    Is there a way to send a group message?

  • joshpetty Level 1 Level 1 (0 points)

    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)

    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)

    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?

  • iMavy Level 1 Level 1 (0 points)

    I'm confused at this point wrote:

     

    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?

     

    Why don't you use the built in feature from notification center for this?

     

    Screen Shot 2014-07-11 at 14.55.43.png

Previous 1 2 Next