Move mail attachment to specified folder - problem

Hi,
I am busy making a script that downloads the mail attachments to a specified folder so that Hazel can work further on the files.
Here is what i am trying to achive:
Only Jpg, Tiff, Doc, Docx, mp3's and PDF's are moved to a folder called: Mail Attachments in the downloads folder and renamed with a prefix to the the date email was send. For Hazel to work correctly i also want to add the senders headers to the spotlight comment.
So far all went well and i got a script working that does all these things.
Only problem that is occuring now that images are duplicate twice in the folder:
first file as i want: i.e. 20130711_RTP_1846.jpg and the second file is only named 2013 with no extension aswell.
This happens only with the images, not with for example an PDF.
Here is my script, by the way this is my first script and i copied, paste things i found online tweaking it to my wishes.
using terms from application "Mail"
          on perform mail action with messages theMessages for rule theRule
  -- set up the attachment folder path
                    tell application "Finder"
                              set folderName to "Downloads:Mail Attachments"
                              set homePath to (path to home folder as text) as text
                              set attachmentsFolder to (homePath & folderName) as text
                    end tell
                    tell application "Mail"
                              repeat with eachMessage in theMessages
  -- set up the folder name for this mail message's attachments. We use the time stamp of the date received time stamp
                                        set {year:y, month:m, day:d, hours:h, minutes:min} to eachMessage's date received
                                        set timeStamp to (y & my pad(m as integer) & my pad(d)) as string
  -- get all headers from email and store in _Comment
                                        set _sel to get selection
                                        set _links to {}
                                        repeat with _msg in _sel
                                                  set _messageURL to _msg's all headers
                                                  set end of _links to _messageURL
                                        end repeat
                                        set AppleScript's text item delimiters to return
                                        set _Comment to (_links as rich text)
                                        try
  -- Save the attachment
                                                  repeat with theAttachment in eachMessage's mail attachments
                                                            set originalName to timeStamp & "_" & name of theAttachment
  --set originalName to name of theAttachment
                                                            set savePath to attachmentsFolder & ":" & originalName
                                                            try
  --save theAttachment in file (savePath) for only these files all other are excluded
                                                                      if originalName contains "jpg" then save theAttachment in file (savePath)
                                                                      if originalName contains "jpeg" then save theAttachment in file (savePath)
                                                                      if originalName contains "tif" then save theAttachment in file (savePath)
                                                                      if originalName contains "tiff" then save theAttachment in file (savePath)
                                                                      if originalName contains "pdf" then save theAttachment in file (savePath)
                                                                      if originalName contains "doc" then save theAttachment in file (savePath)
                                                                      if originalName contains "docx" then save theAttachment in file (savePath)
                                                                      if originalName contains "mp3" then save theAttachment in file (savePath)
  -- insert _Comment in spotlight search
                                                                      tell application "Finder"
                                                                                set theFile to savePath
                                                                                set the comment of file theFile to _Comment
                                                                      end tell
                                                            end try
                                                  end repeat
                                        on error msg
  display dialog msg
                                        end try
                              end repeat
                    end tell
          end perform mail action with messages
end using terms from
on pad(n)
          return text -2 thru -1 of ("0" & n)
end pad
What strange is, that if i take out the part where the comment is filled with the header info, the script works fine producing only 1 file, but when i added this part of the script, i have the two files!
Script is activated by a mail rule when mail has an attachment.
Here a screenshot with the two images:
Hope someone can help me, to tell me what i did wrong.
Grt,
Rob

The problem is subtle, but noticable in your screenshot (that was a stroke of genius).
Look closely at your screenshot. Note the file details below the image and you'll see the Name label:
"2013
07
11_130711RTP_1846.jpg"
Do you see the issue? It's not that the file is called '2013', it's called the entire expected file name, but with returns between the date elements. The problem is that return characters don't show in list view.
So why are the returns there?
That's because in the section of code where you build the comments you include the line:
                                        set AppleScript's text item delimiters to return
Then you build the file name:
                                                            set originalName to timeStamp & "_" & name oftheAttachment
Now the file name is technically a list of components (think of it as {originalName, timeStamp, "_", 130711RTP_1846.jpg} )
When this list is coerced back to a text object, AppleScript inserts the current text item delimiters between each element, which results in your multi-line filename.
The solution is actually a best practice when dealing with text item delimiters - always, always, always reset them when you're done.
In this case you only want to have returns in the comment, so you should restore the TIDs after you've built the comment:
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set _Comment to (_links as rich text)
set AppleScript's text item delimiters to tid
Now the TIDs will be set back to the default and you won't have the extraneous returns in the file name.

Similar Messages

  • How do I move mails to a local folder

    I have created local folders for my work but cannot find out how to move mails into it

    Do you mean the "Local Folders" account in Thunderbird?
    Select what you want to move. Use sorting, filtering and ctrl+click, shift+click etc to select multiple messages.
    Then you can right-click and select "Move to" or "Copy to".
    Or you can drag-and-drop them directly onto the target folder.
    Note that the target folder must exist before you can move things into it; Thunderbird doesn't let you create a folder on the fly.
    You can automate the process by use of Message Filters.
    http://en.flossmanuals.net/thunderbird/filters/
    If your "local folders" are outside Thunderbird, you may be able to drag and drop messages onto them. Otherwise, you can use '''File|Save As'''. You can save "as-is", as a .eml file, or choose HTML format. Other options are available in this add-on:
    https://nic-nac-project.org/~kaosmos/mboximport-en.html

  • How can I move mail from the offline folder inbox to a local folder?

    I have an IMAP account and my provider lost all but the last week of my emails. I still have them in the offline folder in Thunderbird so as long as I stay offline I can see the emails but I want to move them to local folders so I can go back online and sync the account without losing them all. How can I do this?

    Ah! You are BRILLIANT!! Thank you so very very very much. Your solution worked PERFECTLY! I now have all the messages I had lost aside from the past two months as the only computer I still had the offline messages was two month out of date BUT losing two months is far better than losing 5 years. You are my new personal HERO! Honestly, I can't thank you enough. This was nightmare and you solved it simply, beautifully, and completely.

  • Problems with specifying File Extension of E-Mail attachment

    Hi everybody
    I have a problem that seems to be so simple that it almost drives me crazy that I cannot find a solution for it.
    I need to send the content of an internal table (some lines) as a mail attachment to a specified mail address.
    The format should be normal ASCII, so when I open it with any text editor (e.g. Notepad),  I want to have all my lines there (including CR/LF).
    The problem now is - The extension of this mail attachment must be ".key", so the attached file should have the name "L_12345.key" (for example).
    For E-Mailing I use FM "SO_NEW_DOCUMENT_ATT_SEND_API1".
    This part works fine, also the attachment part works in general.  (I receive the mail and I receive the attachment).
    BUT I get either an attachment with the name "L_12345.txt" or "L_12345.key.txt" and in correct format, or I get an attachment "L_12345.key", where the name is correct but format is wrong (no CR/LF - everything in one line).
    Example:
    =======
    Attachment-Content should be:
    Line 1
    Line 2
    Line 3
    Name of Attachment should be: L_12345.key
    What I tried:
    TEST 1 **
    Declare Attachment in Packing list. ..
      ls_packing_list-transf_bin   = ' '.
      ls_packing_list-head_start   = 1.
      ls_packing_list-head_num     = 0.
      ls_packing_list-body_start   = 7.  "Startline of Attachment
      ls_packing_list-body_num     = 3.  "No of Attachment lines
      ls_packing_list-doc_type     = 'RAW'. 
      ls_packing_list-doc_size     = 3 * 255.
      ls_packing_list-obj_name     = 'ATT01'.
      ls_packing_list-obj_descr    = 'L_12345'   "Attachment name
      APPEND ls_packing_list TO lt_packing_list.
    ==> Result: Attachment Filename = L_12345.txt
    ==> File as I need it (ASCII with CR/LF), but extension wrong
        Line 1
        Line 2
        Line 3
    TEST 2 **
    Same as TEST 1, but with:
      ls_packing_list-obj_descr    = 'L_12345.KEY'   "Attachment name
    ==> Result: Attachment Filename = L_12345.KEY.txt
    ==> File as I need it (ASCII with CR/LF), but extension wrong
        Line 1
        Line 2
        Line 3
    TEST 3 **
    Declare Attachment in Packing list. ..
      ls_packing_list-transf_bin   = ' '.
      ls_packing_list-head_start   = 1.
      ls_packing_list-head_num     = 0.
      ls_packing_list-body_start   = 7.  "Startline of Attachment
      ls_packing_list-body_num     = 3.  "No of Attachment lines
      ls_packing_list-doc_type     = 'KEY'.
      ls_packing_list-doc_size     = 3 * 255.
      ls_packing_list-obj_name     = 'ATT01'.
      ls_packing_list-obj_descr    = 'L_12345'. "Attachment name
      APPEND ls_packing_list TO lt_packing_list.
    ==> Result: Attachment Filename = L_12345.KEY
    ==> File Format is wrong:
        Line 1          Line 2     Line3
    I have tried several other Extensions (DOC_TYPE), it seems, that the File format is only correct with "RAW", but it seems with "RAW" SAP adds ".txt" as extension to the filename.
    Does anyone know how I can achieve having an e-mail attachment with multiple text lines and name "<anyname>.key"
    Any help (if possible with example code ?) would be highly appreciated.
    Many thanks
    Harald

    hi,
    check the following code.
    *&      Form  send_email
    FORM send_email.
    CLASS cl_abap_char_utilities DEFINITION LOAD.
    CONSTANTS: con_cret TYPE c VALUE cl_abap_char_utilities=>cr_lf.
    *Body of mail
      CLEAR objtxt.
      CLEAR objtxt[].
      objtxt = 'Dear abc'.
      APPEND objtxt.
      CLEAR objtxt.
      APPEND objtxt.
      objtxt = 'Attached herewith is the detail list of materials.'.
      APPEND objtxt.
      CLEAR objtxt.
      APPEND objtxt.
      objtxt = 'Regards,'.
      APPEND objtxt.
      objtxt = 'Admin'.
      APPEND objtxt.
      DESCRIBE TABLE objtxt LINES tab_lines.
      READ TABLE objtxt INDEX tab_lines.
    *Mail description
      CLEAR docdata.
      docdata-doc_size  = ( tab_lines - 1 ) * 255 + STRLEN( objtxt ).
      docdata-obj_name  = 'List'.
      docdata-obj_descr = 'List of materials'.
      docdata-obj_langu = sy-langu.
    *Packing list for main document
      CLEAR objpack.
      CLEAR objpack[].
      objpack-head_start = 1.
      objpack-head_num   = 0.
      objpack-body_start = 1.
      objpack-body_num   = tab_lines.
      objpack-doc_type   = 'RAW'.
      APPEND objpack.
    *Attachment data
      CLEAR objbin.
      CLEAR objbin[].
      CONCATENATE
            'field1' 'field2' 'field3' 'field4'
        INTO objbin SEPARATED BY ','.
      CONCATENATE con_cret objbin INTO objbin.
      APPEND objbin.
      LOOP AT it_material_list INTO wa_material_list.
        MOVE-CORRESPONDING wa_material_list TO wa_char_material_list.
        CONCATENATE
              wa_char_material_list-field1
              wa_char_material_list-field2
              wa_char_material_list-field3
              wa_char_material_list-field4
          INTO objbin SEPARATED BY con_tab.
        CONCATENATE con_cret objbin INTO objbin.
        APPEND objbin.
        CLEAR objbin.
        CLEAR wa_char_employee_list.
      ENDLOOP.
      DESCRIBE TABLE objbin LINES tab_lines.
      READ TABLE objbin INDEX tab_lines.
    *Packing list for attachment document
      CLEAR objpack.
      objpack-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( objbin ).
      objpack-transf_bin = 'X'.          "check these values
      objpack-head_start = 1.          "check these values
      objpack-head_num   = 1.          "check these values
      objpack-body_start = 1.          "check these values
      objpack-body_num   = tab_lines.
      objpack-doc_type   = 'KEY'.
      objpack-obj_name   = 'Attachment'.
      objpack-obj_descr  = 'Report.KEY'.
      APPEND objpack.
    *Email receiver's list
      CLEAR reclist.
      CLEAR reclist[].
      reclist-receiver = mail_id.
      reclist-rec_type = 'U'.
      APPEND reclist.
    *Function module to send email with an attachment
      CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
        EXPORTING
          document_data              = docdata
          put_in_outbox              = 'X'
          commit_work                = 'X'
        TABLES
          packing_list               = objpack
          contents_bin               = objbin
          contents_txt               = objtxt
          receivers                  = reclist
        EXCEPTIONS
          too_many_receivers         = 1
          document_not_sent          = 2
          document_type_not_exist    = 3
          operation_no_authorization = 4
          parameter_error            = 5
          x_error                    = 6
          enqueue_error              = 7
          OTHERS                     = 8.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.
    ENDFORM.                    "send_email
    Edited by: vikram shah on Sep 25, 2008 5:28 PM

  • Mail attachment saving problem

    Hi,
    When I tried to save a Mail attachment today I got the following warning:
    Mail was unable to save the attachment “File.numbers” to disk. Please verify your downloads folder exists and is writable.
    The warning must be wrong because other attachments, even of the same kind, can be saved without problems. I observed that only such files are effected which contain subdirectories like numbers, keynote, etc. and my suspicion is, that the saving problems may come from wrong file reading/writing rights within these subdirectories, that the sender of the Mail may have imposed to prevent unwanted changes to his/her file. May this be so? Otherwise, what else?
    (MacOs X 10.5.6, Mail.app 3.5 and, in this particular case, Numbers08)
    Thanks for for help in advace!
    Joe.

    Hi Kappy,
    Nothing at all. No error message but no copying either, and no change in the curser appearance when dragging to the Desktop.
    When I go to Mail --> Message --> Forward as Attachment, the attachment in the composer window can be saved like a normal file and has an appropriate size, eg. the desired original file seems to be contained in there. However, I have no Idea how to extract the original .numbers file from that.
    Strange, isn't it?
    Thanks,
    Joe.

  • Is there a way to automatically move mail from one folder to another?

    Is there a way to periodically, automatically, move mail from one folder to another?
    For example, I'd like to move mail from one of my IMAP/Archives folders to my MyMac/Archive folder and I'd like this to happen without intervention.
    Can this be done?

    I'll try to be more specific.
    When I'm done with an email, I archive it.
    Because I have multiple email accounts, this results in emails in multiple Archive folders (one per account). 
    I'd like to have these automatically all moved into one (on my Mac).

  • Problem in Reading Mail Attachement

    I'm facing a problem while reading mail attachments. Java-mail returns null disposition for the below message,but this message has attachment javamail is not considering the mail attachment .I'm using java-mail 1.3.Can anybody help me to solve this problem ?
    Return-Path: prvs=00548bd8bd=[email protected]
    Received: from PRODTM1.TRUST.UASC.AO ([172.28.2.143])
         by trust.uasc.net
         ; Mon, 14 Mar 2011 11:07:04 +0000
    X-TM-IMSS-Message-ID:<[email protected]>
    Received: from gw.northport.com.my ([202.188.31.130]) by trust.uasc.net ([172.28.2.144])
    with ESMTP (TREND IMSS SMTP Service 7.1) id 45727fb90003495a ; Mon, 14 Mar 2011 11:06:52 +0000
    Received: from [192.1.10.13] (port=59768 helo=nmbsmtp-01.northport.com.my)
         by gw.northport.com.my with esmtp (Exim 4.69)
         (envelope-from <[email protected]>)
         id 1Pz5bs-0003EO-0A; Mon, 14 Mar 2011 19:06:48 +0800
    X-CTCH-RefID: str=0001.0A090205.4D7DF6C8.0116,ss=1,fgs=0
    Date: Mon, 14 Mar 2011 19:05:50 +0800 (SST)
    From: AMTedi-Container Svcs <[email protected]>
    Message-ID: <[email protected]>
    Mime-Version: 1.0
    To: [email protected], [email protected],
    [email protected], [email protected]
    Subject: CODCT1UAS
    Content-Type: multipart/mixed; boundary="-"
    X-TM-AS-Product-Ver: IMSS-7.1.0.1394-6.5.0.1024-18010.003
    X-TM-AS-Result: No--4.942-5.0-31-1
    X-imss-scan-details: No--4.942-5.0-31-1

    If you were assuming that every message would have a valid body part
    and that they would tell you definitively which parts you should consider to
    be "attachments", ya, that won't work. The disposition is a hint at best.
    It can be wrong. It might not even be there.

  • New Line Character problem in mail attachment

    Hi all,
    my xi-box is in patch level 16. i need to send the mapped xml as a pipe delimited file(a lineitem per line) attachment to a mail id. so i use MTB to content convert the payload and attach the content as an attachment using a custom module and send the mail. Since the naming convention of the attachment (as per my requirement) says the file should be a .txt file, i 've used text/plain as the content-type.
    if i open the attachment in textpad or wordpad i can see one lineitem per line; but if i open it with notepad its not the case.lineitems do not appear in separate lines but appear continuously. i 'm seeing a box character as a separator between the lineitems. i'm not able to figure it out why.
    i used MTB in file adapter and content converted the payload and opened the file in notepad;it was fine.
    But i'm not able to figure out why its happening with the mail attachment. If you have come across such a problem please suggest me to solve this.
    Regards,
    Sundararamaprasad

    Is there any way you can send the file in an ASCII format instead of BINARY format. In many of my resolved cases I just change from binary to ascii and it works for eg., while doing FTP.
    thansk,
    rakesh

  • Mail attachment problem with IP TUNNELING

    Hi
    We are an Isp and we have two links separatly from two different service provider, one is for Send traffic and another one for recive traffic, we use simple Gre tunnle and configure it in our router ,the tunnel is active and everything seems works fine ,but when users want to use mail attachment like yahoo mail attachment they have problem : yahoo ask them to attach files they can browse and choose files after that when they want to finish ,the yahoo attachment page process opened and never finish it s job !!!!!!!
    please help.
    Thanks.
    Reards Bahman mozaffari

    Hi,
    this sounds like you are having MTU problems due to the GRE overhead. You can try to fix this with the command
    ip tcp adjust-mss ! f.e. 1400
    on the tunnel interfaces. The riouter will intercept TCP MSS negotiation and allow only TCP segments small enough to fit through the tunnel.
    Hope this helps! Please rate all posts.
    Regards, Martin

  • Attachment Link To Specify Folder

    Hi,
    Need some help on the sharepoint attachment link to direct go to specify folder. Is it any possibility that during user clicking on the attach file button, the choose file to upload folder is point to our specify define folder ?
    Very appreciate if someone can give some idea on how to do it.
    Thanks !
    Leong

    Hi,
    As far as I know, we cannot point the choose file path of attachment to specify define folder. The behavior is handled by browser's memory.
    Thanks,
    Eric
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    [email protected]
    Eric Tao
    TechNet Community Support

  • How do i move mails out of exchange, i.e. into an off-line folder?

    how do i move mails out of exchange, i.e. into an off-line folder?

    Move them out of where?  Using what application?  Help us help you by providing us some specifics...

  • Deleted a message from Mail does not move it to the trash folder

    I am a brand new mac user, so it's possible I just don't know what I am doing yet.
    I have added my IMAP email account to Mail with the following mailbox behavior:
    When I delete a message from my Inbox, in Mail it appears to move to the Trash folder.  However, if I log into the webmail interface I can see it actually still exists in the Inbox, but has been marked as deleted (there is a line through it).  It is not actually moving the message on the server into a trash folder, like iPad does.  If I pull up the Trash folder on my iPad, it does automagically reflect the item I deleted.  However, on my phone (not an iPhone), the messages are missing from the Trash folder.
    How do I configure Mail on my Mac to behave like Mail on the iPad?  When I delete a message, I want it to move the message into the Trash folder on the server.
    Thank you very much,
    Jesse

    I figured it out.  Mail created a new folder called, "Deleted Messages."  I was looking in "Deleted Items," which was an old folder.

  • TS3899 I am unable to move email to the trash folder.  The mail appears to move, but stays in the inbox.

    I am unable to move email to the trash folder.  The mail appears to move, but stays in the inbox.

    Try Settings>Mail, contacts, calendars>Your email account>Advanced>Move Discarded Messages into>Deleted Mailbox.
    Or Settings>Mail, contacts, calendars>Your email account>Account>Advanced>Move Discarded Messages into>Deleted Mailbox....and Mailbox Behaviors>. Deleted Mailbox>Trash.

  • Is there a way to set Thunderbird to automatically move mail I have read into an old mail folder? If so, pleased explain.

    is there a way to set Thunderbird to automatically move mail I have read into an old mail folder?

    I would start by creating a filter that looks like the one in the picture, but if you were run the filter automatically when getting new mail, it might behave oddly if new mail arrives while you're reading a message.
    http://kb.mozillazine.org/Filters_(Thunderbird)
    Another approach could be to use the Age in Days + is greater than + 1 criterion in the filter.

  • Why can't I delete mail or move mail to a folder. This started after mail host microsoft update to their exchange server

    We recently moved our mail from an in-house server to an outside hosting service. It was working well until we returned after Christmas. The mail host explained that they had received a Microsoft update to their hosting servers. Now we get an "ERROR 12" code and cannot delete mail or move mail on any of the IMAP folders. I tested the theory on Outlook and the mail deleted fine and was able to move to folders and it synchronized without issue. How can we fix this on Thunderbird?

    Outlook connects to the server using emapi Thunderbird uses imap. So a comparison is nothing more than a waste, or did you explicitly set up an imap account in Outlook?
    Please create an IMAP log https://wiki.mozilla.org/MailNews:Logging
    I did find an [https://bugzilla.mozilla.org/show_bug.cgi?id=741374 old bug] that indicates fasthost had incorrectly configured servers (is that your provider?) with a workaround.
    It is difficult to diagnose exchange imap issues because they do not use any of the standard error codes or messages. But a log should at least provide information on the command the server does not like and that makes a good starting point.

Maybe you are looking for

  • HT201080 How do I edit a family members details?

    How do I edit a family members details? I have set up family sharing in iCloud and accidentally saved an incorrect birthdate. Can I edit these details?

  • What's the shortcut to close the active tab in safari?

    I can't find them in the shortcut list, but it sounds like very basic feature for a common activity. I know the shortcuts for navigating different tabs.

  • Recommend a printer for me

    Hi, I hope someone can help me. Here are the requirements for the printer I need. OUR USE: For printing black images (logos) on CLEAR transparency (clear inkjet films) to burn the image on to silkscreen. We do not need color so least amount of color

  • Sometimes e-mail address shows instead of name

    I have a person's full name and e-mail address in my address book.  When I receive a message from that person in the mail.app, their e-mail address shows.  What can I do to show the person's full name instead?

  • Can't delete a cancelled job in the queue

    Hi, Yesterday i was printing documents and shut down my computer before it was finished. The last document had only been printed in half. I have been in the queue to cancel it but it's stuck and i cannot print any other documents. I can't find how to