How to capture mail.app message url that will remain persistent when message is moved?

So mail.app has this wonderful message:// URL scheme, which works great—until the message is moved. Then links created sometimes work—and sometimes don't. I can't figure out the difference. So my question is twofold:

  1. Is there a way to access messages through a URL even when they've moved?
  2. If not, how do I move a message and then get its message ID?

My current script is

tell application "Mail"
    set theSelectedMessages to selection
    set the selected_message to item 1 of the theSelectedMessages
    set message_id to the message id of the selected_message
    set message_subject to the subject of the selected_message
end tell
set myPrompt to display dialog "Create New To Do in BusyCal" default answer message_subject
set response to the text returned of myPrompt
set quick_entry to encode(response & " " & month of (current date) & day of (current date) & "!!!" & " <message:<" & message_id & ">>") -- see encode handler below
tell application "BusyCal"
    activate
    open location "busycalevent://new/-" & quick_entry
end tell
tell application "Mail"
    move the selected_message to mailbox "Calendar" of mailbox " To-Do" of account "Calion"
end tell
--encode handler
on encode(msg)
    set theText to do shell script "/usr/bin/python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of msg
    set AppleScript's text item delimiters to "/"
    set theTextItems to text items of theText
    set AppleScript's text item delimiters to "%2F"
    set theText to theTextItems as string
    set AppleScript's text item delimiters to {""}
    return theText
end encode

Solution 1:

Short answer

message: URLs are persistent, but Mail needs to be restarted so that its message ID database gets updated and the URLs are properly resolved.

You will have to restart Mail before using BusyCal. Alternatively, you could restart Mail in your script.


Long answer

After some tests I noticed that:

  • message IDs don't change when a message is moved:

    The message ID of this test mail located in the Inbox:

    enter image description here

    doesn't change after moving it to the _People mailbox:

    enter image description here

  • message IDs work after Mail is restarted:

    After moving the message, the URL message:%3CCA+pHXZSgP12HvSg2sQ9J3ochK2K8xOTk_wEoTf+6WmMcYGYxnw@mail.gmail.com%3E shows an empty message because Mail looks for the message in the Inbox (press Command and click the envelope icon on the window bar to show the path to the empty message):

    enter image description here

    After restarting Mail, the message is found successfully:

    enter image description here

(Tested on OS X Mavericks 10.10.4.)

To your questions:

Is there a way to access messages through a URL even when they've moved?

Yes, restart Mail so that it rebuilds its internal message ID database. You will be able to access any moved messages in their new mailboxes.

I understand this is annoying but it seems to be the only workaround.

If not, how do I move a message and then get its message ID?

That's not necessary as message IDs don't change.