Send iMessages via the commandline on OSX, even to yourself
Today on HN someone posted a small ruby utility to send iMessages via the commandline, which piqued my interest. I would love to send messages to my phone when one of my cronscripts discovers something interesting. However, the utility doesn’t allow sending to self.
So I looked around and found a way to get around this restriction, and it doesn’t even require ruby anymore!
After realizing the utility worked through applescript 1, I perused google to find a way to circumvent the inability to send to self. Lo and behold, Stack Overflow has the answer!
I didn’t like typing: osascript "script" "number" "message"
all the
time, so I looked for a way to use it like a normal UNIX
script.
With this unholy shebang hack, it now looks like this:
#!/bin/sh
exec <"$0" || exit; read v; read v; exec /usr/bin/osascript - "$@"; exit
on run {targetBuddyPhone, targetMessage}
tell application "Messages"
set targetService to 1st service whose service type = iMessage
set targetBuddy to buddy targetBuddyPhone of targetService
send targetMessage to targetBuddy
end tell
end run
Gist version here. It’s a bit
more complicated than what’s pasted above because it tries to launch
Messages.app if it isn’t running. Paste that in a file, make it
executable with chmod u+x "thefilename"
and you’re good to go. It
doesn’t even require Ruby, so you can just drop the script in some
folder in your $PATH
and use it like this:
$ imessage 0487198747 "gofinance: your stock AAPL has gone up by 20%"
$ imessage 0495876923 "knock knock!"
Those are not actual telephone numbers (that I know of), by the way. There are even more cool things you could do with this. Someone has even made a Lisp REPL that works over iMessage. Oh the possibilities!
Tags: imessage, osx, applescript