Skip to content

Instantly share code, notes, and snippets.

@aktau aktau/imessage
Last active Apr 10, 2019

Embed
What would you like to do?
Send iMessage from the commandline
#!/bin/sh
if [ "$#" -eq 1 ]; then stdinmsg=$(cat); fi
exec <"$0" || exit; read v; read v; read v; exec /usr/bin/osascript - "$@" "$stdinmsg"; exit
-- another way of waiting until an app is running
on waitUntilRunning(appname, delaytime)
repeat until my appIsRunning(appname)
tell application "Messages" to close window 1
delay delaytime
end repeat
-- the fact that Messages.app is running
-- does not mean it is ready to send,
-- unfortunately, add another small delay
delay delaytime
end waitUntilRunning
on appIsRunning(appName)
application appname is running
end appIsRunning
-- use system events (unused)
on SysevAppIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
-- use finder (unusged)
on finderAppIsRunning(appName)
tell application "Finder" to (name of every process) contains appName
end appIsRunning
-- taken from:
-- http://stackoverflow.com/questions/11812184/how-to-send-an-imessage-text-with-applescript-only-in-provided-service
-- thanks to users @Senseful and @DigiLord
on run {targetBuddyPhone, targetMessage}
tell application "Messages"
-- if Messages.app was not running, launch it
set wasRunning to true
if it is not running then
set wasRunning to false
launch
close window 1
my waitUntilRunning("Messages", 1)
close window 1
end if
-- send the message
set targetService to 1st service whose service type = iMessage
set targetBuddy to buddy targetBuddyPhone of targetService
send targetMessage to targetBuddy
-- if the app was not running, close the window
if not wasRunning
close window 1
end if
end tell
end run
@mapio

This comment has been minimized.

Copy link

commented Sep 23, 2014

Really nice, and works flawlessly, thank you!

@pierredewet

This comment has been minimized.

Copy link

commented Aug 11, 2015

This is great, thanks! Using it to message me my ip address every morning

@gcortes

This comment has been minimized.

Copy link

commented Feb 10, 2016

Nice piece of code. Unfortunately, it doesn't work for me. I copied it into a file called imessage with no extension and made it executable as you described. Then I put it in /usr/local/bin which is in my $PATH. When I enter
imessage xxxxxxxxxx "knock knock"

I get

1496:1529: execution error: Messages got an error: Can’t get buddy id "901DFCFD-7E56-4174-B23B-02FB668FD65E:+1xxxxxxxxxx". (-1728)

xxxxxxxxxx is a replacement for my real ten digit phone number for my iPhone. I'm running this on my iMac where I receive text messages sent to my phone from other numbers.

Update

Fixed it. One thing you didn't mention that may be obvious to most people is that you have to add yourself to your Contacts.

Update 2

I installed the code on my aging Mac Mini. On the first execution when the Message wasn't active and may have never been used previously, I got the error:

1217:1231: execution error: Messages got an error: Can’t get window 1. Invalid index. (-1719)

When I ran it again with Messages active, it was fine. I quit Message and tried it again and got the same error. I'm using the Mac Mini as a server so I can leave Messages running. I'm on OS X 10.11.3

@aktau

This comment has been minimized.

Copy link
Owner Author

commented May 27, 2016

Glad it kinda works for you guys! As for the app not running @gcortes: I actually added a safeguard for that. Could you try increasing delayTime? I.e.: change:

my waitUntilRunning("Messages", 1)

to

my waitUntilRunning("Messages", 10)

Does that work?

@zbholman

This comment has been minimized.

Copy link

commented May 30, 2016

I'm getting the same errors as gcortes, tried to increase the delayTime to 10 like you said and still unsuccessful.

I commented out the close window on line 42 to get by the "Can't get window 1" error, which then works, but then I get an error:

"Can’t get buddy id"

I have a contact card for the number I'm messaging with the phone number in it, but not sure why it can't get the buddy ID correct.

./iMessage.sh phonenumber "iMessage test"

Edit: nvm my phone number was wrong, found out email's work though as well.

And also, not really a syntax error but on line 8, you could use appname instead of "Messages", like this

tell application appname to close window 1

Midway through writing this comment I got it to work, I just had to change the phone number to the email address of the contact.

Excellent work btw though!

@FRIEDRICHEBERT

This comment has been minimized.

Copy link

commented Jun 21, 2016

Excellent Script works well, how to send pictures .jpg's ... ? is there a way?

@andermoran

This comment has been minimized.

Copy link

commented Jul 12, 2016

I can't get it to work! I'm copying the code from gist and pasting it into a shell file then using chmod u+x on the file but I can't use the iMessage command

@peterwc

This comment has been minimized.

Copy link

commented Jul 30, 2016

I had to comment out line 42 "close window 1". Still working on the sending it to myself portion. My contact info is in my contacts.

Edit: Same as @zbholman.

@jpenyc

This comment has been minimized.

Copy link

commented Sep 11, 2016

works like a charm in Sierra. thx.

@jaredmichaelwilliams

This comment has been minimized.

Copy link

commented Mar 22, 2017

i keep getting a execution error: Messages got an error: Can’t get buddy id "E166DC77-8136-4C98-B7EC-F153B020E6D0:+15555555555". (-1728)

(replaced phone number with 5s)

@briangonzalez

This comment has been minimized.

Copy link

commented Jun 11, 2017

@jaredmichaelwilliams did you find a fix?

@dreyershelby

This comment has been minimized.

Copy link

commented Dec 13, 2017

@jaredmichaelwilliams and possibly @briangonzalez I got this error by using the wrong service. If you're trying to send an SMS, the two lines,

set targetBuddy to buddy targetBuddyPhone of service "SMS"
send targetMessage to targetBuddy

will work, rather than writing

set targetService to 1st service whose service type = "SMS"

before those two lines, which I also got that error for.

@ccnokes

This comment has been minimized.

Copy link

commented Jul 14, 2018

All AppleScript solutions I've seen only seem to work with phone numbers that you have existing conversations with and are a contact 😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.