Move mail message AppleScript

I am playing around with the KM/AppleScript shortcut that @MacSparky gave on his recent Apple Mail webinar. It actually works fine for my iCloud account, but fails when used with any of my non-iCloud email accounts. Not sure why. I am the farthest thing from an AppleScript expert. Thoughts? Image of the error as well as the shortcut attached.



To move a message from one mailbox to the other via / AppleScript, it doesn’t always work to set the mailbox of the message. Even though the AppleScript dictionary for does not show this property as readonly, there are limitations on setting this property. I suspect it works when you are moving the message either to an iCloud mailbox (as you have found to work) or if you are moving within mailboxes of the same mail account.

I have a similar script, that is triggered in KM, only when is the active application. What my script actually does is checks the account of the message which lets me know if I received it in my personal email account, my iCloud account, my (old) work account, or my wife’s account. It then sets a target mailbox as one of the folders on my Synology, and I then use the “move” verb in AppleScript to move the email over. This has worked reliably.

The code is reasonably straight forward, and looks somewhat like this:

tell application "Mail"
  set theSelection to the selection
  if the length of theSelection is not 0 then
    repeat with theMessage in theSelection
       set theAccount to the name of the account of the mailbox of theMessage
       if theAccount is equal to "" then
          set theTargetAccount to "me"
      else if theAccount is equal to "" then
         set theTargetAccount to "someoneelse"
         set theTargetAccount to "default"
     end if
     repeat with theSynologyMailbox in theSynologyMailboxes
         -- I already loaded the names of all the mailboxes on the Synology account
         -- into theSynologyMailboxes
         if the name of theSynologyMailbox is equal to theTargetAccount then
            set theTargetMailbox to theSynologyMailbox
            exit repeat
         end if
      end repeat
  end if

-- Here's the move!
   move theMessage to theTargetMailbox
end tell

I hope this is useful for you to get your code running.

Interesting … I’ve successfully used my script with iCloud and IMAP accounts.

Agreed. I have found that’s AppleScript tends to be flakey. For example, there has been a longstanding bug in retrieving attachments. I spent a lot of time trying to get that to work, but TheGoogle informed me that many others had also tried and failed. I don’t know if it’s been fixed in Catalina or Big Sur as my need for saving attachments via AppleScript went away.

At first I thought it might be an issue with my work Gmail account, but got the same error with a non-Gmail IMAP account.

I THINK I have figured out my problem. Seems that once moving to a folder got flagged for an error the error was not going to go away. So, as odd as it seems, I simply deleted and recreated the folder and then things started to work. AppleScript is indeed finicky. My next question is are there any rules I should follow when naming my Mail folders? Getting the impression that spaces in folder names are not consistently handles well by AppleScript. Second, getting the @MacSparky AppleScript to work gets me halfway home. I have tried modifying the script to move messages I need to archive (e.g. I keep all student emails for a year) to a local folder. This, for whatever reason, has proven to be a bigger challenge (at least for me). Thanks for any help!