View Single Post
Here is an illustrative first draft, which transfers any activities selected in OmniPlan (with their sub-activities) to a new date-stamped folder in OF.

--

Code:
-- ver 0.2 (Exports to an OF Folder, rather than making all tasks children of the same project)

-- All activities selected in OmniPlan GUI transferred (with their descendants) to 
-- a new date-stamped *folder* in OmniFocus, and each parent element selected in OP becomes a project in the OF folder,
-- with the descendent OP sub-tasks exported as a hierarchy of OF tasks
--
-- An alternative line shown in the FillofProj() function would exclude transfer of estimated minutes

property pstrOFProjectName : "OmniPlan import"

on run
	set lstTasks to OPSelected()
	
	if length of lstTasks > 0 then
		PlaceInOF(lstTasks)
	else
		display dialog "Select parent task(s) in OmniPlan, and try again"
	end if
end run

-- Get a nested list of the selected OmniPlan activities
on OPSelected()
	tell application "OmniPlan"
		tell front window
			set lstOPTasks to selected tasks
			if (count of lstOPTasks) > 0 then
				return my Tasks2NameList(lstOPTasks)
			else
				return {}
			end if
		end tell
	end tell
end OPSelected

-- -- Translate OmniPlan activities to a nested list of activity names
on Tasks2NameList(lstOPTasks)
	using terms from application "OmniPlan"
		set lstTasks to {}
		repeat with oTask in lstOPTasks
			tell oTask
				set lstChiln to child tasks
				if (lstChiln is not missing value) and (length of lstChiln) > 0 then
					set end of lstTasks to {name, my Tasks2NameList(lstChiln), starting date, ending date, note, duration / 60}
				else
					set end of lstTasks to {name, {}, starting date, ending date, note, duration / 60}
				end if
			end tell
		end repeat
		return lstTasks
	end using terms from
end Tasks2NameList

-- Place nested list of activity names in a new OF project
on PlaceInOF(lstActivities)
	if length of lstActivities > 0 then
		tell application "OmniFocus"
			tell front document
				set oFolder to make new folder with properties {name:pstrOFProjectName & " " & my SysDate()}
				my FillOFFolder(oFolder, lstActivities)
			end tell
		end tell
	end if
end PlaceInOF

on SysDate()
	current date
end SysDate

-- Transfer activities to the specified project (or parent task)
on FillOFFolder(oFolder, lstActivities)
	using terms from application "OmniFocus"
		tell oFolder
			repeat with oAct in lstActivities
				set {strName, lstChiln, dteStart, dteDue, strNote, lngMins} to oAct
				if strNote is missing value then
					set oProj to make new project with properties {name:strName, start date:dteStart, due date:dteDue, estimated minutes:lngMins}
					-- set oProj to make new project with properties {name:strName, start date:dteStart, due date:dteDue}
				else
					set oProj to make new project with properties {name:strName, start date:dteStart, due date:dteDue, note:strNote, estimated minutes:lngMins}
					-- set oProj to make new project with properties {name:strName, start date:dteStart, due date:dteDue, note:strNote}
				end if
				if length of lstChiln > 0 then
					my FillOFProj(oProj, lstChiln)
				end if
			end repeat
		end tell
	end using terms from
end FillOFFolder


-- Transfer activities to the specified project (or parent task)
on FillOFProj(oProj, lstActivities)
	using terms from application "OmniFocus"
		tell oProj
			repeat with oAct in lstActivities
				set {strName, lstChiln, dteStart, dteDue, strNote, lngMins} to oAct
				if strNote is missing value then
					-- set oTask to make new task with properties {name:strName, start date:dteStart, due date:dteDue, estimated minutes:lngMins}
					set oTask to make new task with properties {name:strName, start date:dteStart, due date:dteDue}
				else
					-- set oTask to make new task with properties {name:strName, start date:dteStart, due date:dteDue, note:strNote, estimated minutes:lngMins}
					set oTask to make new task with properties {name:strName, start date:dteStart, due date:dteDue, note:strNote}
				end if
				if length of lstChiln > 0 then
					my FillOFProj(oTask, lstChiln)
				end if
			end repeat
		end tell
	end using terms from
end FillOFProj

Last edited by RobTrew; 2011-09-10 at 12:10 AM.. Reason: Workaround for a new bug in the Applescript library