1

With a Calendar.app Event open and the cursor in the URL field I can select the Services menu and run this script. It activates Mail.app and asks me to select the message to copy its URL.

I tried to use keystroke g_url instead of set clipboard and keystroke "v" but the entire URL wouldn't get pasted, only "message:" was pasted into calendar.

If anyone has any ideas on how to clean this up and streamline it I would appreciate it.

I have it in an Automator workflow Quick Action.

tell application "Mail"
    activate
    tell application "System Events"
        display dialog "Select a Mail message to copy its Message ID" buttons {"Selected", "Cancel"}
        if button returned of result is "Selected" then
            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 the clipboard to "message://<" & message_id & ">"
                tell application "Calendar"
                    activate
                end tell
                tell application "System Events" to keystroke "v" using command down
            end tell
        end if
    end tell
end tell

EDIT: To clarify. When I try to keystroke a variable, g_url, the script does not work.

I receive an error when avoiding the clipboard method. enter image description here

EDIT 2"

This code brings the message viewer window forward if it has been miniaturized. Using activate and reopen will not do so.


    tell application "Mail"
        set MainWindow to get id of every window whose name contains "Inbox"
        set miniaturized of window id MainWindow to false
    end tell

1
  • Can't figure out why users downvote a question without making a good faith effort to understand the question. Pretty clearly stated that I was getting an error using the syntax and looking for a way around it.
    – John
    Aug 27, 2021 at 3:44

1 Answer 1

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.

6
  • The only issue is with avoiding the clipboard. I get an error. Will add to the original post
    – John
    Aug 27, 2021 at 1:29
  • Did you try to click on the link that was added to the calendar event to see if it opens the Mail message?
    – John
    Aug 27, 2021 at 1:33
  • @John, Sorry, while I did check the link with the clipboard I had not checked it with typing the URL. I am investing it and will update the answer if I find a solution. However, the answer as currently now posted does do as stated. Thanks. BTW What version of macOS are you running? Aug 27, 2021 at 1:54
  • @John, I have added back the "If you want to avoid using the clipboard, then change the following lines of code:" section with changs to the code that has worked for me in testing. Aug 27, 2021 at 3:13
  • I am using macOS Catalina. Any clue why we need to resort to Accessibility features to paste a value stored in a variable, but not if we paste from the clipboard? Thank you!
    – John
    Aug 27, 2021 at 3:53

You must log in to answer this question.

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