Best way to delete all attachments in all messages in specific Mailbox - applescript

I'm total AppleScript novice and I'm trying to write a script which will delete all attachments in all messages in specified Mailbox (Mail.app).
Here is what am I trying:
tell application "Mail"
tell mailbox "Sent" of account id "<XXX>"
set ListMessage to get every message
repeat with aMessage in ListMessage
set aList to get every mail attachment of aMessage
repeat with aFile in aList
if (downloaded of aFile) then
delete aFile -- this gives an error
end if
end repeat -- next file
end repeat -- next message
end tell
end tell
Running this results in error:
Error: the Apple Event handler failed (errAEEventFailed:-10000)
What am I doing wrong?
Thank you!

It turns out, that delete doesn’t work on mail attachments, but there is an alternative way to solve the issue: you can click the Remove Attachments menu item with System Events:
tell application "Mail"
activate
every item of (get selection)
end tell
tell application "System Events" to click menu item "Remove Attachments" of menu "Message" of menu bar item "Message" of menu bar 1 of process "Mail"

Related

How do I make this AppleScript Folder Watch ignore hidden items?

I have pretty much spent my entire Sunday trying to find an answer to this and I am now admitting defeat and asking for your help. I've read several examples on here and some other AppleScript sites but non of them seem to work when applied to this script. I do however put this down to me.
I'm trying to use the script below to watch a folder then send me an email when a new item has been added and it works just fine. The trouble I am having is getting it to ignore system [or hidden] folders.
I have removed my efforts to do this as they all kept breaking the whole thing, so the version below works but currently it will include hidden items.
property theName : "Name"
property theAddress : "name#company.com"
on adding folder items to this_folder after receiving added_items
set added_Items_List to {}
repeat with oneItem in added_items
set end of added_Items_List to name of (info for oneItem)
end repeat
set {TID, text item delimiters} to {text item delimiters, ", "}
set added_Items_List to added_Items_List as text
set text item delimiters to TID
set dateString to (current date) as string
set theBody to "There are new files in the FTP folder " & name of (info for this_folder) & ":" & return & return & added_Items_List
tell application "Mail"
set newMessage to make new outgoing message with properties {visible:true, subject:"New submissions on FTP: " & dateString, content:theBody}
tell newMessage
make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
end tell
activate
send newMessage
end tell
end adding folder items to
Any help or direction would be much appreciated and hopefully others will benefit from a working solution.
For anyone who come across this post with a similar issue, you may find the answer to your problem on one of these posts.
Applescript System Events Returns .DS_Store - How can I ignore
How to I tell this Applescript to skip some files?
AppleScript : How to get files in folder without hidden files?
Hidden files start with a dot, just filter those items
on adding folder items to this_folder after receiving added_items
set added_Items_List to {}
repeat with oneItem in added_items
set fileName to name of (info for oneItem)
if fileName does not start with "." then
set end of added_Items_List to fileName
end if
end repeat
if (count added_Items_List) = 0 then return
....
But if the Finder on your machine does not show the hidden files by default you don't need the additional lines.

apple scripting with Messages to take action on URL

I'm trying to write an AppleScript for Messages such that if I get a message that contains a URL, the script automatically opens the link in my browser.
I've been able to set a script to run that sends me a notification with the properties of the message, but I've not yet been able to figure out how to take action on the URL.
Here's what I've been able to produce:
using terms from application "Messages"
on message received from theSender for theChat with theContents
display notification theContents as text ¬
with title "New Message from " & theSender
end on message received
end using terms
Here's an example that might help you:
set msg to theContents as text
set AppleScript's text item delimiters to space
set msg to msg as list
repeat with x in items of msg
if "http" is in x then
display dialog x
end if
end repeat

Use Applescript to set the "Reply-To" address

I have a registered domain, and I use hushmail as a mail provider. I'd like to send emails from Mail.app as if they were sent from my domain. The Hushmail SMTP server does not allow me to use a different "from" address than my account name, for security reasons (spam).
I found a way to have Apple mail fill the reply-to mail with a default all the time, here: http://email.about.com/od/macosxmailtips/qt/etalwaysreplyto.htm but that's too drastic for me, as I have multiple mail accounts in my mail client.
In Mail.app, I can set the "Reply to" field manually, but there is no setting in Mail.app to have that automatically filled based on the mailbox I select.
So far, I have an AppleScript which is able to create a reply on the selected mail:
tell application "Mail"
set theSelection to selection
if theSelection is {} then return
activate
repeat with thisMessage in theSelection
set theOutgoingMessage to reply thisMessage with opening window
# Wait for Mail.app to create the reply window
repeat until exists (window 1 whose name = "Re: " & subject of thisMessage)
end repeat
delay 0.1
#
# Here I want to set the reply-to address here based on the
# selected mailbox, or the "from" address of
# the current mail.
#
#
# The I need to restore the cursor to the body of the mail
# (if cursor was moved)
#
end repeat
end tell
I've looked in the AppleScript Dictionary (File -> Open Dictionary -> Mail.app -> Message -> message -> reply to), and this seems to be a property I should be able to set, but when I do something like:
tell theOutgoingMessage
make new recipient at end of reply to with properties {address:"myreplyto#example.com"}
An error pops up saying "Mail got an error: Can't get reply to of outgoing message id 65."
I also tried
tell theOutgoingMessage
set reply to to "myreplyto#example.com"
But that pops up an error saying "Mail got an error: Can’t set reply to of outgoing message id 69 to "myreplyto#example.com".
How can I set the reply-to property of the reply mail I just created?
As I mentioned in the comments to your post, you can't set the reply-to address programmatically because it's a read-only property. Therefore you need to ui script this solution.
The problem with ui scripting is that it's not an exact science. If Apple changes the positioning of the ui elements then your script will stop working. For example, I have Mail.app v6.5 and the reply-to field can be referenced using "text field 1 of scroll area 1 of window 1". In other versions of Mail.app that could be different (and probably is).
Anyway, in v6.5 of Mail.app this will do what you want using ui scripting.
tell application "Mail"
set theSelection to selection
if theSelection is {} then return
activate
repeat with thisMessage in theSelection
set replyToAddress to item 1 of (get email addresses of account of mailbox of thisMessage)
set replyToWindowName to subject of thisMessage
if replyToWindowName does not start with "Re:" then set replyToWindowName to "Re: " & replyToWindowName
-- open the reply message
set theOutgoingMessage to reply thisMessage with opening window
-- Wait for Mail.app to create the reply window
repeat until exists (window 1 whose name is replyToWindowName)
delay 0.2
end repeat
-- make sure the reply-to field is showing
my openReplyToField()
-- set the reply-to address here based on the selected mailbox
my setValueOfReplyToField(replyToAddress)
end repeat
end tell
on openReplyToField()
tell application "System Events"
tell process "Mail"
-- figure out if the reply-to text field is visible
set staticText to value of every static text of window 1
if staticText contains "Reply To:" then return
-- the reply-to field is not visible so we click the menu item
set viewMenu to menu 1 of menu bar item "View" of menu bar 1
set replyToMenuItem to first menu item of viewMenu whose name contains "Reply-To"
click replyToMenuItem
end tell
end tell
end openReplyToField
on setValueOfReplyToField(theValue)
tell application "System Events"
tell process "Mail"
set replyToField to text field 1 of scroll area 1 of window 1
set value of replyToField to theValue
end tell
end tell
end setValueOfReplyToField
Try if this works:
make new recipient at end of to recipients with properties {address:"myreplyto#example.com"}

How do I make a copy of a sent email, to edit and send again

I am using Outlook for Mac 2011 version 14.3.1 on Mac OS X 10.7.5. I want to take a sent email, make a copy, open, edit and send it again. I used to have an applescript to do this but the link to download it no longer appears to function.
I suspect it was something like this.
Select you email in the sent iTems and run the script
tell application "Microsoft Outlook"
set theMessage to item 1 of (get selection) # get the first email message
set subj to subject of theMessage
set recip to (email address of to recipient of theMessage)
set cont to content of theMessage
set newMail to make new outgoing message with properties {subject:subj, content:cont}
repeat with i from 1 to number of items in recip
set this_item to item i of recip
tell newMail to make new recipient at newMail with properties {email address:this_item}
end repeat
open newMail
end tell
The script creates a new message with ALL previous recipients,subject and content.
Then opens it ready for editing

AppleScript: Call handler inside tell statement

I get this error everytime i run this script: System Events got an error: "Test123" doesn’t understand the notify message.
Code:
--more code...
tell application "System Events"
if some_system_events_property then
my notify of "Test123" thru "Test"
end if
end tell
--more code...
to notify of message thru level
display dialog message with titel level
end notify
I have tried to replace
my notify of "Test123" thru "Test"
with the following, without any success:
notify of "Test123" thru "Test" of me
(notify of "Test123" thru "Test") of me
not exactly sure what your trying to do but here is an example of how to call a function and pass parameter
tell application "System Events"
set m to "message content"
my notify(m)
end tell
--more code...
on notify(message)
display dialog (message)
end notify
Try this:
tell application "System Events"
if some_system_events_property then
tell me to notify of "Test123" thru "Test"
end if
end tell
to notify of message thru level
display dialog message with title level
end notify
Although I'll also say that I never use the direct parameter syntax for AppleScript handlers, preferring positional parameters (i.e., notify( message, level )), as it avoids the ambiguous syntax troubles you discovered.

Resources