Get e-mail address of selected mailboxes - macos

I am trying to receive the mail addresse of the account that is active in mail.
So far I did the following
tell application "Mail"
set selected to selected mailboxes of message viewer 1
end tell
and
tell application "Mail"
email addresses of every account
end tell
But somehow i am not able do somethin like
tell application "Mail"
set selectedMailboxes to selection
set theAccount to account of selection
return theAccount
end tell
The Last applescript does not work. How can I retrieve the account of the mailbox in message vieview 1?

Try this
tell application "Mail"
try
set selectedMailbox to item 1 of (get selected mailboxes of message viewer 1)
set mailAddresses to email addresses of account of selectedMailbox
on error
display dialog "No Mailbox selected"
end try
end tell
As an account can have multiple mail addresses, the class of the variable mailAddresses is a list even there is only one address.
Get the address you want with an index number (1-based)
set myAddress to item 1 of mailAddresses
or
set myAddress to second item of mailAddresses

Related

How to set the out of office autoreply with applescript?

Trying to figure out how to use Applescript to toggle the Outlook autoreply option from off/on and vice versa.
I can query the status with
tell application "Microsoft Outlook"
query autoreply data exchange account 1
end tell
This works fine (although it returns the entire property list, and I only need the enabled element). The bigger problem is that I'm just not sure how to set the parameters with set autoreply data, as I'm not sure of the formatting of the command.
Appears in the dictionary as
set autoreply data v
set autoreply data exchange account
new data autoreply data
with the parameters of:
autoreply data n
properties
enabled (boolean) : Whether autoreplies are sent
I got this to work by getting the entire property list into a variable, toggling the enabled field, and then sending the property list back to Outlook.
tell application "Microsoft Outlook"
set theResult to query autoreply data exchange account 0
if enabled of theResult is false then
set enabled of theResult to true
else
set enabled of theResult to false
end if
set autoreply data exchange account 0 new data theResult
end tell

Last sent e-mail from Mail.app with Applescript

Is there any way to get the last sent e-mail from Mail?
It can only access the top level sent mailbox.
tell application "Mail"
set mabo to the sent mailbox
set selected to messages of mabo
return properties of item 1 of selected
end tell
The reason I need this is that I want to get information about the e-mail that is beeing written. My thought was to get the frontmost window.
tell application "Mail"
set the win to windows
set theMessage to item 1 of win
get name of theMessage
end tell
It is working for the name but I do not get the sender or receipient. Any ideas?
As said, you just have to loop through each account, and each time, compare the date of the email with previous email date from other account.
here is the script : (I also add a test in case the sent box of one account is empty !)
tell application "Mail"
set LastDate to date "samedi 1 janvier 2000 00:00:00"
set {myLastMail, myAccount, myDate} to {"", "", LastDate}
repeat with CAccount in every account
if (count of every message in mailbox "Sent Messages" of CAccount) > 0 then
set X to first message of mailbox "Sent Messages" of CAccount
set MDate to date sent of X
if MDate > LastDate then
set LastDate to MDate
set {myLastMail, myAccount, myDate} to {X, CAccount, LastDate}
end if
end if
end repeat
-- the last email from all your accounts is the variables Mylastemail, in account MyAccount and at the date MyDate
end tell
Unfortunately the messages in the sent mailbox are unsorted in the AppleScript array.
You can use this code, but it could take a while depending on the number of messages in the mailbox (ca. 30 sec for 1000 messages).
tell application "Mail"
set sentMessages to messages of sent mailbox
set lastestMessage to item 1 of sentMessages
set latestDate to date received of lastestMessage
repeat with aMessage in sentMessages
if date received of contents of aMessage comes after latestDate then
copy contents of aMessage to lastestMessage
set latestDate to date received of lastestMessage
end if
end repeat
tell lastestMessage
set theSender to sender
set theRecipient to 1st recipient
end tell
end tell
The code bellow uses to work for me in all my Mail versions up to 4.6 (Snow Leopard). To be tested on later versions. Strangely, "last message" gives the oldest message in the mailbox and the "first message" gives the last message sent or received ! if you have many accounts, just loop on accounts names :
tell application "Mail"
-- to get the last message sent
set X to first message of mailbox "Sent Messages" of account "your_account_name"
-- to get the oldest message received in receiving box
set X to last message of mailbox "INBOX" of account "your_account_name"
open X --if want to see it
end tell

Applescript Purge Email Older than 2 Days

I am new to Applescript. I have one specific email account that exists solely for the purpose receiving error reports with an attached image. The mailbox can fill up quickly.
I'd like to be able to run a script that will delete mail older than two days, so I tried my hand at the following script.
I'd like to correct what I have written so I can learn from my mistakes rather that use a different method. Looking for some constructive criticism:
set daysToPreserve to 2
tell application "Mail"
activate
set mailboxList to mailbox "INBOX" of account “MyAccount"
repeat with theCurrentMailbox in mailboxList
set emailList to (every message of (mailbox theCurrentMailbox of account “MyAccount") whose date received is less than or equal to ((current date) - daysToPreserve * days))
if (count mailboxList) is greater than 0 then
move mailboxList to mailbox "Trash" of account “MyAccount"
end if
end repeat
end tell
display dialog "Old Mail Messages Have Been Purged" buttons ["OK"]
Your edit worked great. I edited your script to bring the dialog to the front.
set daysToPreserve to 2
set myAcount to "MyAccount"
set dateReference to (current date) - (daysToPreserve * days)
tell application "Mail"
activate
set myMailbox to mailbox "INBOX" of account myAcount
set accountTrash to mailbox "Trash" of account myAcount
set messagesToDelete to messages of myMailbox whose date received ≤ dateReference
repeat with aMessage in messagesToDelete
move aMessage to accountTrash
end repeat
end tell
tell current application
activate
display dialog (count messagesToDelete) & " old Mail Messages Have Been Purged" as text buttons ["OK"]
end tell
You put 1 item into a repeat block with this:
set mailboxList to mailbox "INBOX" of account “MyAccount"
repeat with theCurrentMailbox in mailboxList
You can try something like this:
set daysToPreserve to 2
set myAcount to "MyAccount"
set dateReference to (current date) - (daysToPreserve * days)
tell application "Mail"
activate
set myMailbox to mailbox "INBOX" of account myAcount
set accountTrash to mailbox "Trash" of account myAcount
set messagesToDelete to messages of myMailbox whose date received ≤ dateReference
repeat with aMessage in messagesToDelete
move aMessage to accountTrash
end repeat
end tell
display dialog (count messagesToDelete) & " old Mail Messages Have Been Purged" as text buttons ["OK"]
this version tests number of versions to move before continuing
#Applescript Purge Email Older than 2 Days
set daysToPreserve to 7
set myAccount to "MyEmail Account"
set dateReference to (current date) - (daysToPreserve * days)
tell application "Mail"
activate
set myMailbox to mailbox "INBOX/7 days" of account myAccount
set accountTrash to mailbox "Trash" of account myAccount
set messagesToDelete to messages of myMailbox whose date received ≤ dateReference
if (count messagesToDelete) as number = 0 then
return
end if
repeat with aMessage in messagesToDelete
move aMessage to accountTrash
end repeat
end tell
tell current application
activate
display notification (count messagesToDelete) & " old messages have been purged from folder 7 days" as text
delay 1
end tell

How to create list of Mail messages to move together rather than moving them individually?

I've managed to make this AppleScript work. It basically searches my inboxes for all my accounts and then selects any messages from specific address that are older than 14 days. It then proceeds to move each one of those filtered messages to a specified mailbox.
set ExcludeList to {"Trash", "Sent", "Drafts", "Deleted Messages", "Archive", "Junk", "Notes"} -- mailboxes you don't want to search
set SenderList to {"events#goldstar.com", "hello#touchofmodern.com", "staples#e.staples.com", "deals#livingsocial.com"} -- email addresses of senders you want to remove old emails for
set DestinationFolderName to "Old_Newsletters" -- mailbox to move messages to. If you want to just delete them, leave it blank.
set StaleTime to 14 -- days old the message must be before moved or deleted
set ShowMailboxesProgress to true -- determines if you want the "Processing" box displayed for each mailbox
set current_date to current date
set _msgs_to_move to {}
tell application "Mail"
set everyAccount to every account where enabled is true
-- Get acount-specific mailboxes
repeat with eachAccount in everyAccount
set accountName to the name of eachAccount
set currentMailbox to mailbox "INBOX" of eachAccount
set mailboxName to the name of currentMailbox
if mailboxName is not in ExcludeList then
if ShowMailboxesProgress then
display dialog "Processing folder " & mailboxName & " in account " & accountName
end if
try
repeat with SenderToRemove in SenderList
set messages_list to (every message of currentMailbox whose sender ends with "<" & SenderToRemove & ">")
repeat with i from 1 to number of items in messages_list
set theMessage to item i of messages_list
set difference to ((current_date) - (date sent of theMessage)) div days
if difference is greater than StaleTime then
if DestinationFolderName is not equal to "" then
move theMessage to mailbox DestinationFolderName of account "BlueStar Studios"
else
delete theMessage
end if
end if
end repeat
end repeat
end try
end if
end repeat
display dialog "Finished!"
end tell
It seems to work nice. HOWEVER, it takes a long time to run. Because it moves each filtered message individually. Is there a way to make a list of messages to be moved while in the repeat and then move that entire list of messages to another folder in one go?
Also, I'm running on 10.7.5 if that makes any difference.
Try using a filter reference form like this:
set d to (current date) - 14 * days
tell application "Mail"
repeat with a in (get accounts where enabled is true)
move (messages of mailbox "INBOX" of account a where date sent < d and (sender ends with "<events#goldstar.com>" or sender ends with "<hello#touchofmodern.com>")) to mailbox "Old_Newsletters" of account "BlueStar Studios"
end repeat
end tell

Applescript Moving Email to Trash, But Not Deleting from the Server

I wrote an AppleScript that moves selected emails to the Trash. The script works fine in that regard. The problem is that the emails remain on the server. Is there additional code I need to add to get it to do that? Here's the code:
using terms from application "Mail"
on perform mail action with messages these_messages for rule this_rule
tell application "Mail"
set the message_count to the count of these_messages
repeat with i from message_count to 1 by -1
set this_message to item i of these_messages
set this_content to (every character of content of this_message) as Unicode text
if "bowles" is not in this_content and "patton" is not in this_content then
set theAccount to account of mailbox of this_message
set mailbox of this_message to mailbox "Trash" of theAccount
end if
end repeat
end tell
end perform mail action with messages
end using terms from
It is hard to troubleshoot without seeing the entire script and the other rules that may be causing a problem. Try adding the Stop evaluating rules action to your rule :
using terms from application "Mail"
on perform mail action with messages these_messages for rule this_rule
tell application "Mail"
repeat with this_message in these_messages
set this_content to content of this_message
if "bowles" is not in this_content and "patton" is not in this_content then
set theAccount to account of mailbox of this_message
set junk mail status of this_message to false
set mailbox of this_message to mailbox "Trash" of theAccount
end if
end repeat
end tell
end perform mail action with messages
end using terms from
Sadly the answer appears to be to create a separate rule for email source. In the end, I just unsubscribed from the mailing lists as I hadn't seen any useful information from them in years.

Resources