Jim's Add an Event to Apple Calendar Macro:

Rather than using a subscription based utility (like Fantastical), I wanted to be able to add an event to Apple Calendar with all of the details in place.

What follows is my solution, done in AppleScript. It uses multiple AppleScript dialog boxes to collect information (using lists or suggestions), then it creates the event for you. The only option I did not include was an end time. Since my events are most often an hour, I use that as my default.

It even grabs the URL from the front tab in Safari as a suggested URL. I am very happy with it, and felt that it was wroth sharing.

You will have to edit the following to suit your needs:

  • The list of calendars (to match the calendars in your app)
  • The list of locations (to your most used ones)
  • The list of times (as I like 11:00am to 9:00pm—which may not suit you)
  • You may wish to change the reminder times (I use 0, -15, -30, -60, -120, -1d, -1w)
  • The defaults (set them to your own preferences)

I hope that you enjoy it. Here is the AppleScript, followed by a downloadable macro, that you can import into your Keyboard Maestro library.

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

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"}
	end tell
end tell

F4—Calendar: + an event.kmmacros (3.8 KB)

8 Likes

Thank you for sharing @Jim. I've edited it to fit with my specific calendars etc as you suggest and it works perfectly.

1 Like

You're welcome @Zabobon !

1 Like

Hey @Jim,

Well done!

A couple of things:

Just in case you didn't know – you can get a list of calendars like so:

tell application "Calendar"
   set calendarList to name of calendars
end tell

When I use static lists in AppleScript I really dislike using horizontal lists. They are horrible to read and maintain – so long ago I adopted the “vertical list” format:

set calendarList to items 1 thru -2 of {¬
   "Things", ¬
   "Magic", ¬
   "Gaming", ¬
   "Home", ¬
   "Social", ¬
   "Travel", ¬
   "Webinar", ¬
   ""}

However – I've become partial to letting AppleScript do all the work for me:

set calendarList to paragraphs 2 thru -2 of "
Things
Magic
Gaming
Home
Social
Travel
Webinar
"

These formats make inserting new items, deleting items, and sorting the list sooo much easier.

In Choose-From-List dialogs I like to provide a default item:

set calendarList to paragraphs 2 thru -2 of "
Things
Magic
Gaming
Home
Social
Travel
Webinar
"

set chosenCalendar to choose from list calendarList with title "Calendars" with prompt ¬
   "Please Choose a Calendar:" default items {item 1 of calendarList} ¬
   without empty selection allowed

-Chris

1 Like

Thanks @ccstone — great points!

I chose to use a manual list of calendars, as I have several subscription based calendars (US holidays, Spanish holidays, moon cycles, etc.) and I didn't want them showing up in the list. A manual list allows for curation.

In case others want to use an automated list of calendars, here is the code that I was going to use:

tell application "Calendar"
	set CalList to name of every calendar
	set varCal to choose from list CalList with title "List of Calendars" with prompt "Please choose a calendar:" default items "Things" OK button name "Choose"
end tell

I like the vertical lists, and did not know about them. I will use them. Thanks!

My lists do use default items—you may have missed them due to the length of the horizontal lists.

Thank you again for the feedback!

2 Likes

Hi @Jim - I am getting one quirk with this - when I select a PM time it is entered into the Calendar as an AM time. So, selecting 5 pm from the dropdown list puts the event at 5 am in my Calendar. Is this something that you are getting too? It might be a setting in my Calendar. I will try changing 5 pm in the list to 17:00

Yes, that was it (to answer my own question). As my Calendar is set to display 24 hour time I had to change the time choosing part to:

Now it puts the event in the right time.

2 Likes

Happy to see that you figured that out @Zabobon — I don't use military (2400) time, so this did not affect me.

Not that any of us are gong anywhere on airplanes at the moment... but I got into the habit of 2400 time when I was...

2 Likes

2400 does make sense to me. However, since I grew up with 1200, it is a hard habit to change.

Use what works best for you.

I adopted 24 hour time when I had my first regular job and mis-set my shiny new digital alarm clock to PM instead of AM.

All the clock had to indicate the difference was a dot, and it was too easy to mis-set when I was tired or not paying close attention.

When I found the clock could do 24 hour time, I was off to the races and changed all of my timekeepers to same.

No more AM/PM mistakes for me.

-Chris

1 Like

When I started with Windows (ages ago), I always had difficulties sorting my files. Then I found out in Korea that they used YYYYMMDD. Ever since my files are named YYYYMMDD-hhmmss and then the other useful informations. And when the PCs allowed for an appropriate setting I used it. It helps to find a file where you know the date, to catch a plane on time, ... But Jim, I know that it takes forever to change the habits accordingly.

1 Like

For scheduling the thing I find very ambiguous with AM PM is 12am and 12pm. I always have to really think about that when I see it in a schedule. The problem is that 12pm (pm being late sounding) feels intuitively like it should represent Midnight. I know that 12 is like zero in time - which also makes it confusing. 12:00 and 24:00 are clearly two different times in the 24:00 clock.

So, I have got into the habit of writing 12 noon. Everyone knows what that is.

True.

I use 00:00 for Midnight and 12:00 for Noon, which is the typical military convention (although they tend to omit the colon character).

But military time confuses many people, so when I write times for folks other than myself I use 12-hour time – unless I know they grok military time.

-Chris

1 Like

Jim
Nice work and thanks for doing this. You have a small typo in your code ("minute" is missing an "n").

I wonder if it would be possible to make this grab a contact that's selected rather than a (Safari) url? This would be more useful I think. I'm not an AppleScript jock but I will try to figure this out and post if if I can get it to work.

Thanks again.

You're welcome @michaelcolacino. The typo has been fixed.

Regarding Contacts, I believe that it is possible, but since I would never use it, I will leave the research, coding and implementation to you. It will be a good learning experience.

Just wondering @Jim and @ccstone if there is a way to open the event in Calendar after it has been created... I can't figure our how to do that.

I am really liking this way of adding events. What is cool is that while the user input dialogs pop up I can still view whatever email or whatever has motivated me to create the event. So, I can even copy and paste bits and pieces of information during the event creation process.

@Zabobon — this is a little rough (use at your own risk, and after testing), but I think it will do what you want:

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

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"}
	end tell
end tell

tell application "Calendar"
	tell calendar varCal
		show (first event where its summary = varName)
	end tell
end tell

tell application "System Events"
	tell process "Calendar"
		set frontmost to true
		click menu item "Edit Event" of menu "Edit" of menu bar 1
	end tell
end tell

Thank you @Jim for taking the time to look into this. Your new AppleScript works but in my case it causes Keyboard Maestro to hang after entering the reminder time. Aborting the Macro allows the AppleScript to finish and the Event opens up in Calendar. It must be something specific to my setup if it works for you. I am wondering if I need to put a pause in the AppleScript to allow it to complete the adding of the new event before trying to open Calendar - it feels like it must be jumping to the last step too quickly. Like I said, when it hangs, Aborting the Macro allows it to complete and opens the event just fine in Calendar.

Hey @Zabobon,

Try this one.

I made some small changes and used a Keyboard Maestro action to activate editing the event instead of System Events.

See if it gets around the hang.

-Chris


Calendar ⇢ Create New Event v1.01.kmmacros (8.5 KB)

1 Like