Jim's Add an Event to Apple Calendar Macro:

So, this is the whole AppleScript that worked for me, to set the new event and open it in Calendar.

Thanks so much again, @Jim and @ccstone

set theDate to short date string of (current date)

tell application (path to frontmost application as Unicode text)
  set varCal to (choose from list {"Family", "Work"} with title "List of Calendars" with prompt "Please select a calendar" default items "Family" OK button name {"Choose"}) as string
end tell

display dialog "Enter the date:" default answer (theDate as text)
set varDate to text returned of the result

set varTime to (choose from list {"09:00", "09:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30", "20:00", "20:30", "21:00", "21:30", "22:00", "22:30", "23:00", "23:30"} with title "List of start times" with prompt "Please select the start time" default items "11:00" OK button name {"Choose"}) as string

set varStart to date (varDate & space & varTime)
set varEnd to (date (varDate & space & varTime)) + (1 * hours)
display dialog "Enter the event:" default answer "Appointment with "
set varName to text returned of the result

display dialog "Enter the description:" default answer "→ "
set varDesc to text returned of the result

set varLocation to (choose from list {"Home", "Work", "Other"} with title "List of locations" with prompt "Please select a location" default items "Home" OK button name {"Choose"}) as string

tell application "Safari"
  set varURL to URL of front document
end tell

display dialog "Enter a link:" default answer varURL
set varURL to text returned of the result

set varRemind to (choose from list {"0", "-15", "-30", "-60", "-120", "-1440", "-10080"} with title "List of reminder miutes" with prompt "Please select a reminder time" default items "0" OK button name {"Choose"}) as string

tell application "Calendar"
  activate
  
  set theCurrentDate to current date
  set newEvent to make new event in calendar varCal at end with properties {description:varDesc, summary:varName, location:varLocation, start date:varStart, end date:varEnd, url:varURL}
  tell newEvent
    make sound alarm at end of sound alarms with properties {trigger interval:varRemind, sound name:"Crystal"}
  end tell
  
  show newEvent
  
end tell
2 Likes

Wow! You guys were busy!

So everyone is happy? Or do I need to look at any code?

That said, I will be taking a look at @ccstone's version. I had not thought about using Keyboard Maestro's menu command.

Yes, very happy. All is working for me fine now. It has been helpful for me also as a way to get a little more confident editing AppleScript.

Happy to hear it @Zabobon! Enjoy!

That is am amazing piece. I have been working with calendar scripting myself.

One thing I would suggest as an update (though it is not working for me yet) is to add an invitation.

The code in general for that is:

tell newEvent
   make new attendee at end of attendees with properties {email:"test@test.com"}
end tell

My issue is that I want to make the attendee as a variable.
Hence I have before the following script.

set varInvite to (choose from list {"No", "test1@test.com", "test2@test.com", "test3@test.com"} with title "Invite" with prompt "Do you want to send an invite to someone?" default items "No" OK button name {"Choose"}) as string

However, for me the function

make new attendee at end of attendees with properties {email:"test@test.com"}

only works if there is a specific email address. If I put "with properties {email:varInvite} it does not send an invite.

Any solutions on that? I was trying to put the variable in brackets, but nothing changed. It only sends the invite if I do not have a variable there.

@michael_e — Try this:

set theDate to short date string of (current date)

tell application (path to frontmost application as Unicode text)
	set varCal to (choose from list {"Things", "Magic", "Gaming", "Home", "Social", "Travel", "Webinar"} with title "List of Calendars" with prompt "Please select a calendar" default items "Things" OK button name {"Choose"}) as string
end tell

display dialog "Enter the event:" default answer "Appointment with "
set varName to text returned of the result

set varLocation to (choose from list {"Home", "Work", "Online", "Store", "Convention"} with title "List of locations" with prompt "Please select a location" default items "Online" OK button name {"Choose"}) as string

display dialog "Enter the date:" default answer (theDate as text)
set varDate to text returned of the result

set varTime to (choose from list {"10:00 am", "11:00 am", "12:00 pm", "1:00 pm", "2:00 pm", "3:00 pm", "4:00 pm", "5:00 pm", "6:00 pm", "7:00 pm", "8:00 pm", "9:00 pm"} with title "List of start times" with prompt "Please select the start time" default items "1:00 pm" OK button name {"Choose"}) as string
set varStart to date (varDate & space & varTime)
set varEnd to (date (varDate & space & varTime)) + (1 * hours)

display dialog "Enter the description:" default answer "→ "
set varDesc to text returned of the result

tell application "Safari"
	set varURL to URL of front document
end tell

display dialog "Enter a link:" default answer varURL
set varURL to text returned of the result

set varRemind to (choose from list {"0", "-15", "-30", "-60", "-120", "-1440", "-10080"} with title "List of reminder minutes" with prompt "Please select a reminder time" default items "-60" OK button name {"Choose"}) as string

set varInvite to (choose from list {"test1@test.com", "test2@test.com", "test3@test.com"} with title "Invite" with prompt "Do you want to send an invite to someone?" default items "No" OK button name {"Choose"}) as string

tell application "Calendar"
	set theCurrentDate to current date
	set newEvent to make new event in calendar varCal at end with properties {description:varDesc, summary:varName, location:varLocation, start date:varStart, end date:varEnd, url:varURL}
	tell newEvent
		make sound alarm at end of sound alarms with properties {trigger interval:varRemind, sound name:"Crystal"}
		make new attendee at beginning of attendees with properties {email:varInvite}
	end tell
end tell

Thanks. That is exactly what I have done before. Mine still does not work, your's does work. I now figured out my invite also works, however, it does not work with my own email address that I used for testing, in my case "xxx@gmail.com", although the selected calendar is a different one. Many thanks anyway!

4 posts were split to a new topic: Create a New Contact with Keyboard Maestro and AppleScript

Hi Jim,

I am struggling with another function that I am missing: sometimes I have all day events (reminders). Would you know how to make that?

I was wondering about

"set varTime to (choose from list {"All-Day", "10:00 am",[...]"

And then combine it with an if / else function, like

tell application "Calendar"
set theCurrentDate to current date
if varTime is "AllDay" set newEvent [...]
else set newEvent [...]

However, this does not work for me. Any idea how to set that up? Would be great.

@michael_e — That looks like a good start, except that i see All-Day and AllDay (keep them exactly the same).

If that isn't the issue, post your entire script and I will take a stab at it.

I am using this Macro on a daily basis. So much better than any other way to create an event. The dialog boxes coming up in order to prompt for the information make the whole process flow very naturally.
One thing I am wondering and maybe @ccstone might have some ideas... it would be great to be able to enter the date in more forms. The prompt for the date already works with different forms (i.e. "25/06/2021" and "25 June 2021" both work) but "tomorrow" doesn't. But I am wondering if it is possible to bolt-on a date parser to this part of the Macro to take the User Input and make a Variable that Calendar can use.

See this:

-Chris

I did not manage to set it up; the "if" "else" did not work for me. What I did now is a "workaround" with KM running a prompt for user input first and asking for "Allday?". If I select that, then it sets the calendar entry with the following:

set newEvent to make new event in calendar varCal at end with properties {description:varDesc, summary:varName, location:varLocation, start date:varStartDate, end date:varStartDate, allday event:true}

That works fine for me.

Different question that I was also not able to solve, even with lot of research is the following. With many all day events, I would like to have a reminder at the same day at 20:00 and also on the next day at 09:00. Hence, I am looking to set up multiple reminders.

So the varReminder is set up like this:

set varRemind to (choose from list {"+1980", "+540", "0", "-15", "-30", "-60", "-120", "-1440", "-10080"} with title "List of reminder Minutes" with prompt "Please select a reminder time" default items "0" OK button name {"OK"} with multiple selections allowed) as string

Now I need to make something to make it as reminders. Any ideas here?

Maybe add the following after this line in my original: make sound alarm at end of sound alarms with properties {trigger interval:varRemind, sound name:"Crystal"}

with:

make sound alarm at end of sound alarms with properties {trigger date:(date "08:00 PM" of varStart), sound name:"Crystal"}
make sound alarm at end of sound alarms with properties {trigger date:(date "09:00 AM" of varStart + (1 * days)), sound name:"Crystal"}

That will work, thank you. However, do you have a solution if you have a meeting at 16:30 and then want to have a reminder 1h before and 15min before?

1 Like

Try:

make sound alarm at end of sound alarms with properties {trigger date:(varStart - 75 * minutes), sound name:"Crystal"}

Great. Thanks Chris. I am halfway to incorporating this into the complete AppleScript... I will post back once I have it working. Looks very exciting as it happily takes "tomorrow", "next Wednesday" "Monday" as inputs and correctly converts them to dates Calendar can use. I have had to change it from making ISO format to dd/MM/yyyyy as Calendar got confused by ISO.

1 Like

UPDATE - Thanks to a combination of AppleScripts and Macros by @ccstone and @JMichaelTX I have got the Date Parsing working.

Now I can enter the date for the Event in lots of Common English terms, rather than having to type something like, "28//09/2021"

I can type "tomorrow" or "next thursday" or "monday" and the Macro will convert them to a form that Calendar can use. It makes using the Macro really intuitive.

UPDATE 2 - I've included a Checkbox to set an Event for "All Day" with the reminder time off-set. This AppleScript gleaned from @michael_e's solution in the threads above. (The Checkbox is unchecked by default, but clicking in it will make an All Day Event in Calendar.)

Instead of one mammoth AppleScript I broke the Macro into Keyboard Maestro Actions - which made it simpler to edit, troubleshoot and maintain (also for others to adapt to their own specific Calendars and Locations).

I learnt a lot about the Prompt for User Input Action in the process of doing this.

One little subtlety that I didn't know before is that in a Prompt with List entry, if an item is duplicated in the list at the start it becomes the default selection but still keeps its place in the dropdown list:

"11:00|09:00|09:30|10:00|10:30|11:00|11:30|12:00|12:30|13:00|13:30|14:00|14:30|15:00|15:30|16:00|16:30|17:00|17:30|18:00|18:30|19:00|19:30|20:00|20:30|21:00|21:30|22:00|22:30|23:00|23:30" will use "11:00" as the default selection.

And putting a double underscore after an Entry will allow the display of a more user-friendly Prompt. So, "-1440__1 day before" shows "1 day before" in the Prompt but selects "-1440" as the actual value. (Calendar needs the time in minutes but as a human, I find it nicer to think in hours or days :smiling_face:︎)

And being able to make a Checkbox in the Keyboard Maestro Prompt for User Input Action is great too (and made use of the the "All Day" toggle in the Macro).

@Jim, I know you prefer the AppleScript Dialogs to the Keyboard Maestro Prompts - but I have found the Keyboard Maestro Prompts more flexible and a lot easier to edit than the AppleScript ones. They also exploit Keyboard Maestro's great strength as a modular system of Actions where different techniques can be used in one Macro, reordered etc.

I have used a series of Keyboard Maestro Prompt windows -

the first to enter the date
the second to enter the event name
the third to enter the time
and a fourth prompt for information that I can just leave blank if I want.

I found this was better than having just a single Prompt window or a Prompt for every single bit of info. And the great thing is that it's easy to drag these Prompts into a different order or combine them in the future.

EXAMPLE Calendar ⇢ Create New Event (with Date Parsing and All Day).kmmacros (48.0 KB)

1 Like

These lists of Calendars, they're only apple calendars and not available named Google Calendar calendars, correct?

I just posted here:

I'm hoping to find an easy KM solution to add GCal events from my daily flow.

Thanks.

Correct. Since I don't use other calendars, I did not invest time into supporting them.

1 Like