Skip navigation

Applescript - Mail - Declare to Date Found in Parse

573 Views 8 Replies Latest reply: Nov 3, 2013 1:56 PM by Pierre L. RSS
gsilver352 Calculating status...
Currently Being Moderated
Jan 29, 2013 12:53 PM

Trying to fix apple script I have for mail.

 

 

This is my question...

 

 

Do lines 58 to 95 contain a "string date" less than the current date? If so move to Old Mailbox.

 

I know there's another process I need, to somehow set another declaration to the date found.

How would I process this?

 

Currently I have:

 

 

tell (current date) to set dateForm to (text 1 thru 3 of (its month as string)) & " " & day & ", " & year

-- formats the current date like "Feb 29, 2012"

 

tell application "Mail" to set emailsSource to messages of mailbox "Brands" of account "MyStuff"

 

repeat with anEmail in emailsSource

          tell application "Mail" to set theSource to source of anEmail

          set tLines to paragraphs of theSource

          set tc to (count tLines)

          if tc > 203 then

                    set foundDate to false

                    repeat with i from 58 to 95

                              if item i of tLines contains dateForm then

                                        set foundDate to true

                                        exit repeat

                              end if

                    end repeat

 

                    if foundDate then

                              set theMailbox to "Old"

                              tell application "Mail"

                                        move the anEmail to mailbox theMailbox of account "MyStuff"

                              end tell

                    end if

          end if

end repeat

MacBook, Mac OS X (10.5.8), Tech
  • Pierre L. Level 4 Level 4 (3,860 points)

    Assuming that

    1. the date and time format specified in System Preferences is for the United States,

    2. the date searched for in your emails has the same format as "Feb 29, 2012",

    then the following script should do what you are asking for:

     

     

    tell (current date) to set dateForm to (text 1 thru 3 of (its month as string)) & " " & day & ", " & year

    -- formats the current date like "Feb 29, 2012"

     

    tell application "Mail" to set emailsSource to messages of mailbox "Brands" of account "MyStuff"

     

    repeat with anEmail in emailsSource

        tell application "Mail" to set theSource to source of anEmail

        set tLines to paragraphs of theSource

        set tc to (count tLines)

        if tc > 203 then

            set foundDate to false

            repeat with i from 58 to 95

                if getDate(item i of tLines) then

                    set foundDate to true

                    exit repeat

                end if

            end repeat

           

            if foundDate then

                set theMailbox to "Old"

                tell application "Mail"

                    move the anEmail to mailbox theMailbox of account "MyStuff"

                end tell

            end if

        end if

    end repeat

     

    on getDate(theText)

        set k to 0

        repeat with thisWord in words of theText

            set k to k + 1

            if thisWord is in ¬

                {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} then

                try

                    if (exists word (k + 1) of theText as integer) ¬

                        and (exists word (k + 2) of theText as integer) then

                        set theFoundDate to word k of theText & " " & ¬

                            word (k + 1) of theText & ", " & ¬

                            word (k + 2) of theText

                        if date theFoundDate < (current date) then return true

                    end if

                end try

            end if

        end repeat

        return false

    end getDate

  • HappyLooking Calculating status...

    Hey Pierre, not sure if you are still active in this. I am having a new problem related to this. The format of the date has changed and I cant figure out how to fix it. If you have a moment could you take a look, I would really appreciate it.

     

    Thank you,

     

    Here is the script as is now...I would like to compare dateForm to theFoundDate

     

    tell (current date) to set dateForm to (its weekday as string) & (its month as string) & " " & day & ", " & year

    --set dateForm to "Tuesday, March 6, 2012" -- for testing on your Sample Email for Script, remove this line to  matches current date

    -- formats the current date like "Tuesday, February 29, 2012"

     

    tell application "Mail" to set emailsSource to messages of mailbox "Good Tuesday" of account "HappyGoLucky45"

     

    repeat with anEmail in emailsSource

              tell application "Mail" to set theSource to source of anEmail

              set tLines to paragraphs of theSource

              set tc to (count tLines)

              if tc > 165 then

                        set foundDate to false

                        repeat with i from 120 to 190

                                  if getDate(item i of tLines) then

                                            set foundDate to true

                                            exit repeat

                                  end if

                        end repeat

     

                        if foundDate then

                                  set theMailbox to "zOld"

                                  tell application "Mail"

                                            move the anEmail to mailbox theMailbox of account "HappyGoLucky45"

                                  end tell

                        end if

              end if

    end repeat

     

     

    on getDate(theText)

              set k to 0

              repeat with thisWord in words of theText

                        set k to k + 1

                        if thisWord is in ¬

                                  {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} then

                                  try

                                            if (exists word (k + 1) of theText as integer) ¬

                                                      and (exists word (k + 2) of theText as integer) then

                                                      set theFoundDate to word k of theText & " " & ¬

                                                                word (k + 1) of theText & ", " & ¬

                                                                word (k + 2) of theText

                                                      if date theFoundDate < (date dateForm) then return true

                                            end if

                                  end try

                        end if

              end repeat

              return false

    end getDate

     

     

     

    --Below is how the email appears and the Date I am trying to compare to the Current Date (DateForm)

    --The format of the email has changed and I can't figure out how to fix it.

    --This is to move old messages to different folder

     

     

    --About this event

     

     

          

    --Tuesday, October 29, 2013 at 11:55 PM (EDT)

  • Pierre L. Level 4 Level 4 (3,860 points)

    Hi HappyLooking,

     

    First of all, there's an error in the very first line of your code. In order to insert a comma between the weekday and the month, that line should be written like this:

     

    tell (current date) to set dateForm to ¬

        (its weekday as string) & ", " & (its month as string) & " " & day & ", " & year

     

    That being said, however, there's absolutely no need to use dateForm anywhere in the script. Just replace (date dateForm) with (current date) in the handler.

     

     

    on getDate(theText)

        set k to 0

        repeat with thisWord in words of theText

            set k to k + 1

            if thisWord is in ¬

                    {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} then

                    try

                        if (exists word (k + 1) of theText as integer) ¬

                            and (exists word (k + 2) of theText as integer) then

                            set theFoundDate to word k of theText & " " & ¬

                                word (k + 1) of theText & ", " & ¬

                                word (k + 2) of theText

                            if date theFoundDate < (current date) then return true

                        end if

                    end try

                end if

            end repeat

            return false

    end getDate

     

    Hope it can help.

  • HappyLooking Level 1 Level 1 (0 points)

    Sorry I pasted that top portion incorrecly omitting the comma, but thank you for noticing.

     

    The reason I want to compare dateForm is because when it finds the month anywhere in the script within those lines it moves it. But I only want it to move it, if it finds the whole format of the date which only occurs one time within those lines, not just the month.

     

    Currently it's moving emails even after current date.

     

    So I only want it to pickup and compare if it falls into this format Tuesday, October 29, 2013 and concludes the current date is greater.

  • HappyLooking Level 1 Level 1 (0 points)

    I made the following changes so it would look for the Day of the week, however it is now returning nothing. "Tuesday, " does not exist elsewhere within the lines.

     

    I am trying to read it...I thought if the first word was the day, then it would just pickup from there if it was true.

    Also since we are dealing with an additional word that the k count would have to be increased... I'm not very expirenced, but it sounded logical to me lol

     

    on getDate(theText)

    set k to 0

    repeat with thisWord in words of theText

    set k to k + 1

    if thisWord is in ¬

    {"Monday, ", "Tuesday, ", "Wednesday, ", "Thursday, ", "Friday, ", "Saturday, ", "Sunday, "}then

    try

    if (exists word (k + 1) of theText as integer) ¬

    and (exists word (k + 2) of theText as integer) ¬

    and (exists word (k + 3) of theText as integer) then

    set theFoundDate to word k of theText & ¬

    word (k + 1) of theText & " " & ¬

    word (k + 2) of theText & ", " & ¬

    word (k + 3) of theText

    if date theFoundDate < (current date) then return true

    end if

    end try

    end if

    end repeat

    return false

    end getDate

  • Pierre L. Level 4 Level 4 (3,860 points)

    Try this:

     

    on getDate(theText)

        set k to 0

        repeat with thisWord in words of theText

            set k to k + 1

            if thisWord is in {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} then

               try

                    if word (k + 1) of theText is in {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} ¬

                        and (exists word (k + 2) of theText as integer) ¬

                        and (exists word (k + 3) of theText as integer) then

                        set theFoundDate to word (k + 1) of theText & " " & ¬

                            word (k + 2) of theText & ", " & ¬

                            word (k + 3) of theText

                        if date theFoundDate < (current date) then return true

                    end if

                end try

            end if

        end repeat

        return false

    end getDate

     

    Message was edited by: Pierre L. (layout)

  • Pierre L. Level 4 Level 4 (3,860 points)

    It was my pleasure. Thanks for your feedback.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.