AppleScript texting app messages the wrong person - applescript

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?

Related

How can I check if a number is registered in iMessage or not?

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?

How to send pictures using AppleScript

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

Can't access to mail attachments in mail app via apple script

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?

How to start new conversation in iMessage using AppleScript?

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

How to use Applescript to delete Conversations whose senders contain email addresses from the Messages app

I want to use applescript to delete conversations from people who sent me messages using email addresses. I want to keep all conversations from mobile numbers, but delete all conversations, in which my correspondent is an email address.
I've tried:
tell application "Messages"
tell every item
delete (every item whose sender contains ".com")
end tell
save
end tell
All scriptable applications have an AppleScript Dictionary that can be accessed from Script Editor via the Window --> Library menu. Reading through an application's dictionary can help you obtain the proper terms for generating a script to do your bidding.
The script below will successfully create a list of chat IDs of all chats (conversations) wherein at least one participant is using an email address. I have not tested the delete section, since I am not interested in deleting anything. I strongly recommend that you run Time Machine or another backup service BEFORE executing that portion of the script, just in case you get unexpected results.
set chatsToKill to {}
tell application "Messages"
set allChats to every chat
repeat with eachChat in allChats
set thePeeps to participants of eachChat
repeat with oneParticipant in thePeeps
if oneParticipant's handle contains "#" then
if chatsToKill does not contain eachChat's id then set end of chatsToKill to eachChat's id
end if
end repeat
end repeat
repeat with deathChat in chatsToKill
delete item 1 of (get every chat whose id = deathChat)
end repeat
end tell

Resources