1 2 3 4 5 Previous Next 68 Replies Latest reply: Jul 30, 2013 2:30 AM by Nick Williams1 Go to original post
  • 45. Re: Applescript on Mountain Lion not working
    2point5 Level 3 Level 3 (955 points)

    Hahaha. I might have to give this a go if all else fails but it might take me a while to understand what you are doing in the code so that I can make sense of it and wrap it up in some other code.

  • 46. Re: Applescript on Mountain Lion not working
    2point5 Level 3 Level 3 (955 points)

    Tony,

     

    Update:

     

    I changed the code so that it will step through messages looking for one from a specific person.  Having achieved that the code steps into the Save Attachment section but still refuses to save the attachment.  Handling the error that results - "AppleEvent handler failure" - means that it steps through and creates the text file as before.

  • 47. Re: Applescript on Mountain Lion not working
    Tony T1 Level 6 Level 6 (8,380 points)

    Hmmm..... Does this also happen if you apply the rule manually (⌥⌘L)  (to test this, you need to mark the message as unread, or comment out the code that tests if the email in unread).

     

    My only other thought is to create a new user (for testing purposes), and see if the error occurs there.

  • 48. Re: Applescript on Mountain Lion not working
    2point5 Level 3 Level 3 (955 points)

    That's the method I've been using to test it all along...... well, via the menu as in Message > Apply Rules..... which is the same as (⌥⌘L).

     

    Since I know that the rule works, insofar as when new mail arrives it triggers that rule when it finds an email has arrived from a specific person, I have en email in the inbox at this time that is marked unread which has been the target for testing.

     

    The code, as it stands, looks at every email in the inbox starting from the most recent working back to the oldest.  I initially wanted to find out if it was looking at each message so set up a counter and displayed a dialog box for each message the code scanned including the Sender details.  I've changed the code since then as I am only interested in messages from a specific person.

     

    It only gets a little busier when the Sender matches my test

    if theSender contains "person_of_interest" then

    blah blah blah.

     

    Current code:

     

    (*

              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:"

    *)

     

    set MsgNum to 1

     

    tell application "Mail"

              repeat with theMessage in (every message of inbox)

                        set theSender to (sender of theMessage)

                        if theSender contains "person_of_interest" then

      (* Diagnostic statement *)

                                  tell application "Finder" to display dialog "Sender is " & theSender & " for Message " & MsgNum as string

                                  try

                                            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

                                            if theMessage's mail attachments is not {} then

                                                      tell application "Finder" to display dialog "Dealing with Attachment of Message " & MsgNum as string

                                                      repeat with theAttachment in theMessage's mail attachments

                                                                set theFileName to pathToAttachments & "MembershipLatest.xls"

                                                                try

      (* Diagnostic statement *)

                                                                          tell application "Finder" to display dialog theFileName as string

                                       save theAttachment in theFileName

      (* Diagnostic statement *)

                                                                          tell application "Finder" to display dialog "After the Save attachment instruction" as string

                                                                on error errnum

                                                                          my diagnosticAlert(errnum)

                                                                end try

                                                      end repeat

                                            end if

                                  on error errnum

                                            my diagnosticAlert(errnum)

                                  end try

                        end if

                        set theSender to ""

                        set MsgNum to MsgNum + 1

              end repeat

    end tell

     

    on diagnosticAlert(errstr)

              tell application "Finder"

         activate

         display alert "Mail Error" message errstr

              end tell

    end diagnosticAlert

  • 49. Re: Applescript on Mountain Lion not working
    2point5 Level 3 Level 3 (955 points)

    OK twtwtw, I've resorted to using your suggested script and it works. 

     

    The only issue is that Finder hangs (wheel of death spinning) until the file is copied to the Desktop.  It took about a minute and 15 seconds to complete. It finds the most recent version which is exactly what I need so I am assuming that Finder searches through the folders looking for any file so named until it find the most recent one. "Last" seems to be the right option.

     

    Thanks for your input and assistance.

  • 50. Re: Applescript on Mountain Lion not working
    twtwtw Level 5 Level 5 (4,690 points)

    Yeah, the Finder can be super-slow on content searches.  You can use the following instead, which pulls the data from mdfind. This will be much faster, but it's more challenging technically, and it raises some new issues.  for example, the following looks for an attachment that was created within the last five minutes; if two attachments of the same name show up in that time frame, it's not clear which mdfind will find first.

     

    tell application "Mail"

      -- ...

              tell ThisMessage

                        set attachName to name of first mail attachment

                        set acctDir to (account directory of account of mailbox of ThisMessage)

              end tell

    end tell

     

    set acctDirPosix to POSIX path of acctDir

    set search to (do shell script "mdfind 'kMDItemFSName == " & attachName & "' -onlyin " & acctDirPosix)

    set attachFileAlias to POSIX file (first paragraph of search)

     

    tell application "Finder"

      duplicate attachFileAlias to desktop

    end tell


    I should point out one other quibble here.  If you download the same file more than once (e.g. someone sends you a document, and then later in the day sends you a revised version of the document with the same name), the second one will be renamed with with a numeric suffix so that only the first will be found.  there are workaround for that, but I don't know if it's enough of an issue to bother with..  

  • 51. Re: Applescript on Mountain Lion not working
    2point5 Level 3 Level 3 (955 points)

    This is getting very interesting.

     

    Can that shell command be this?

    set search to (do shell script "mdfind 'kMDItemFSName == " & attachName & "' -onlyin " & acctDirPosix & "' grep INBOX.mbox | grep Attachments")

     

    I've tried the shell command via terminal and the result is pretty much instantaneous from the POP-username folder (which would be the acctDirPosix).

     

    I haven't incorporated this code as yet. Just asking the question about the shell command first before I make a start on it.

     

    It's unlikely that the person I am monitoring emails from is likely to send another update in the same day, but should that occur I would deal with that at the time and manually extract the latest document from the email.  It's not a deal breaker.

  • 52. Re: Applescript on Mountain Lion not working
    twtwtw Level 5 Level 5 (4,690 points)

    Well, first thing is that I gave you an incomplete command above: I left off the 5-minute restriction.  the complete command looks like so:

     

    set search to (do shell script "mdfind 'kMDItemFSName == " & attachName & " && kMDItemFSCreationDate >= $time.now(-300)' -onlyin " & acctDirPosix)

     

    with respect to the grep bit, I'm not sure what that's supposed to accomplish.  Are you just trying to restrict the result set?  if that's the case, there's a trickier way to do it.  Mail puts the massage and its attachments in a rat's nest of sub-folders, but it's a systematic rat's nest.  it stores message components in folders that are titled with the id number of the message.  So you can do something like this:

     

    tell application "Mail"

      -- ...

              tell ThisMessage

                        set attachName to name of first mail attachment

                        set messageID to its id

                        set acctDir to (account directory of account of its mailbox)

              end tell

    end tell

     

    set acctDirPosix to POSIX path of acctDir

    set search to (do shell script "mdfind 'kMDItemFSName == " & attachName & "' -onlyin " & acctDirPosix & " | grep Attachments/" & messageID)

     

    That works because the message's attachments will all be stored in a folder with a path like:

     

    ~/Library/Mail/V2/IMAP-account@server.com/INBOX.mbox/0D36517B-5FE4-4425-A252-AE71A23A812A/Data/6/7/3/Attachments/376485/. ..

     

    where the 376485 bit will be the id of the message.  Careful, you want to be sure to use the id property and not the message id property.  It's easy to get them confused, but the latter is not much use for this purpose.

  • 53. Re: Applescript on Mountain Lion not working
    2point5 Level 3 Level 3 (955 points)

    Ah the reason I was using grep like that was to limit the result, as you say, to just the INBOX as that's where the message I am interested in will be.  I'm a bit anal when it comes to mail and I put things into folders elsewhere to keep some sort of order in the house so that means that anything I have dealt with is no longer in the INBOX.

     

    I noticed that in Terminal when I ran the mdfind command from the /V2/POP-..... folder it found the match in the INBOX as the first two entries (one message and one attachment) and then found every other match from 1066 so I figured grep would narrow the focus right down. Yeah it sure is a rats nest but I guess whoever dreamed up the rats nest knew what they were doing..... I hope.

     

    Your suggestion to use the message ID is perfect so I'll give that a go.

     

    Given that it now narrows down the search to a specific message ID is it still necessary to use the timeout as the action ought to be very quick?

     

    Thanks heaps for your help.  I hope others reading along are finding this useful too.

  • 54. Re: Applescript on Mountain Lion not working
    twtwtw Level 5 Level 5 (4,690 points)

    2point5 wrote:

     

    Given that it now narrows down the search to a specific message ID is it still necessary to use the timeout as the action ought to be very quick?

     

    Thanks heaps for your help.  I hope others reading along are finding this useful too.

     

    you can get rid of the timeout block.  it won't hurt anything, but it shouldn't be needed.

     

    and you're welcome. 

  • 55. Re: Applescript on Mountain Lion not working
    2point5 Level 3 Level 3 (955 points)

    I had to mess around with the shell script code that finds the attachment and then I had to create another shell script call to copy the file to the Desktop.  The POSIX stuff didn't want to play the game (not that I understand what it is doing).

     

    Here's what I ended up with and it works a treat.

     

                        set mySearch to (do shell script "mdfind '" & s_attachName & "' -onlyin ~Library/MAILV2/POP-account @ server.com" & " | grep Attachments/" & s_messageID)

                        set desktopPath to "~/Desktop/"

                        set myResult to (do shell script "cp '" & mySearch & "' " & desktopPath)

     

    It's not transportable from one account to another but I can live with that until I understand a little more about Applescript.  It's been a baptism of fire.

     

    I'm a happy lad.  It all works nicely and I have learnt so much for which I am very grateful.

  • 56. Re: Applescript on Mountain Lion not working
    Tony T1 Level 6 Level 6 (8,380 points)

    ...and all this because Apple won't address the bug!

     

    Anyway, good to hear that you found a work-around.

  • 57. Re: Applescript on Mountain Lion not working
    twtwtw Level 5 Level 5 (4,690 points)

    It's easy enough to make transportable.  That was the intention behind the account directory of account of its mailbox part of the script, which tries to get the proper folder from the message itself. Look at that when you get around to playing with it.  Oh, and as for the Finder and POSIX paths - convert them to aliases before you send them to be used by the FInder.  the syntax is set HFSPath to POSIX file "/.../.../...".  But as long as you're happy...

  • 58. Re: Applescript on Mountain Lion not working
    2point5 Level 3 Level 3 (955 points)

    Tony,

    I agree with you and I submitted a bug report too.

     

    twtwtw

    I'll have a look at that option and see if I can fix it.

     

    Oh.... does anyone know of a really good Applescript reference book that defines what all the commands do and also provides some examples?  If it has an ISBN number it should be easy enough to source if there isn't a copy locally.

     

    Cheers.

  • 59. Re: Applescript on Mountain Lion not working
    baltwo Level 9 Level 9 (60,120 points)

    2point5 wrote:

    Oh.... does anyone know of a really good Applescript reference book that defines what all the commands do and also provides some examples?

    AppleScript 1-2-3, by Soghoian & Cheeseman would be your best bet. The others I know about are a bit dated.