View Single Post
Good !

Here it is en clair.

(I've edited it slightly to make sure that the Applescript text delimiters are properly restored at the end)

Code:
property pVer : "2.0"

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, 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 my text item delimiters to 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 my text item delimiters to 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