Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator with Calendar

I am a novice at programing. I am trying to find a quick way to copy all events on a given day, which I can then paste into a different day. I am trying to use Automator to accomplish this, but it is giving an error at the copy step. What am I doing wrong?


Posted on Oct 5, 2020 4:34 PM

Reply
4 replies

Oct 6, 2020 12:45 AM in response to Mt. Fuji

You're going to have a really hard time doing this via Automator actions. It just doesn't have the hooks for it.


As you can see from the log, the actions you have are simply going to create a text-based list of events. You can't use this to create new events using Automator's New Calendar Event action, because the only input this action accepts is a list of contacts for the event. You have to specify the dates/times/etc. separately.


In any case, the clipboard is usually the last, and least preferable, way to automate anything. It's usually much better to go direct.


To do that, though, you're going to have to step outside of Automator actions and into AppleScript.


The following script should do what you describe (although there are many possible gotchas, so be careful running it on production data). It asks for a date to move from, finds those events, asks for a date to move to, does some quick calculations then updates the event to the new date


The comments should be self-explanatory, but feel free to ask if it isn't clear.



Oct 6, 2020 12:49 AM in response to Camelot

-- ask the user for FROM date
set d to display dialog "Move events from which date?" default answer (short date string of (get current date))
try
	-- did they supply a valid date?
	set eventDate to date (text returned of d)
on error
	--if not, bail
	display dialog "Invalid date specified" buttons "Cancel" default button 1
end try

-- calculate the start and end times of the day in question
-- this defines the time range of events to move
copy eventDate to eventStart
set time of eventStart to 0 -- midnight
copy eventDate to eventEnd
set time of eventEnd to (23 * 60 * 60) + (59 * 60) + 59 -- 11:59:59pm

tell application "Calendar"
	-- get events on the calendar whose start date is within the above range
	set events2Move to every event of calendar "Work" whose start date is greater than eventStart and start date is less than eventEnd and allday event is false
end tell

-- did we find any events?
if (count events2Move) = 0 then
	-- if not, let the user know, and bail out.
	display dialog "No events found on " & short date string of eventDate buttons "Cancel" default button 1
end if

Oct 6, 2020 12:49 AM in response to Camelot

-- con'd (script was too long for one post)


-- now we know we have some events to move, so ask when to move them to
set d to display dialog ("Move " & (count events2Move & " events to which date?")) default answer (short date string of (get current date))
try
	-- same logic as above to see if they supplied a valid date
	set newDate to date (text returned of d)
on error
	display dialog "Invalid target date specified" buttons "Cancel" default button 1
end try

-- if we get here we have:
-- a) one or more events to move
-- and b) a valid date to move them to

tell application "Calendar"
	repeat with eachEvent in events2Move
		--  capture the start and end times of the current event
		set eventStartTime to time of (get start date of eachEvent)
		set eventEndTime to time of (get end date of eachEvent)
		-- reset the event's times to the new date
		set end date of eachEvent to (newDate + eventEndTime)
		set start date of eachEvent to (newDate + eventStartTime)
	end repeat
end tell


Automator with Calendar

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.