-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathAutoForwardIMessageText.applescript
78 lines (67 loc) · 2.73 KB
/
AutoForwardIMessageText.applescript
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# AutoForwardIMessageText.applescript
# - Supports 2-way relaying of iMessage to/from another chat service
# - To send iMessage, each outgoing iMessage must be formatted as: "[iMessage destination] Message"
# References
# https://discussions.apple.com/thread/5214769?start=0&tstart=0
# https://46b.it/2012/hacking-with-imessage
# You can enumerate all your chat accounts on Messages like this:
(*
tell application "Messages"
get name of every service
end tell
*)
# Example result: {"Bonjour", "Facebook Chat", "AIM", "E:[email protected]"}
# What's your iMessage/iCloud/Apple ID? Note the format
property myIMsgService : "E:[email protected]"
# Remember the iMessage that just came in
property recvService : ""
property recvText : ""
property recvBuddy : ""
# Account to use for forwarding
property fwdService : "Fowarding Chat Acct"
# The buddy to receive the forwarded message - presumably your primary chat acct
property fwdServiceBuddy : "[email protected]"
# What ultimately gets forwarded
property fwdMsg : ""
# Event handler
using terms from application "Messages"
on message received theText from theBuddy for theChat
# get what we need
set recvService to name of service of theChat
set recvText to theText
set recvBuddy to name of theBuddy as text
# fwd
if recvText "" then
try
if recvService = myIMsgService then # incoming iMessage
# recvBuddyId is ABCDEFGH-IJKL-MNOP-QRST-UVWXYZABCDEF:+17894560123
set recvBuddyId to id of theBuddy as text
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set recvBuddyId to text item 2 of recvBuddyId
set AppleScript's text item delimiters to oldDelims
# now recvBuddyId is +17894560123
set fwdMsg to "[" & recvBuddy & "] [" & recvBuddyId & "] [" & recvText & "]"
set sendServiceName to name of 1st service whose name = fwdService
set myid to get id of service sendServiceName
set sendBuddy to buddy fwdServiceBuddy of service id myid
send fwdMsg to sendBuddy
else if recvService = fwdService then # outgoing iMessage
set sendServiceName to name of 1st service whose name = myIMsgService
set myid to get id of service sendServiceName
set tokens to words of recvText
set targetBuddy to get item 1 of tokens
set sendBuddy to buddy targetBuddy of service id myid
# chop out targetBuddy from recvText
set fwdMsg to text (2 + (length of targetBuddy)) thru (length of recvText) of recvText
send fwdMsg to sendBuddy
end if
on error err
# optionally log errors here
end try
end if
# make messages happy
return true
end message received
end using terms from
# Nothing you put here will get executed - only what's inside the event handler block runs