Skip to content

Instantly share code, notes, and snippets.

@aktau
Last active June 20, 2026 00:20
Show Gist options
  • Select an option

  • Save aktau/8958054 to your computer and use it in GitHub Desktop.

Select an option

Save aktau/8958054 to your computer and use it in GitHub Desktop.
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
@FRIEDRICHEBERT

Copy link
Copy Markdown

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

@andermoran

Copy link
Copy Markdown

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

peterwc commented Jul 30, 2016

Copy link
Copy Markdown

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

jpenyc commented Sep 11, 2016

Copy link
Copy Markdown

works like a charm in Sierra. thx.

@jaredmichaelwilliams

Copy link
Copy Markdown

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

Copy link
Copy Markdown

@jaredmichaelwilliams did you find a fix?

@dreyershelby

Copy link
Copy Markdown

@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

ccnokes commented Jul 14, 2018

Copy link
Copy Markdown

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

@dalenguyen

Copy link
Copy Markdown

Yeah, for the buddy id error, you need to have an existing conversation first!

@coolaj86

coolaj86 commented Feb 16, 2020

Copy link
Copy Markdown

For the Messages got an error: Can’t get buddy id ... error Is there any way to create a buddy id without manually messaging first?

I tried importing a CSV into Contacts.app, which I thought would work, but... nope.

I just got a bunch of people join a meetup I co-organize and was hoping to not have to message all of the new ones individually before sending reminders.

@james-tindal

james-tindal commented Mar 25, 2020

Copy link
Copy Markdown

@solderjs
Here's how to send messages without manually creating a buddy:

#!/bin/sh
if [ "$#" -eq 1 ]; then stdinmsg=$(cat); fi
exec <"$0" || exit; read v; read v; read v; exec /usr/bin/osascript - "$@" "$stdinmsg"; exit

on run {phoneNumber, message}
    activate application "Messages"
    tell application "System Events" to tell process "Messages"
        key code 45 using command down -- press Command + N to start a new window
        keystroke phoneNumber -- input the phone number
        key code 36 -- press Enter to focus on the message area 
        keystroke message -- type some message
        key code 36 -- press Enter to send
    end tell
end run

Here's how to send to multiple contacts with a csv file: https://gist.github.com/james-tindal/3951ec2c076a5158e15245f817f92de4

Works on my computer.

@iSilentP

iSilentP commented May 9, 2020

Copy link
Copy Markdown

@solderjs
Here's how to send messages without manually creating a buddy:

#!/bin/sh
if [ "$#" -eq 1 ]; then stdinmsg=$(cat); fi
exec <"$0" || exit; read v; read v; read v; exec /usr/bin/osascript - "$@" "$stdinmsg"; exit

on run {phoneNumber, message}
    activate application "Messages"
    tell application "System Events" to tell process "Messages"
        key code 45 using command down -- press Command + N to start a new window
        keystroke phoneNumber -- input the phone number
        key code 36 -- press Enter to focus on the message area 
        keystroke message -- type some message
        key code 36 -- press Enter to send
    end tell
end run

Here's how to send to multiple contacts with a csv file: https://gist.github.com/james-tindal/3951ec2c076a5158e15245f817f92de4

Works on my computer.

Thanks for this. Is there a way to do this without having the iMessage window popup and display on the desktop?

@thedovester101

Copy link
Copy Markdown

I get error "966:967: syntax error: Expected expression, etc. but found unknown token. (-2741)" what does this mean ? thanks

@boygiandi

Copy link
Copy Markdown

Can you help me send photo through iMessage ? I tried this code but it show "Failed to send" after few minutes of waiting

set theAttachment1 to POSIX file "/path to file/test.png"
send file theAttachment1 to targetBuddy

@devopsec

devopsec commented Feb 1, 2021

Copy link
Copy Markdown

Added some features to my fork you may find interesting:
https://gist.github.com/devopsec/0359ce51ea4fa904b1c3674b8c56db78

@punkroast

Copy link
Copy Markdown

Is there a way to keystroke a Group chat instead of a phone number?

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