So I'm working on creating an applescript which essentially automates sending an imessage. What I have working now is:
on run {msg, phoneNum}
tell application "Messages"
set serviceID to id of 1st service whose service type = iMessage
send msg to buddy phoneNum of service id serviceID
end tell
end run
This works for the most part except it doesn't work when starting a new conversation. When you run the script to a number which you don't have a conversation with in messages, you get a popup warning saying "Your message does not have any recipients". However, this creates a conversation with that person, and when you run the same script again it works.
I figured if it works the second time, there must be a way to create a new conversation somehow, however I have never really used applescript or really any script languages before so I'm not sure how to go about that.
Edit: Immediately after posting I thought of a rough workaround. If right before you send the message you send an empty string, you can create a new conversation, and it works with a already existing conversation.
on run {msg, phoneNum}
tell application "Messages"
set serviceID to id of 1st service whose service type = iMessage
send "" to buddy phoneNum of service id serviceID
send msg to buddy phoneNum of service id serviceID
end tell
end run
While this works, I'd imagine there is a better/more elegant solution than this one.
My solution is to tell Applescript to press "Command + N", which is the shortkey for "Start a new conversation"
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 "<replace with phone number>" -- input the phone number
key code 36 -- press Enter to focus on the message area
keystroke "<replace with message>" -- type some message
key code 36 -- press Enter to send
end tell
This script will start a new conversation and send the message to the phone number through iMessage
There are many ways to do it.
First example:
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
Second example:
tell application "Messages"
set targetBuddy to "+18001234567"
set targetService to id of 1st service whose service type = iMessage
repeat
set textMessage to "Hello pal!"
set theBuddy to buddy targetBuddy of service id targetService
send textMessage to theBuddy
delay (random number from 10 to 30)
end repeat
end tell
Related
I'm looking for a way to find out that if a number has registered in iMessage or not. So far I know how to send a message using this script:
set number to "+111111111111"
set message to "Test"
tell application "Messages"
set targetServise to 1st service whose service type = iMessage
set targetBuddy to buddy number of targetService
delay 1
if targetBuddy exists then
send message to targetBuddy
end if
#delay 5
end tell
All I need now, is to know whether this number has been registered in iMessage or not?
P.S: Is there any kind of API for that?
I made a script that sends messages to people. I also want to be able to send pictures through this script.
on run
tell application "Messages"
set targetService to 1st service whose service type = iMessage
set targetBuddy to buddy "18008888888" of targetService
repeat 3 times
send "/Users/ADMIN/Desktop/photo.png" to
targetBuddy
end repeat
end tell
end run
Putting the directory of the photo in quotations makes the message sent be a string, and taking away the quotes results in an error.
You need a file reference. Try
send POSIX file "/Users/ADMIN/Desktop/photo.png" to targetBuddy
or
set filePath to (path to desktop as text) & "photo.png"
tell application "Messages"
set targetService to 1st service whose service type = iMessage
set targetBuddy to buddy "18008888888" of targetService
repeat 3 times
send file filePath to targetBuddy
end repeat
end tell
I have this script that sends multiple text messages, but sometimes the script will send the first message to another number that I've recently messaged in Messages. I can't seem to find a pattern either, sometimes the bug happens and sometimes it sends all of the texts to the right number.
--usage: osascript sendMessage phone_number text_to_send amount_to_send
--Sends targetMessage to targetBuddyPhone repeatAmount times to targetMessage
on run {targetBuddyPhone, targetMessage, repeatAmount}
set counter to 0
repeat while counter < repeatAmount
tell application "Messages"
send targetMessage to buddy targetBuddyPhone of service "SMS"
set counter to counter + 1
end tell
end repeat
end run
How can I fix this?
I found a script that sends messages from the command line, but It won't work with my group chat with spacing and capitalization in the name.
#!/bin/sh
recipient="${1}"
message="${*:2}"
echo "$recipient"
cat<<EOF | osascript - "${recipient}" "${message}"
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
EOF
when I run ./sendmessage "GROUP CHAT" "test" it gives me
228:261: execution error: Messages got an error: Can’t get buddy id "7087805D-ED73-4DB9-81AB-7C964B98AB34:group chat". (-1728)
Unforunately, it is not possible to send a message to a group chat with applescript by using the chat's name. To send to an existing group chat, you will instead need to obtain the chat's internal guid. There are 2 methods of doing this.
Extract it from the messages.app internal database, located on disk at ~/Library/Messages/chat.db. It is the guid column under the chat table. If you have the group chat named it will be easy to find by display_name.
You can use an AppleScript handler for Messages with the on chat room message received handler to capture the ID when someone posts in that chat.
Once you have the guid (should in a format similar to iMessage;+;chat831551415676550949), your script can be modified to send to the ID instead:
#!/bin/sh
recipient="${1}"
message="${*:2}"
echo "$recipient"
cat<<EOF | osascript - "${recipient}" "${message}"
on run {targetChatID, targetMessage}
tell application "Messages"
set targetChat to a reference to text chat id targetChatID
send targetMessage to targetChat
end tell
end run
EOF
To send an image instead of text, replace the targetMessage with:
set ImageAttachment to POSIX file "File path here" as alias
Let's assume that in the following code
tell application "Mail"
tell first account
tell first mailbox
set myMessage to first message
set myAttachment to mail attachments of myMessage
end tell
end tell
end tell
the first message of the first mailbox of the first account actually has attachments. Let's further assume that the account in question is a GMail account (maybe that proves relevant) and that I am trying this on El Capitan.
On the "set myAttachments" line I always get the error "error in apple event routine, -10000" (losely translated from German). I have read in old threads that there was indeed a bug in the access to attachments. But I also saw a new thread that was talking specifics about how to best handle them (no mentioning of having no access at all).
What am I missing? Does it work? Maybe in general but not with GMail? Do I have to do something specific?
Thanks!
Sandro
The exact syntax for this is "mail attachement of myMessage" (without "s").
The script bellow is working perfectly on my side for selected emails:
tell application "Mail"
activate
set ListMessage to selection
repeat with aMessage in ListMessage
set AList to every mail attachment of aMessage
repeat with aFile in AList
if (downloaded of aFile) then
-- do something with aFile
end if
end repeat -- next file
end repeat -- next message
end tell
Are you sure the the message with attachement is in your first account/first mailbox?