forked from matrix-hacks/matrix-puppet-imessage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimessage-send-group.js
55 lines (46 loc) · 1.31 KB
/
imessage-send-group.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const spawn = require('child_process').spawn;
const escapeString = (str) => {
return str.replace(/[\\"]/g, '\\$&');
};
module.exports = function(id, _message, file) {
console.log(id);
let message = escapeString(_message);
let args = ['-e'];
const setAttachment = `
tell application "System Events"
set theAttachment to POSIX file "${file}"
end tell
`;
const sendAttachment = `
send theAttachment to thisChat
`;
const sendMessage = `
send "${message}" to thisChat
`;
args.push(`
${file ? setAttachment : ''}
tell application "Messages"
activate
set thisChat to a reference to chat id "iMessage;+;${id}"
${file ? sendAttachment : sendMessage}
end tell
`);
console.log('full applescript', args[1]);
return new Promise(function(resolve, reject) {
// Check user input
if (!id) reject(new Error('You didn\'t enter a chat id!'));
else if (!message) reject(new Error('You didn\'t enter a message!'));
else {
var proc = spawn('/usr/bin/osascript', args );
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
proc.on('exit', function(exitCode) {
if (exitCode != 0) {
reject(new Error('exited nonzero'));
} else {
resolve('SENT');
}
});
}
});
};