Skip to content

Instantly share code, notes, and snippets.

@xoebus
Last active October 21, 2017 06:41
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xoebus/2376590 to your computer and use it in GitHub Desktop.
Save xoebus/2376590 to your computer and use it in GitHub Desktop.
Send iMessages from the Command Line

imsg

Send iMessages from the Command Line


Install

Just run the following in your shell:

$ curl https://raw.github.com/gist/2376590/imsg.rb > ~/bin/imsg
$ chmod +x ~/bin/imsg

The same command will update the script if/when I update it. It also assumes that ~/bin is in your path.

Usage

$ imsg <recipient e-mail/phone no.> <message>
#!/usr/bin/env macruby
# imsg - Send iMessages from the command line.
#
# Usage: imsg <recipient e-mail/phone> <message>
framework 'ScriptingBridge'
# Scripting Bridge is hella cool.
messages = SBApplication.applicationWithBundleIdentifier('com.apple.iChat')
buddies = messages.buddies
buddy = buddies.find do |b|
b.id.include? ARGV[0]
end
unless buddy
warn "Error: No buddy with that address."
exit 1
end
msg = ARGV[1..-1].join(" ")
messages.send msg, :to => buddy
# Hide the opened application.
command = "'tell application \"System Events\"\nset visible of process \"Messages\" to false\nend tell'"
system "osascript -e #{command}"
@jpschwinghamer
Copy link

Howdy! After installing I get a Permission denied when trying to execute that command. Any suggestions?

@sodabrew
Copy link

sodabrew commented Aug 9, 2013

chmod a+x /path/to/imsg

@xoebus
Copy link
Author

xoebus commented Nov 27, 2013

Thanks @sodabrew, I'll update the instructions.

@mike0004
Copy link

good stuff, thanks.
curl ended up with an empty file, so I used the github 'raw' link:

curl https://gist.githubusercontent.com/xoebus/2376590/raw/4b8e05ee6f2b65622b6a3ea0dbfc30fa44db738f/imsg.rb > imsg

@vitorgalvao
Copy link

The last two lines can be simplified as

command = 'tell application "System Events" to set visible of process "Messages" to false'
system "osascript", "-e", command

If you want to tell a single command with AppleScript, you don’t need

tell <application>end tell

You can instead do it in a single line with

tell <application> to

@Leckatall
Copy link

Leckatall commented Sep 27, 2017

I get the error:
/usr/bin/env: ‘macruby’: No such file or directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment