AppleScript aborts execution after moving a mail message? - applescript

I'm using the following script with a Mail.app rule.
It seems that on my computer the code isn't being executed after moving the message to the Trash mailbox. (adayzdone below reports it works for him).
How can I identify the reason for this and solve it?
using terms from application "Mail"
on perform mail action with messages theMessages
repeat with eachMessage in theMessages
set theText to content of eachMessage
--
-- ... here happens some processing
--
-- this works:
move eachMessage to mailbox "Trash"
-- but this doesn't:
display dialog "reached this point"
-- i.e. additional code I'm adding here isn't executed...
end repeat
end perform mail action with messages
end using terms from

Calling it with a rule from Mail, this script works for me. Are you sure the message is reaching the inbox and not getting caught in a filter such as "skip inbox" from gmail?
using terms from application "Mail"
on perform mail action with messages theMessages
repeat with eachMessage in theMessages
set theText to content of eachMessage
move eachMessage to mailbox "Trash"
display dialog "reached this point"
beep 2
end repeat
end perform mail action with messages
end using terms from

Related

Applescript Mail - Script won't get past ''on perform mail action with messages theMessages for rule theRule"

I have set up a rule that when a email comes in from a certain sender an Applescript gets triggered. I would like that the Applescript extracts contents from the mail that triggers the rule to be send to a PHP script that sends it as a webhook to a database.
Everything works except for the the code won't run past 'on perform mail action with messages theMessages for rule theRule', it just skips that block of code and will never get to the repeat block.
What am I doing wrong?
If you need any more info I would be happy to provide.
Thanks in advance!
set d_recd to missing value
set theText to missing value
set theSender to missing value
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
repeat with theMessage in theMessages
set theDate to date recieved of theMessage
set theText to content of theMessage
set theSender to sender of theMessage
end repeat
end perform mail action with messages
end using terms from
do shell script ("php -q /Users/kaartendrukkerijmacmini/Dropbox/Technische_ontwikkeling/PHP_Webhook_approval_post.ctp " & d_recd & space & theText & space & theSender)
The do shell script line must be inside the handler. And you have to quote all parameters and coerce theDate to text.
Finally there is a typo: It's date received
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
repeat with theMessage in theMessages
set theDate to date received of theMessage
set theText to content of theMessage
set theSender to sender of theMessage
do shell script "php -q /Users/kaartendrukkerijmacmini/Dropbox/Technische_ontwikkeling/PHP_Webhook_approval_post.ctp " & quoted form of (theDate as text) & space & quoted form of theText & space & quoted form of theSender
end repeat
end perform mail action with messages
end using terms from
If the handler is not called at all the issue is somewhere else.

AppleScript texting app messages the wrong person

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?

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 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

Applescript for Mail to check if the from email address = the reply email address

I'm trying to create a rule for my incoming email to check if the From email address is the same as the Reply email address. I'm finding that a lot of the SPAM that I am getting could be filtered with this check and I'm not seeing a way to do it with the options that are in Apple Mails rules.
Try:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with aMessage in theMessages
if aMessage's sender = aMessage's reply to then
beep
--insert your code here
end if
end repeat
end tell
end perform mail action with messages
end using terms from

Resources