Skip to content

Instantly share code, notes, and snippets.

@devopsec
Forked from aktau/imessage
Last active June 11, 2023 19:51
Show Gist options
  • Save devopsec/0359ce51ea4fa904b1c3674b8c56db78 to your computer and use it in GitHub Desktop.
Save devopsec/0359ce51ea4fa904b1c3674b8c56db78 to your computer and use it in GitHub Desktop.
Send iMessage from the commandline
#!/usr/bin/osascript
-- 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 (unused)
on finderAppIsRunning(appName)
tell application "Finder" to (name of every process) contains appName
end appIsRunning
-- create initial conversation in Messages
-- adapted from @iSilentP
on createMessagesConversation(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 createMessagesConversation
-- 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}
-- handles conversation not started
-- does not handle contact not existing
set hasError to false
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
log "trying via imessage"
try
set targetService to 1st service whose service type = iMessage
set targetBuddy to buddy targetBuddyPhone of targetService
send targetMessage to targetBuddy
log "sent via imessage"
on error
log "trying via SMS"
try
set targetService to service "SMS"
set targetBuddy to buddy targetBuddyPhone of targetService
send targetMessage to targetBuddy
log "sent via SMS"
on error
set hasError to true
end try
end try
-- if the app was not running, close the window
if not wasRunning
close window 1
end if
end tell
if hasError
log "trying via new conversation"
try
createMessagesConversation(targetBuddyPhone,targetMessage)
log "sent via new conversation"
on error
log "contact does not exist, can not send message"
end try
end if
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment