diff --git a/index.js b/index.js index 826bf6a..417628f 100644 --- a/index.js +++ b/index.js @@ -81,6 +81,16 @@ function handleForName(name) { })(name) } +// Gets the display name for a given handle +// TODO: support group chats +function nameForHandle(handle) { + assert(typeof handle == 'string', 'handle must be a string') + return osa(handle => { + const Messages = Application('Messages') + return Messages.buddies.whose({ handle: handle }).name()[0] + })(handle) +} + // Sends a message to the given handle function send(handle, message) { assert(typeof handle == 'string', 'handle must be a string') @@ -195,6 +205,7 @@ module.exports = { send, listen, handleForName, + nameForHandle, getRecentChats, SUPPRESS_WARNINGS: false, } diff --git a/readme.md b/readme.md index 13603db..d09fbf0 100644 --- a/readme.md +++ b/readme.md @@ -144,6 +144,22 @@ Type: `Promise` A promise that resolves with the `handle` of the contact, or rejects if nobody was found. +### Get the name associated with a given handle + +`nameForHandle(handle) -> Promise` + +**handle** + +Type: `string` + +The handle of a contact. + +**return** + +Type: `Promise` + +A promise that resolved with the full name of the desired contact, as displayed in `Messages.app`. + ### Get recents chats `getRecentChats(limit) -> Promise`