[X4U] Need Applescript help for Mail

Bill White billwhite at mac.com
Wed May 11 20:13:53 PDT 2005


On 5/11/05 6:25 PM, John McGibney <ensignjd at optonline.net> wrote:

> I'm trying to setup an applescript for Mail so that it will delete
> messages based on these 2 conditions.
> 
> condition one
> 1. mailbox folder is "Temp Save"
> 2. subject contains the word digest
> 3. and the date sent is one or more days old
> 
> condition two
> 1. mailbox folder is "Temp Save"
> 2. delete all messages older than 5 days
> 
> 
> Here's the script i wrote. but i can't get the line with subject to
> work.
> 
> -=-=
> 
> tell application "Mail"
>       set localMailboxes to "Temp Save"
>       if message subject contains "Digest" then
>           if date sent is not today then
>               delete message
>           end if
>       else
>           if date sent is greater than 5 then
>               delete message
>           end if
>       end if
> end tell
> 
> -=-=-=
> 
> here's the error message:
> 
> tell application "Mail"
>       get subject
>           "Mail got an error: Can't make subject into type reference."
> 
> 
> any applescript gurus out there?

Mail is a bear to script (that's one of the many reasons I use Entourage, an
actual shining example of scriptability!). But dictionary and syntax aside,
there are several problems with your script above. I can go into more detail
if you want, but in here are three:

- set localMailboxes to "Temp Save" is just assigning the value "Temp Save"
to a variable named localMailboxes, not referencing a mailbox as such

- "today" isn't an AppleScript constant, and you haven't defined what today
is

- "date sent" [of a particular message] returns a date object, including
time and day of the week, so something like "date sent greater than 5" isn't
going to work

Try something like the following, with the caveat that I've stayed away from
scripting, or even dealing with(!), Mail. I'm just going by its dictionary
and some scripting logic:

---------- BEGIN SCRIPT ----------

set today to date string of (current date)

tell application "Mail"
    
    set theseMessages to every message of mailbox "Temp Save"
    
    repeat with thisMessage in theseMessages
        
        set msgDate to date sent of thisMessage
        set msgDateString to date string of msgDate
        
        if subject of thisMessage contains "Digest" and msgDateString is not
today then -- watch line wraps; should all be on one line
            
            delete thisMessage
            
        else
            
            if (current date) - (date sent of thisMessage) is greater than
(5 * days) then delete thisMessage -- watch line wraps; should be one line
            
        end if
        
    end repeat
    
end tell

----------- END SCRIPT -----------

This isn't tested, but I think it's more along the lines of what you're
after.

-- Bill




More information about the X4U mailing list