Is there a script for pulling calendar info into task notes?

I’d like to pull the location of events in iCal into a related note in OF 2.

I meet families in their community and make a task in OF to confirm the meeting. I’d like the date, time, and location to appear in that in the notes section of that task. Is this possible?

I’ve not scripted Calendar before, but this should be technically possible. I’ll take a look and see if anyone has done something like this before and post anything I find here.

I have an old script by Rob Trew for creating tasks from Calendar appointments. In 2015, something changed in AppleScript so that when you copy multiple appointments to the clipboard and run this code:

set strClip to (text of ((the clipboard) as record)) as string

The result is truncated to the first appointment only. I searched and asked around, but I could not find the new, correct way to form this command to convert the multiple appointments to a string without truncating.

As a workaround, I set up a Keyboard Maestro macro to copy the appointments, open Text Edit, paste into a plain text document, then select all and cut them back to the clipboard, and then run the old Applescript. That has worked for the last six months.

I’d prefer to have the conversion happen entirely in the Applescript.

Any help would be appreciated.

Here is the original AppleScript from Rob Trew. Maybe this can be adapted for @Jasmynp’s purposes:

property piCalProject : "iCalProject"
property piCalContext : "iCalContext"

property pDatePrefix : "\\rscheduled "
property pTimePrefix : "[0-9] from [0-9]"
property pTimeDelim : " to "
property plngPrefix : length of pDatePrefix

on run
	set strClip to (text of ((the clipboard) as record)) as string
	
	set lstActions to ParseClip(strClip)
	Add2OF(lstActions)
end run

on Add2OF(lstActions)
	tell application id "OFOC"
		tell default document
			set oProj to my GetProject(piCalProject)
			set oContext to my GetContext(piCalContext)
			
			repeat with oAct in lstActions
				set {strAction, strDate, strFrom, strTo} to oAct
				set strStart to strDate & space & strFrom
				set strDue to strDate & space & strTo
				try
					set dteStart to my date strStart
					set dteDue to my date strDue
				on error
					display alert ("Unexpected date: " & strStart) as string
					return
				end try
				tell oProj to make new task with properties {name:strAction, context:oContext, start date:dteStart, due date:dteDue}
			end repeat
		end tell
		activate
	end tell
end Add2OF

on GetProject(strName)
	tell application id "OFOC"
		tell default document
			set lstProj to flattened projects where name = strName
			if lstProj ≠ {} then
				first item of lstProj
			else
				make new project with properties {name:strName}
			end if
		end tell
	end tell
end GetProject

on GetContext(strName)
	tell application id "OFOC"
		tell default document
			set lstContext to flattened contexts where name = strName
			if lstContext ≠ {} then
				first item of lstContext
			else
				make new context with properties {name:strName}
			end if
		end tell
	end tell
end GetContext

on ParseClip(strClip)
	set {dlm, my text item delimiters} to {my text item delimiters, "

"}
	set my text item delimiters to return & return
	set lstRecs to text items of strClip
	set lstActions to {}
	repeat with oRec in lstRecs
		set lngDate to (PatternMatch(oRec, pDatePrefix)) + 1
		try
			set strAction to text 1 thru (lngDate - plngPrefix) of oRec
		on error
			return
		end try
		set strRest to text lngDate thru -1 of oRec
		set {dlm, my text item delimiters} to {my text item delimiters, return}
		set strDateTime to first text item of strRest
		set lngTime to (PatternMatch(strDateTime, pTimePrefix))
		if lngTime > 0 then
			set strDate to text 1 thru (lngTime - 7) of strDateTime
			set strTime to text lngTime thru -1 of strDateTime
			set {dlm, my text item delimiters} to {my text item delimiters, pTimeDelim}
			set {strFrom, strTo} to text items of strTime
		else
			set strDate to text 5 thru -1 of first text item of strDateTime
			set {strTime, strFrom, strTo} to {"", "", ""}
		end if
		set end of lstActions to {strAction, strDate, strFrom, strTo}
	end repeat
	set my text item delimiters to dlm
	return lstActions
end ParseClip

(* Returns position of last character of matched pattern *)
on PatternMatch(strText, strPattern)
	try
		(do shell script "echo " & quoted form of strText & " | perl -ne 'if (m/(" & strPattern & ")/) {print \"$+[1]\"}'") as integer
	on error
		0
	end try
end PatternMatch
2 Likes

This is awesome!!! Thanks so much!