Jim's Add an Event to Apple Calendar Macro:

Great!! Works perfectly now. I changed the Calendar names to match mine and added back in the option to choose Locations that was in your original script.
Thank you so much @ccstone and @Jim, I can see that this is going to become one of my most used Macros. Like I said, what makes it really good for me is that while the Dialog boxes are flashing up, I can copy and paste text in from whatever file or webpage or email has prompted me to make the new event. This is much better than the inbuilt Apple context menu system.

1 Like

Ah... the new version will not let me select any text from the front Application. I've tried editing the AppleScript to allow me to do that but it is beyond my skills. Really sorry, I don't want to use up more of your time.

So, old version allows selection of text and copy and paste of items into the dialog boxes.
New version doesn't allow that (just makes a beep sound if I try and select anything else) but does open the event in Calendar.

I think this must be the reason.

Yes. Try this one:

Updated Script – Allows Copying From the Front Application
set theDate to short date string of (current date)

tell application "System Events"
   
   set varCal to (choose from list {"TEST CALENDAR"} with title "List of Calendars" with prompt "Please select a calendar" default items "TEST CALENDAR" OK button name {"Choose"}) as string
   
   display dialog "Enter the event:" default answer "Appointment with "
   set varName to text returned of the result
   
   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
   
   display dialog "Enter the description:" default answer "→ "
   set varDesc 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
   
end tell

set varStart to date (varDate & space & varTime)
set varEnd to (date (varDate & space & varTime)) + (1 * hours)

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, start date:varStart, end date:varEnd}
   tell newEvent
      make sound alarm at end of sound alarms with properties {trigger interval:varRemind, sound name:"Crystal"}
   end tell
   
   show newEvent
   
end tell
1 Like

Hi @ccstone

Interestingly, that script wouldn't run at all from Keyboard Maestro (I changed "TEST CALENDAR" to the name of my actual Calendar, but maybe I needed to do more).

But adding on the bit of AppleScript you had at the end to the original AppleScript I had adapted from @Jim's first version worked and opened the new event successfully. Here is the working Keyboard Maestro Macro which has the modified AppleScript inside an Action:

Calendar ⇢ Create New Event v1.02.kmmacros (33.8 KB)

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