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

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?

Related

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

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?

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 aborts execution after moving a mail message?

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

How to open an email message using applescript?

I am writing a small applescript which retrieves all "unread" messages in the viewer and loops them.
I have two goals to complete:
I need to get the subject of each message and perform a regular expression to see if it's suitable for step 2 (ex: get emails with subject {.*})
I need to open each message on a separate window and after 4 seconds, I need to close that window and proceed with the next message
Do you know how to do these?
Thanks in advance.
The following applescript works for me, but I'm not sure how to do the regex matching. You can use the unix 'grep' function with applescript's 'do shell script' command, but I'm no expert in how to use grep properly. I'll leave that for someone else to answer.
on run
tell application "Mail"
set myInbox to mailbox "INBOX" of account 1
set myMessages to every message of myInbox
repeat with theMessage in myMessages
if read status of theMessage is false then
if my subjectIsInteresting(subject of theMessage) then
open theMessage
delay 4
close window 1
end if
end if
end repeat
end tell
end run
on subjectIsInteresting(subject)
-- do some regex magic here
return true -- for now
end subjectIsInteresting
For regexes -- If you're running the script on your own machine, or can distribute it bundled, you could use Satimage's Smile extension (http://www.satimage.fr/software/en/downloads/index.html) which adds regexes to Applescript.
I know you already have your answer but have you looked into Automator? For most standard scripts such as this, it can be less painful if you aren't too familiar with AppleScript. It's not very 'programmy' but it's quick and you'll spend less time debugging.

Resources