AppleScripts to create Calendar Events not working in Catalina

applescriptcalendarcatalina

I have a number of AppleScripts that are triggered by Mail.app rules. They were working perfectly (and still are) on my old iMac running High Sierra, but I now have a new iMac running Catalina on which they are no longer working.

An example script is the one below which extracts some details from the email and then creates a Calendar event containing those details and adds a couple of alarms to the event.

-- Triggered by Mail rule.
using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        tell application "Mail"
            repeat with theMessage in theMessages
                try
                    set msgsubject to subject of theMessage
                    set msgcontent to content of theMessage
                    set msgid to message id of theMessage
                    set {amount, dueon} to my parseMsg(msgcontent)
                    my createEvent(msgsubject, msgid, amount, dueon)
                end try
            end repeat
        end tell
    end perform mail action with messages
end using terms from


-- Parse the email content to extract invoice details.
on parseMsg(msgcontent)
    set amount to extractBetween(msgcontent, "Invoice for", "due by")
    set dueon to extractBetween(msgcontent, "due by", "Review")
    return {amount, dueon}
end parseMsg

-- Extract the substring from between two strings
to extractBetween(theString, startText, endText)
    set tid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to startText
    set startComps to text items of theString
    set AppleScript's text item delimiters to endText
    set endComps to text items of second item of startComps
    set AppleScript's text item delimiters to tid
    return trim(first item of endComps)
end extractBetween

-- Trim all whitespace from start and end of a string
on trim(theString)
    set theChars to {" ", tab, character id 10, return, character id 0, character id 8232}
    repeat until first character of theString is not in theChars
        set theString to text 2 thru -1 of theString
    end repeat
    repeat until last character of theString is not in theChars
        set theString to text 1 thru -2 of theString
    end repeat
    return theString
end trim


-- Create a calendar event for the specified invoice.
on createEvent(msgsubject, msgid, amount, dueon)
    set startdate to (current date) + 28 * days
    set sentdate to current date
    -- set enddate to startdate
    tell application "Calendar" to tell calendar "Invoices"
        set theEvent to make new event with properties {start date:startdate, summary:"Check " & msgsubject, allday event:true}
        delay 5
        set description of theEvent to amount & return & "Due on: " & dueon & return & "Sent on: " & sentdate
        delay 5
        set url of theEvent to "message:" & "%3c" & msgid & "%3e"
        delay 10
        tell theEvent
            -- Add a email alarm
            make new mail alarm at end of mail alarms with properties {trigger interval:10}
            delay 10
            make new sound alarm at end of sound alarms with properties {trigger interval:370, sound name:"Sosumi"}
        end tell
    end tell
end createEvent

The following Mail.app Rule triggers the scriptenter image description here

I have also enabled access to the Calendar for Mail.app in the Privacy Panel of System Preferences.
enter image description here

The rule tries to run as I see a little gear icon appear in the menu bar and the Calendar.app opens, but the calendar event isn’t added. As I say, this works perfectly in High Sierra. I also have a corresponding AppleScript that removes the alarms from the Calendar when an alternative email is received. Again this one works in High Sierra but dosen't work in Catalina either.

Has anyone got any ideas?

Thanks,
Alan.

Best Answer

OK, I've made some changes so that I can run the Script in Script Editor (by giving the variables plain text strings rather than extracting them from the email).

When I run the script I get the following error: error "Calendar got an error: Can’t get calendar "Invoices"." number -1728 from calendar "Invoices"

That gives me a clue I guess - although the Calendar that it should be adding the event to does exist in my Calendar app. Could it be something to do with the quotes around "Calendar" and "Invoices" in the following line of the script?

tell application "Calendar" to tell calendar "Invoices"