Search code examples
applescript

AppleScript error -10000 when writing to dropbox directory


I have pasted together an apple script to save email attachments to a directory. Everything works fine, but when I set the output to a dropbox directory, I get an error message:

Result:
error "Mail got an error: To view or change permissions, select the item in the Finder and choose File > Get Info." number -10000

The line producing the error is the one starting with save anAttachment to... in the following script:

tell application "Mail"
    set theMessages to messages of mailbox "Inbox" of account "xxx@yyy.zzz"
    repeat with theMessage in theMessages
        set theOutputFolder to ("/.../Dropbox (Company)/") as string
        repeat with anAttachment in (get mail attachments in theMessage)
            set theAttachmentName to name of anAttachment
            set theSavePath to theOutputFolder & theAttachmentName
            save anAttachment in theSavePath
        end repeat      
    end repeat
end tell

In a non-dropbox directory everything works.

The flags of the dropbox directory are set as follows:

~ $ ls -FLAG -lhd@ Dropbox\ \(Company)                                
drwx------@ 15 username  staff   480B Apr  8 13:52 Dropbox (Company)/
    com.apple.FinderInfo      32B 

I tried to run chmod go+rw on the directory, but that did not help.

GetInfo on the directory yields:

enter image description here

Changing everyone to Read&Write does not help either.


Solution

  • Whilst you get to the bottom of what is preventing you from writing out to folders other than your Downloads folder (which presumably includes subfolders in the Downloads folder), here's a barebones script that should save mail attachments to the folder ~/Downloads/Mail Attachments/.

    I've put a filter on the messages being acted upon as retrieving every message in a mailbox is potentially very costly. This retrieves messages from the previous three days:

    tell application id "com.apple.SystemEvents" to tell (make new folder ¬
            with properties {name:"~/Downloads/Mail Attachments"}) to set ¬
            directory to the POSIX path
    
    
    tell application id "com.apple.Mail"
            set _M to a reference to (every message in the inbox whose ¬
                    date received > ((my date named 0.0) - 3 * days))
    
            repeat with A in _M's mail attachments
                    set f to the directory & "/" & A's name
                    save A in (f as POSIX file)
            end repeat
    end tell
    

    You could perhaps add code to migrate the "Mail Attachments" folder from your Downloads folder to your Dropbox folder.