Selected Event in Calendar → Things To Do

This script will make to-do’s in Things based on the name and date of the selected event in Calendar.

Thank you to johneday for his clever AppleScript to reference selected Calendar events using sqlite3.

set defaultsReply to (do shell script "defaults read com.apple.ical SelectedEvents")
set selectedEvents to parseDefaults(defaultsReply)

if selectedEvents = {} then
	display notification "Please try again" with title "No Calendar Event Selected"
	return
end if

set eventReferenceList to {}
repeat with sEvent in selectedEvents
	set {eventID, calendarID} to sqlQuery(sEvent)
	tell application "Calendar"
		set eventReference to event id eventID of calendar id calendarID
		
		
		set theName to summary of eventReference
		set theDate to start date of eventReference
		
		tell application "Things3"
			set newToDo to make new to do with properties {name:theName, due date:theDate}
		end tell
		
		
	end tell
end repeat

on parseDefaults(resultText)
	set localUIDs to {}
	set {TID, text item delimiters} to {text item delimiters, quote}
	set resultItems to text items of resultText
	set text item delimiters to TID
	repeat with i from 1 to (count resultItems)
		if i mod 2 = 0 then set end of localUIDs to resultItems's item i
	end repeat
	return localUIDs
end parseDefaults


on sqlQuery(localUID)
	local dateString, localUID
	if localUID contains "/" then
		set {TID, text item delimiters} to {text item delimiters, "/"}
		set {dateString, localUID} to text items of localUID
		set text item delimiters to TID
	end if
	
	set sqlText to "
        SELECT DISTINCT zcalendaritem.zshareduid AS eventID
             , znode.zuid as calID
          FROM zcalendaritem
          JOIN znode
            ON znode.z_pk = zcalendaritem.zcalendar
           AND zcalendaritem.zlocaluid = '" & localUID & "'
        ;"
	
	set sqlPath to POSIX path of (path to library folder from user domain) & "Calendars/Calendar Cache"
	set {TID, text item delimiters} to {text item delimiters, "|"}
	set {eID, cID} to text items of (do shell script "echo " & quoted form of sqlText & " | sqlite3 " & quoted form of sqlPath)
	set text item delimiters to TID
	return {eID, cID}
end sqlQuery
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s