Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applescript to delete message in a category in Outlook 2011

Context

In Outlook 2011, I have a rule that automatically sets category for incoming mail - say "Purple Category". I would like the email from this category to be automatically deleted after one week.

I would like to use AppleScript to select all the mail in my inbox that are in the "Purple Category" and older that one week and move them to the deleted folder.

Issue

The problem is I am not able to select messages in the purple category with Applescript. From the description of the Outlook Dictionary it seems that message and category are at the same level:

Below is the description of both item in the Outlook dictionary:

category n, pl categories [inh. object > item] : A category.

contained by application.


message n [inh. todoable object > categorizable object > object > item; see also Debug Suite] : An e-mail message.

contains recipients, to recipients, cc recipients, bcc recipients, attachments;

contained by application, mail folders.

As you can see both are contained by application so in AppleScript when I try:

set messagesToDelete to message in inbox whose {category "Purple Category"}

I get an error message saying:

Can’t get message of inbox of application "Microsoft Outlook" whose {category "Purple Category"}. Access not allowed.

The rest of the code:

set daysToPreserve to 7
set dateReference to (current date) - (daysToPreserve * days)
tell application "Microsoft Outlook"
set messagesToDelete to message in inbox whose {category "Purple Category"} and time received ≤ dateReference
if messagesToDelete is {} then
return
end if
permanently delete messagesToDelete
end tell
display dialog (count messagesToDelete) & " old Mail Messages Have Been Purged" as text buttons ["OK"]
like image 488
vrleboss Avatar asked Dec 20 '25 23:12

vrleboss


1 Answers

you have to loop through all the messages that have categories

tell application "Microsoft Outlook"

    repeat with afolder in mail folders
        set theMsgs to (every message of afolder where it's category is not {})

        -- loop through the messages
        repeat with aMsg in theMsgs
            -- get all the categories of the message
            set cats to categories of aMsg
            -- loop through the categories
            repeat with aCat in cats
                set catName to name of aCat
                -- check to see if this is the category of emails we want to delete
                if catName is "Purple Category" then
                    get time received of aMsg
                    set foo to date of time received of aMsg
                    -- compare dates  and delete aMsg
                    -- exit the loop so we don't error after deleting the message
                    exit repeat
                end if
            end repeat
        end repeat
    end repeat
end tell
like image 187
mcgrailm Avatar answered Dec 22 '25 21:12

mcgrailm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!