0
votes

I have a script to copy all email attachments in a selected set of emails to a folder after some renaming. Because the attachments sometimes have identical names at origin, even with renaming, I need to add something like " copy" to subsequent versions so they don't save atop one another. With some programming knowledge but very little understanding of AppleScript, I cobbled this together:

tell application "Mail"

set theMessages to selection
set theOutputFolder to (choose folder) as string
repeat with a from 1 to length of theMessages
    set theMessage to item a of theMessages
    set {year:y, month:m, day:d} to date sent of theMessage
    set theDate to (y * 10000 + m * 100 + d) as string
    set theAttachments to every mail attachment of theMessage
    repeat with b from 1 to length of theAttachments
        set theAttachment to item b of theAttachments
        set theAttachmentName to theDate & " " & name of theAttachment
        set theSavePath to theOutputFolder & theAttachmentName
        tell application "System Events" to exists file theSavePath
        repeat while the result
            set oldDelims to AppleScript's text item delimiters
            set AppleScript's text item delimiters to {"."}
            set delimitedList to every text item of theSavePath
            set suffix to "." & last item of delimitedList
            try
                copy text items 1 thru -2 of theSavePath to theSavePathBase
            on error
                copy theSavePath to theSavePathBase
            end try
            -- display dialog "theSavePath pre- : " & theSavePath
            -- display dialog "theSavePathBase & ' copy' & suffix pre- " & theSavePathBase & " copy" & suffix
            set AppleScript's text item delimiters to oldDelims
            copy theSavePathBase & " copy" & suffix to theSavePath
            -- display dialog "theSavePath post- : " & theSavePath
            tell application "System Events" to exists file theSavePath -- <<< *** BOMBS HERE
            -- display dialog "Made it past existence check."
        end repeat
        try
            display dialog "preparing to save..."
            save theAttachment in theSavePath
        on error errText number errNum
            display dialog errText
        end try
    end repeat
end repeat

end tell

It works, except that when the need to fix the name of a copy is encountered, it bombs at the indicated location with this message: "System Events got an error: Can’t make {"...Desktop:Mail Attachments:20140830 Resumé 2014", "pages", " copy", ".zip"} into type integer." Something about the way I reconstituted the name changed the type to something the exists checker can't handle.

Any help would be appreciated. Help with an explanation would be better, since I'm not an AppleScripter but am interested. Thanks!

1
For starters, your line "tell application "System Events" to exists file theSavePath" is not in a repeat loop, so I don't think the flow would account for a situation where a file name conflict has occurred for the second time. It will just use the same " copy" scheme.jweaks

1 Answers

1
votes

Your error message is telling you the problem. Notice the brackets {} around the error message.

Can’t make {"...Desktop:Mail Attachments:20140830 Resumé 2014", "pages", " copy", ".zip"} into type integer."

That's indicating that this is a list of items, not a string. As such you need to make it a string first therefore change...

copy text items 1 thru -2 of theSavePath to theSavePathBase

to:

copy (text items 1 thru -2 of theSavePath) as text to theSavePathBase

With that being said, I don't think your actual copy command (as follows) will work. It seems you're wanting the copy command to rename the file and copy it all in one step.

copy theSavePathBase & " copy" & suffix to theSavePath

The copy command won't be able to find "...Desktop:Mail Attachments:20140830 Resumé 2014.pages. copy.zip" because it doesn't exist. That's the name you want for the renamed file. I think you're best approach would be to use the "cp" unix executable command to preform your copy because it can copy and rename in one step. Something like this although you'll have to figure out how to get the actual value for the variable "theAttachmentCurrentPath".

do shell script "cp " & quoted form of POSIX path of theAttachmentCurrentPath & space & quoted form of POSIX path of (theOutputFolder & text 1 thru -5 of theAttachmentName & " copy" & text -4 thru -1 of theAttachmentName)