Advice on AppleScript syntax for adding Apple Mail.app message URL into Calendar.app

Solution 1:

The example AppleScript code, shown below, was tested in Script Editor, with minor modifications to work from within it, and as is as an Automator Service/Quick Action, under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

Example AppleScript code:

tell application "Mail"
    reopen
    activate
end tell

delay 0.25

tell current application
    activate
    set msg to "Select a Mail message to copy its Message ID"
    display dialog msg buttons {"Cancel", "Selected"} default button 1
end tell

if button returned of result is "Selected" then
    tell application "Mail"
        set theSelectedMessages to selection
        set the selectedMessage to item 1 ¬
            of the theSelectedMessages
        set messageID to the message id of the selectedMessage
        set the clipboard to "message://<" & messageID & ">"
    end tell
    tell application "Calendar" to activate
    delay 0.2
    tell application "System Events" to keystroke "v" using command down
end if

Notes:

The code in the OP is not structured well having nested tell blocks/statements within one another in a manner that is not appropriate. The example AppleScript code shown above is properly structured for the events that need to take place.

In the first tell application "Mail" block reopen is used so if Mail is already opened and without a window opened, it opens the default window, thus allowing it to be activated for the user to make a selection.

The tell current application block allows the display dialog to come to the front, so as not to be hidden under other windows.

In the Automator Service/Quick Action the example AppleScript code is all that is in the Run AppleScript action. The Automator Service/Quick Action is set to Workflow receives [no input] in [Calendar].

As this is dealing with two applications and an Automator Service/Quick Action using a display dialog, one might consider adding some additional coding to set the bounds/position/size of the windows and dialog box involved.

If you want to avoid using the clipboard, then change the following lines of code:

Change:

set the clipboard to "message://<" & message_id & ">"

To:

set theURL to "message://<" & messageID & ">"

Change:

tell application "System Events" to keystroke "v" using command down

To:

tell application "System Events"
    tell front window of process "Calendar"
        if exists pop over 1 then
            set the value of the first text field of pop over 1 ¬
                whose value of attribute "AXPlaceholderValue" is "Add URL" to theURL
        end if
    end tell
end tell

Note that in order to use the modified code shown directly above, the Event must be showing as a pop up and the cursor placed in the Add URL text box.

Example AppleScript code:

tell application "Mail"
    reopen
    activate
end tell

delay 0.25

tell current application
    activate
    set msg to "Select a Mail message to copy its Message ID"
    display dialog msg buttons {"Cancel", "Selected"} default button 1
end tell

if button returned of result is "Selected" then
    tell application "Mail"
        set theSelectedMessages to selection
        set the selectedMessage to item 1 ¬
            of the theSelectedMessages
        set messageID to the message id of the selectedMessage
        set theURL to "message://<" & messageID & ">"
    end tell
    tell application "Calendar" to activate
    delay 0.2
    tell application "System Events"
        tell front window of process "Calendar"
            if exists pop over 1 then
                set the value of the first text field of pop over 1 ¬
                    whose value of attribute "AXPlaceholderValue" is "Add URL" to theURL
            end if
        end tell
    end tell
end if


Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.