2

I'm an AppleScript novice and receiving the above error in what should be a very simple AppleScript. I believe it is somewhere in the set statement but unable to ascertain. Would greatly appreciate any guidance or suggestions.

using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
set attachmentsFolder to (("Macintosh HD:Users:John:Dropbox:John & Julie:Wirecard") as Unicode text) 
tell application "Mail"
    -- Save the attachment
        set savePath to attachmentsFolder & (name of theAttachment)
            try
                with timeout of 1800 seconds
                    save theAttachment in file (savePath)
                end timeout
            end try 
end tell
end perform mail action with messages
1

You omitted the closing line in the using terms from... block.

▸ Add end using terms from to the end of your script.


However, your script will encounter an error, given that the variable theAttachment is undefined.

On less important notes, you needn't declare attachmentsFolder as class Unicode Text, as you already expressed the value of the variable as a string (which is unicode text). Since it's a folder you're specifying, the HFS path ought to have an ending colon, which you've omitted. This would actually lead to another error when you concatenate it with the file name. In fact, you needn't use HFS paths at all when providing a path to the location where the attachment is to be saved. A POSIX path string is entirely acceptable, and actually preferable. So, it would look like this:

    set attachmentsFolder to "/Users/John/Dropbox/John & Julie/Wirecard/"
    set savePath to the attachmentsFolder & (the name of theAttachment)
    save theAttachment in POSIX file savePath

I'd also say that the try...end try is unnecessary, and hindering: if there was an error in the save command (which there will be as it currently stands, given the omitted colon in the path string), it would go unnoticed and your attachment simply wouldn't get saved, but you'd have no idea why until you remove the try...end try enclosure.

But the reason you don't actually need it is because there's no reason to silence the errors at this point in the script. It's the very last command, so if the script terminates due to an error, it doesn't matter. Typically, the try block would be useful if there were more commands to follow afterwards, and you didn't want the failure to save the attachment to prevent the rest of the script from executing.

Implementing these changes would result in this as a revised script:

    using terms from application "Mail"
        on perform mail action with messages theMessages for rule theRule
            set attachmentsFolder to "/Users/John/Dropbox/John & Julie/Wirecard/"
            tell application "Mail"
                set savePath to the attachmentsFolder & the name of theAttachment

                with timeout of 1800 seconds -- 30 minutes!
                    save theAttachment in POSIX file savePath
                end timeout
            end tell
        end perform mail action with messages
    end using terms from

(Although you still need to define the theAttachment variable.)

  • Thank you is WAS the missing "end using terms from" statement that was causing my syntax error. No on to debugging the rest of the script. This is intended to run as a mail rule and save attachments from specific mail users into the drop box folder specified. – Qwikatb Jul 22 '18 at 6:40
  • Thank you again..... still working through this and am stuck trying to get the name of the mail attachment into the variable. I have tried Get and Extract to no avail. Any clues.... – Qwikatb Jul 22 '18 at 7:56
  • Finally got it working... Thanks to everyone who assisted.... – Qwikatb Aug 13 '18 at 13:47
1

Delimit the ampersand using a backslash like so:

(("Macintosh HD:Users:John:Dropbox:John \& Julie:Wirecard")

From: https://stackoverflow.com/questions/11233682/opening-a-file-with-an-in-the-middle-mac-terminal

  • Thank you tried your suggestion and received an error "Expected """ but found unknown token" I tried removing the & from the filename to rule it out as the source of the problem and continued to receive my original error message. – Qwikatb Jul 21 '18 at 19:57
  • Does this script run correctly with other Dropbox files/folders? – IconDaemon Jul 21 '18 at 21:15
  • 1
    The link you referenced in your answer pertains to file paths in the Terminal and in bash scripts. It bears no relevance here. Accordingly, escaping the ampersand is not necessary in this situation, even when using posix paths. – CJK Jul 21 '18 at 22:51
  • 1
    In the context of the OP, this answer has no bearing and is wrong. That line of code, by itself, throws an error:Syntax Error Expected “"” but found unknown token., but without escaping the &, it doesn't throw an error and returns the string as it should. This answer should be deleted! – user3439894 Jul 22 '18 at 0:48
  • I finally got it sorted out so want to thank everyone for their suggestions and assistance. – Qwikatb Aug 13 '18 at 13:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .