Using Messages.app new "AppleScript handler" in Mavericks

Hi all,


Since upgrading to Mavericks last fall, I was disappointed to see that many of the custom alert options in the Messages.app preferences have since been removed. Now, I am looking at using its built-in "AppleScript handler" option to get Messages.app to do what I'm wanting. The problem is, I don't know a thing about AppleScript.


In a nutshell...


I'm using Jabber, and all I want is to have a custom sound play when a certain buddy sends me a message. What would I need to do in AppleScript to get this to happen? I've been playing around with the Script Editor for the better part of six hours, and I'm failing miserably. Anyone able to walk me through it? (e.g. AppleScript for Dummies style.)


Thanks for any help.

Posted on Aug 2, 2014 7:32 AM

Reply
18 replies

Aug 2, 2014 8:21 AM in response to snicks-capital

There are a lot of tutorials for using Applescript, but finding one that gets to the heart of what you want isn't easy.


See the following for an example script:

http://www.toobusyto.org.uk/tooBusy/2013/12/29/contact-online-notification-in-me ssages-os-x-mavericks/

I'll try to walk you through how I think it should work, but I don't have a Mac with me to test, so you'll have to post the errors if they occur, and I might be able to fix them. Or, someone else here might be able to help.


Note that you have to process every handler, regardless of the ones you really want.

If you want to keep the notification that your buddy came online, just modify it like this:

property myBuddysName: "buddy"


using terms from application "Messages"

on buddy became available theBuddy with description

if name of theBuddy is myBuddysName then

set notificationMessage to myBuddysName & " is online."

set notificationTitle to "Buddy is online"

display notification notificationMessage with title notificationTitle sound name "Sosumi"

delay 1

end if

end buddy became available

on message received theMessage from theBuddy for theChat

if name of theBuddy is myBuddysName then

set notificationMessage to myBuddysName & " sent a message"

display notification notificationMessage with sound name "Sosumi"

delay 1

end if

end message received

-- INSERT ALL OTHER EMPTY HANDLERS HERE

end using terms from

Aug 2, 2014 8:26 AM in response to Barney-15E

I have also found that there are different lists of handlers floating around.

You will need to look at the Messages Dictionary in Applescript Editor to get the correct ones.


This might affect the on message received theMessage from theBuddy for theChat as it doesn't appear in this list:

http://stackoverflow.com/questions/12377572/trying-to-receive-a-message-from-os- x-message-w-applescript


You may need to use this handler:

 on addressed message received theMessage from theBuddy for theChat with eventDescription
end addressed message received

Nothing inside will change, but if the one I posted no longer exists, you'll need to change it to match the above (if it exists).

Aug 2, 2014 1:55 PM in response to snicks-capital

Hi,


There is a simpler way in Messages 8 (Mavericks).


Place a sound in ~/Library/Sounds This is the Sounds folder in the Library within your Home folder (Little House icon).

In the Finder use the Go Menu > Go to Folder and enter the path above (you can Copy it form here) in the dialogue box that appears.


Then in Messages > Preferences > General section changes the "Messages Received" item drop down to the item you want.

The list displays the default Messages Received Sound from within the app.

It goes on to list the System Sounds and then the Contents of ~/Library/Sounds

User uploaded file


This will play a sound for All Buddies and iMessages incoming Messages.


AppleScript is needed to play it for separate Buddies or for different types of Chats/Actions.


EDIT.
Note that where the AppleScript goes has also changed

This is where Mavericks and Messages 8 wants them

User uploaded file

That ~/Library/Application Scripts/Com.apple.iChat


They used to be in:-

~/Library/Scripts/Messages in Messages 7.x.x (Mountain Lion)

User uploaded file



User uploaded file

9:50 pm Saturday; August 2, 2014


 iMac 2.5Ghz i5 2011 (Mavericks 10.9)
 G4/1GhzDual MDD (Leopard 10.5.8)
 MacBookPro 2Gb (Snow Leopard 10.6.8)
 Mac OS X (10.6.8),
 Couple of iPhones and an iPad

Oct 9, 2014 8:48 PM in response to Barney-15E

Hi,


Thanks for taking the time to provide this information to me. I know it's been awhile, but I just recently got the time to try this. I think I'm close, but still running into some errors. I'm an AppleScript novice, so bear with me, but here's what I got...


When I'm attempting to run the script, I'm getting the message:

Syntax Error

Expected end of line, etc. but found “"”.

When I click OK, it highlights the first quote after before the sound name ("Sosumi").


Any ideas? I've posted my syntax below. Thanks.

----------------------------------



using terms from application "Messages"


on message received theMessage from theBuddy for theChat

if name of theBuddy is myBuddysName then

set notificationMessage to myBuddysName & " sent a message"

display notification notificationMessage with sound name "Sosumi"

delay 1

end if

end message received


# The following are unused but need to be defined to avoid an error


on received text invitation theText from theBuddy for theChat


end received text invitation


on received audio invitation theText from theBuddy for theChat


end received audio invitation


on received video invitation theText from theBuddy for theChat


end received video invitation


on received remote screen sharing invitation from theBuddy for theChat


end received remote screen sharing invitation


on received local screen sharing invitation from theBuddy for theChat


end received local screen sharing invitation


on received file transfer invitation theFileTransfer


end received file transfer invitation


on buddy authorization requested theRequest


end buddy authorization requested


on message sent theMessage for theChat


end message sent


on message received theMessage from theBuddy for theChat


end message received


on chat room message received theMessage from theBuddy for theChat


end chat room message received


on active chat message received theMessage


end active chat message received


on addressed chat room message received theMessage from theBuddy for theChat


end addressed chat room message received


on addressed message received theMessage from theBuddy for theChat


end addressed message received


on av chat started


end av chat started


on av chat ended


end av chat ended


on login finished for theService


end login finished


on logout finished for theService


end logout finished


on buddy became unavailable theBuddy


end buddy became unavailable


on completed file transfer


end completed file transfer

end using terms from

Oct 9, 2014 10:14 PM in response to Barney-15E

Thanks so much! I removed the "with" and it took it! But there's one more hiccup. I finally tested it out by plugging my buddy's name in the placeholder in the syntax. I put the script in Messages' scripts folder and gave it a go.


When I test it out, Messages throws this error:


An error occurred while executing an AppleScript event handler.

Event: Message Received

File: Untitled.applescript

Error: The operation couldn’t be completed. /Users/Me/Library/Application Scripts/com.apple.iChat/BuddyMessageNotification.applescript: execution error: The variable myBuddysName is not defined. (-2753)

3)


So there's something wrong with the placeholder. I replaced "myBuddysName" with their username, but the above error still comes up when receiving (or even sending) a message.


Any thoughts? Thanks again.

Oct 10, 2014 4:31 PM in response to snicks-capital

Hi,


In your script:


on message received theMessage from theBuddy for theChat

if name of theBuddy is myBuddysName then

set notificationMessage to myBuddysName & " sent a message"

display notification notificationMessage with sound name "Sosumi"

delay 1

end if

end message received


Replace myBuddysName with theBuddy.


Edit: myBuddysName probably needs to be in double quotes "myBuddysName".


Otherwise the script thinks that it is a variable.

Oct 10, 2014 4:37 PM in response to Barney-15E

Sorry, let me clarify: it doesn't actually say the word: "MyBuddysName" in the error, it actually says my buddy's name. You know, his username. So in other words, the error is saying, "The variable AWESOMEdudeJASONx1xxa2004soccerchick is not defined."


Haha, but my buddy's name is certainly not AWESOMEdudeJASONx1xxa2004soccerchick, I just kept it as myBuddysName to keep it private on the web. 🙂

Oct 10, 2014 4:49 PM in response to kelhak1

That did it! Thank you! The script is now sending a notification with a sound whenever this certain buddy sends a message! Thank you for the tip!


Okay, so now I have a follow up... what are the chances that I can customize this script so that I'd be able to get that sound for more than one buddy?


Can I have "myBuddysName1" and "myBuddysName2" both in the same script? So that if either of them message me, the same sound would be played?


Thanks again.

Oct 10, 2014 4:51 PM in response to Barney-15E

I mentioned this to one of the other responses — thank you, adding the quotes did it. So I have another question though, and this is a long shot: what are the chances that I can customize this script so that I'd be able to get that sound for more than one buddy?


Can I have "myBuddysName1" and "myBuddysName2" both in the same script? So that if either of them message me, the same sound would be played?


Thanks again.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Using Messages.app new "AppleScript handler" in Mavericks

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