Skip navigation

Applescript on Mountain Lion not working

14916 Views 68 Replies Latest reply: Jul 30, 2013 2:30 AM by Nick Williams1 RSS
  • jbublik Calculating status...
    Currently Being Moderated
    Dec 6, 2012 5:33 AM (in response to Niko Nyman)

    Yes, I am running the latest 10.8

  • Terry (Rochester) Calculating status...
    Currently Being Moderated
    Dec 12, 2012 10:32 PM (in response to nohairdave)

    I'm trying to use this work around on my inbox (which is hooked to 3 POP accounts) and I 'randomly' get errors like:

     

    1. Mail got an error: Can't get date received of message id 267304 of mailbox "INBOX" of account "BTR Email".
    2. Mail got an error: Can't get every message of inbox whose deleted status of it = false and date received of it > date "Monday, December 10, 2012 5:24:28 PM"
    3. Mail got an error: Can't get content of message id 267339 of mailbox "INBOX" of account "BTR Email".
    4. When recieving errors, often I don't even get my 'Hello World' display from below which is also surprising.

     

    Any suggestions as to why I'm getting random errors would be appreciated.

     

    My script is as follows:

     

    -- See https://discussions.apple.com/thread/4186119?start=15&tstart=0 for reason why 'default mailscript sample' doesn't work

     

    tell application "Mail"

              set theSubject to ""

              set theSender to ""

              set dateReceived to ""

     

              try

                        set limitDate to current date

                        set limitDate to limitDate - 3300 * minutes

     

                        set newMessages to (every message in inbox) whose (deleted status is false and date received > limitDate) -- or read status is false

     

                        my diagnosticAlert("Hello World")

     

                        set the message_count to the count of newMessages

     

                        my diagnosticAlert((message_count as string) & " Message(s) to Process Since " & (limitDate as string) & "…")

     

                        repeat with newMessage in newMessages

                                  set theSubject to (subject of newMessage) as Unicode text

                                  set theSender to the sender of newMessage

                                  set theBody to content of newMessage

                                  set dateReceived to date received of newMessage

     

      -- Do various checks on email based on subject, sender, body and date received

      -- Based on checks, could delete email, move to archive/local folder, color email background or forward the message

     

      -- even with no 'checks' or 'processing' in code, I'm still getting errors just trying to loop messages and get local

      -- variables setup.

                        end repeat

              on error processingError

                        my diagnosticAlert("Last Sender: " & theSender & return & "Last Subject: " & theSubject & return & "Date Received: " & dateReceived & return & return & processingError)

              end try

    end tell

     

    on log_event(themessage)

              try

                        set theLine to (do shell script ¬

                                  "date  +'%Y-%m-%d %H:%M:%S'" as string) & " " & themessage

                        do shell script "echo " & quoted form of theLine & " >> ~/Library/Logs/AppleScript-events.log"

              on error logError

                        my diagnosticAlert("Log Error: " & logError & return & return & "Message: " & themessage)

              end try

    end log_event

     

    on diagnosticAlert(errstr)

              display alert "Mail Rules Diagnostics" message errstr

    end diagnosticAlert

  • Niko Nyman Calculating status...
    Currently Being Moderated
    Jan 6, 2013 3:10 AM (in response to Terry (Rochester))

    It's difficult to say why your errors are random. I can answer your 4th case though; you're not getting the "Hello World" message because the script is failing on the previous line:

                          set newMessages to (every message in inbox) whose (deleted status is false and date received > limitDate) -- or read status is false

       

      Either Applescript cannot get the messages in the inbox (case 2), or the script can get to the messages but cannot get the date of a message (case 1), or the script cannot get the contents of a message (case 3). In case 3 you are seeing the "Hello World" message, because the script is most probably failing after that line, here:

                                    set theBody to content of newMessage

    1. 2point5 Calculating status...
      Currently Being Moderated
      Feb 26, 2013 5:54 PM (in response to nohairdave)

      Some time has elapsed and I was wondering if anyone has discovered a solution to this issue?

       

      My script is designed to save an attachment to the desktop based on a rule.  The script runs but does not save the attachment as expected (used to work fine in Lion but has spat the dummy in Mountain Lion). Code added to provide some diagnostics (as suggested by twtwtw back in October) proves that the script is being executed. 

       

      Here's the code.

      (* save attachments of selected messages

      AK IT Carlow April 2006

      will crash if file already exits

      *)

      using terms from application "Mail"

         on perform mail action with messages theMessages

            try

               tell application "Finder"

                  activate

                  set theAttachmentPath to "Macintosh HD:Users:boxerflat4:Desktop:"

                 display dialog theAttachmentPath as string

                 display dialog "Message = " & theMessages as string

               end tell

               tell application "Mail"

                  repeat with ThisMessage in theMessages

                     set Attached to mail attachments of ThisMessage

                     set FileIndex to 0

                     repeat with ThisAttach in Attached

                        save ThisAttach in theAttachmentPath & (name of ThisAttach)

                     end repeat

                  end repeat

               end tell

               on error errstr

                  my diagnosticAlert(errstr)

            end try

         end perform mail action with messages

      end using terms from

       

      using terms from application "Mail"

         on run

            tell application "Mail" to set sel to selection

            tell me to perform mail action with messages (sel)

         end run

      end using terms from

       

      on diagnosticAlert(errstr)

         tell application "Finder"

            activate

            display alert "Mail Error" message errstr

         end tell

      end diagnosticAlert

       

       

      The error trap inserted provided the following:

      Mail Error

      Can't make <<class mssg>> id 171638 of <<class mbxp>> "INBOX" of <<class mact>> "boxerflat4" into type Unicode text.

       

      Any help would be sincerely appreciated.

    2. Tony T1 Level 6 Level 6 (8,115 points)
      Currently Being Moderated
      Feb 26, 2013 6:32 PM (in response to 2point5)

      The only solution is to run the applescript on the inbox as described above

    3. 2point5 Level 3 Level 3 (955 points)
      Currently Being Moderated
      Feb 26, 2013 8:04 PM (in response to Tony T1)

      Thanks Tony T1, I've implemented that approach as you've suggested though I've discovered that the Save command is where it is failing.

       

      The diagnostic is an "Mail got an error: AppleEvent handler failed".

       

      Struggling with that one unfortunately.  Not a lot of experience with Applescript sadly.

       

      Here's the code:

       

      tell application "Finder"

                set theAttachmentPath to "Macintosh HD/Users/boxerflat4/Desktop/" as string

      end tell

      tell application "Mail"

                repeat with theMessage in (every message of inbox)

                          set theSender to (sender of theMessage)

                          if theSender contains "Degabriele" then

                                    try

                                              set Attached to mail attachments of theMessage

                                              set FileIndex to 0

                                              repeat with ThisAttach in Attached

        save ThisAttach in theAttachmentPath & (name of ThisAttach)

                                              end repeat

                                    on error errstr

                                              my diagnosticAlert(errstr)

                                    end try

                          end if

                end repeat

      end tell

      on diagnosticAlert(errstr)

                tell application "Finder"

        activate

        display alert "Mail Error" message errstr

                end tell

      end diagnosticAlert

    4. twtwtw Level 5 Level 5 (4,580 points)
      Currently Being Moderated
      Feb 26, 2013 9:13 PM (in response to 2point5)

      Apple Event Handler Failed can mean either that the the save feature is not implemented, or it is implemented but throwing a runtime error.  It's literally what it says: it tried to run the appropriate method and suffered a non-fatal but applescript-ending problem.  That can only be fixed on Apple's side, so you should file a bug report. 

       

      Mail scripting has been buggy for years.  The first bug report I ever filed was about Mail.app: that was back in 2007, and it still hasn't been fixed.

      MacBook Pro (15-inch Early 2011), OS X Mountain Lion (10.8.2)
    5. 2point5 Level 3 Level 3 (955 points)
      Currently Being Moderated
      Feb 26, 2013 9:30 PM (in response to twtwtw)

      Thanks for your feedback. At least I am reasonably confident that I have coded it right.  I'll do as you suggest.

    6. Tony T1 Level 6 Level 6 (8,115 points)
      Currently Being Moderated
      Feb 27, 2013 7:16 AM (in response to 2point5)

      Here's a script that will save the email (as a txt file) and the attachment to the Desktop.

      See if this works:

       

      (*

                Set a Mail Rule to Save Message and Attachment to Desktop.

                To save attachments to another another folder on the desktop (i.e. Attachments) create the folder and then

                change tell application "Finder" to set pathToAttachments to (path to desktop folder as string) & "Attachments:"

      *)

       

      using terms from application "Mail"

                on perform mail action with messages theMessages

                          tell application "Finder" to set ptd to (path to desktop folder) as string

                          tell application "Finder" to set pathToAttachments to (path to desktop folder) as string

                          tell application "Mail"

                                    repeat with theMessage in theMessages

                                              set d_recd to date received of theMessage as string

                                              set d_recd to ReplaceText(d_recd, ":", " ") of me

                                              set d_recd to ReplaceText(d_recd, ",", " ") of me

                                              set theText to content of theMessage

                                              if theMessage's mail attachments is not {} then

                                                        repeat with theAttachment in theMessage's mail attachments

                                                                  set theFileName to pathToAttachments & (theMessage's subject) & " (Attachment From " & (theMessage's sender) & " Sent " & d_recd & ")" & space & theAttachment's name

                                                                  try

                                                                           save theAttachment in theFileName

                                                                  on error errnum

                                                                  end try

                                                        end repeat

                                              end if

                                              set theFile to ptd & (theMessage's subject) & " (From " & (theMessage's sender) & " Sent " & d_recd & ")" & ".txt"

                                              set theFileID to open for access file theFile with write permission

        write theText to theFileID

        close access theFileID

                                    end repeat

                          end tell

                end perform mail action with messages

      end using terms from

       

      on ReplaceText(theString, fString, rString)

                set current_Delimiters to text item delimiters of AppleScript

                set AppleScript's text item delimiters to fString

                set sList to every text item of theString

                set AppleScript's text item delimiters to rString

                set newString to sList as string

                set AppleScript's text item delimiters to current_Delimiters

                return newString

      end ReplaceText

    7. 2point5 Level 3 Level 3 (955 points)
      Currently Being Moderated
      Feb 27, 2013 3:22 PM (in response to Tony T1)

      Hi Tony,

       

      Thanks for that. You have been busy. 

       

      The code saves the body text of the target email to a file on the Desktop but the section of code that deals with saving the attachment:

            save theAttachment in theFileName

      unfortunately does not save the Attachment.

       

      I added in the diagnostic to see what the error was and it is the same as I was encountering with my code. "AppleEvent handler failure".

       

      At least it proves that Mail can write out a text file based on some parameters but strangely it wont save an attachment.  Why one and not the other? (rhetorical question).  Doesn't make sense.

       

      Cheers

      Chris

    8. Tony T1 Level 6 Level 6 (8,115 points)
      Currently Being Moderated
      Feb 27, 2013 3:47 PM (in response to 2point5)

      I tested it before I posted, and it worked for me. 

      Are you using any 3rd party apps (i.e. MailTags)?

    9. 2point5 Level 3 Level 3 (955 points)
      Currently Being Moderated
      Feb 27, 2013 4:15 PM (in response to Tony T1)

      No I'm not using any 3rd party stuff associated with Mail at all.

       

      Interesting that it works on your machine but not mine.

       

      Incidentally I had tried that "save" code syntax you are using too by concatenating the path and filename.  Didn't make any difference.

       

      I'm using a MacBook Pro mid 2010 i5.  Nothing out of the ordinary as far as I can tell.

       

      Updates are all done via downloading the DMG from the Apple Support site rather than allowing Software update to do the work.  I Always run Disk Utilities before and after to repair permissions.  This has got me stumped.

    10. twtwtw Level 5 Level 5 (4,580 points)
      Currently Being Moderated
      Feb 27, 2013 6:51 PM (in response to Tony T1)

      Maybe it's time to stop beating a dead horse and go get yourself a mule.  In 10.8 it looks as though attachments are automatically downloaded and stored in deep, dark folders way down in the Mail Library folder. So, just find that file and duplicate it to the desktop (I used message 2 as proof of concept, because I knew that message had attachments and I haven't put in any error checking; you'll need to adjust it to make it work in your context):

       

       

      tell application "Mail"

                set ThisMessage to message 2 of inbox

                tell ThisMessage

                          set attachName to name of first mail attachment

                          set acctDir to (account directory of account of mailbox of ThisMessage) as rich text

                end tell

      end tell

       

      with timeout of 300 seconds

                tell application "Finder"

                          set attachFile to last file of entire contents of folder acctDir whose name is attachName

        duplicate attachFile to desktop

                end tell

      end timeout

       

      You might need to use first instead of last.  I don't remember whether the Finder sorts in ascending or descenting date order.

    11. Tony T1 Level 6 Level 6 (8,115 points)
      Currently Being Moderated
      Feb 27, 2013 7:06 PM (in response to 2point5)

      This one should work:

      Copy this to ~/Library/Application Scripts/com.apple.mail/

       

      tell application "Mail"

                repeat with theMessage in (every message of inbox)

                          if read status of theMessage is false then

                                    tell application "Finder" to set ptd to (path to desktop folder) as string

                                    tell application "Finder" to set pathToAttachments to (path to desktop folder) as string

                                    set d_recd to date received of theMessage as string

                                    set d_recd to ReplaceText(d_recd, ":", " ") of me

                                    set d_recd to ReplaceText(d_recd, ",", " ") of me

                                    set theText to content of theMessage

                                    if theMessage's mail attachments is not {} then

                                              repeat with theAttachment in theMessage's mail attachments

                                                        set theFileName to pathToAttachments & (theMessage's subject) & " (Attachment From " & (theMessage's sender) & " Sent " & d_recd & ")" & space & theAttachment's name

                                                        try

                                                          save theAttachment in theFileName

                                                        on error errnum

                                                        end try

                                              end repeat

                                    end if

                                    set theFile to ptd & (theMessage's subject) & " (From " & (theMessage's sender) & " Sent " & d_recd & ")" & ".txt"

                                    set theFileID to open for access file theFile with write permission

        write theText to theFileID

        close access theFileID

      set the read status of theMessage to true

                          end if

                end repeat

      end tell

       

       

      on ReplaceText(theString, fString, rString)

                set current_Delimiters to text item delimiters of AppleScript

                set AppleScript's text item delimiters to fString

                set sList to every text item of theString

                set AppleScript's text item delimiters to rString

                set newString to sList as string

                set AppleScript's text item delimiters to current_Delimiters

                return newString

      end ReplaceText

    12. 2point5 Level 3 Level 3 (955 points)
      Currently Being Moderated
      Feb 27, 2013 7:58 PM (in response to Tony T1)

      OK I placed that in the folder you specified which is the folder I have been using all along.

       

      This version does not save the text file whereas your previous does.

       

      Placing dialog statements in the code to reveal where it fails shows that it gets through to the point where it is about to set 'theFileID to open for access'  and looks like it dies right there.

    Actions

    More Like This

    • Retrieving data ...

    Bookmarked By (1)

    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.