I have been attempting to create a JavaScript for Automation script to send an iMessage via the Messages app on Yosemite 10.10.2 (the end goal to write a solution to allow backoffice tasks to iMessage failure statuses etc.).
The script executes through without error in Script Editor, however no message is sent and nothing shows or happens in Messages.app. Although my service and buddy below are obfuscated, my script with the real values retrieves the correct service and buddy (as verified by doing a messages.displayAlert() for both the service and buddy name). The only output I get from the script is an "undefined" for the service.send() which I assume is correct since that method has a void return type). This script below is what I've been unsuccessfully trying to get working:
messages = Application('Messages');
service = messages.services["E:foo@bar.com"];
buddy = service.buddies["+61nnnnnnnnn"]
service.send({
send: "Hello World",
to: buddy
});
The equivalent AppleScript script (below) successfully works:
tell application "Messages"
send "Hello World" to buddy "+61nnnnnnnnn" of service "E:foo@bar.com"
end tell
Based on http://gwfrontiers.blogspot.nl/2015/01/javascript-for-automation-of-messages.html
with (Application("Messages")) {
send("Hey", {to: services["E:johndoe@icloud.com"].buddies["someone@somewhere.com"]})
}
The buddies parameter is the e-mail address or phone number you want to send it to. The johndoe@icloud.com is the (in this case) iMessage service name. Pick from:
services = Application("Messages").services();
for (var i in services) console.log(services[i].name());