The Omni Group
These forums are now read-only. Please visit our new forums to participate in discussion. A new account will be required to post in the new forums. For more info on the switch, see this post. Thank you!

Go Back   The Omni Group Forums > Developer > AppleScripting Omni Apps
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
Job $$ : Apple Script to backup each context separately Thread Tools Search this Thread Display Modes
Hello,


I need a simple apple script which open the software“OmniFocus” and export each “subcontext” as a csv file.

Right now I have to click on each subcontext, click onfile-> export which takes a lot of time. I want to automate the processusing apple script.

I already have a script which open Omnifocus, and export everything.


Best Regards

Last edited by smartmobilesoftware; 2010-09-06 at 02:48 PM..
 
Here is a rough draft with which you are welcome to experiment

Code:
-- Export tasks of each Omnifocus context to a separate .csv text file on the User Desktop
-- Ver 0.01
-- Copyright ©2010 Robin Trew, UK. All rights reserved.

property pOutPutFolder : path to desktop
-- property pFolderName : "OFContexts "
property pstrHeader : "Task ID,Type,Task,Project,Context,Start Date,Due Date,Completion Date,Duration,Flagged,Notes" & return
property pDblQuote : "\""
property pDbldblQuote : "\"\""
property pComma : ","
property pType : ",Action"

on run
	set lstContextTree to ContextTasks()
	WriteContextFiles(lstContextTree)
end run

on WriteContextFiles(lstContextTree)
	if length of lstContextTree > 0 then
		repeat with oContext in lstContextTree
			set {strContextName, lstContextChiln, lstTasks} to oContext
			if length of lstTasks > 0 then
				set lngTask to 1
				set strCSV to pstrHeader
				repeat with oTask in lstTasks
					set {strTaskName, lstTaskChiln, strProject, strContext, dteStart, dteDue, dteDone, lngMins, blnFlagged, strNote} to oTask
					
					set strCSV to strCSV & (lngTask as string) & pType & WrapQuote(strTaskName) & WrapQuote(strProject) & WrapQuote(strContext)
					if dteStart is not missing value then
						set strCSV to strCSV & WrapQuote(dteStart as string)
					else
						set strCSV to strCSV & pComma
					end if
					
					if dteDue is not missing value then
						set strCSV to strCSV & WrapQuote(dteDue as string)
					else
						set strCSV to strCSV & pComma
					end if
					
					if dteDone is not missing value then
						set strCSV to strCSV & WrapQuote(dteDone as string)
					else
						set strCSV to strCSV & pComma
					end if
					
					if lngMins is not missing value then
						set strCSV to strCSV & pComma & (lngMins as string)
					else
						set strCSV to strCSV & pComma
					end if
					
					set strCSV to strCSV & pComma
					if blnFlagged then
						set strCSV to strCSV & "1"
					else
						set strCSV to strCSV & "0"
					end if
					
					if length of strNote > 0 then
						set strCSV to strCSV & WrapQuote(strNote) & return
					else
						set strCSV to strCSV & pComma & return
					end if
					set lngTask to lngTask + 1
				end repeat
				WriteCSVFile(strCSV, strContextName)
				
			end if
			if length of lstContextChiln > 0 then WriteContextFiles(lstContextChiln)
		end repeat
	end if
end WriteContextFiles

on WriteCSVFile(strText, strFileName)
	set fTemp to (pOutPutFolder & strFileName & ".CSV") as text
	try
		close access file fTemp
	end try
	
	try
		open for access file fTemp with write permission
	on error
		display dialog strFileName & ".csv seems to be open in another program ...  Close and try again."
		return
	end try
	set eof file fTemp to 0
	write strText to file fTemp starting at eof
	close access file fTemp
end WriteCSVFile

-- wrap string in double quotes if it contains any commas or double quotes
-- and duplicate any embedded double quotes
-- ALSO precede with a comma ...
on WrapQuote(strField)
	set blnWrap to strField contains "\""
	if blnWrap then
		set strOldDelim to text item delimiters
		set text item delimiters to pDblQuote
		set lstWords to text items of strField
		set text item delimiters to pDbldblQuote
		set strNew to lstWords as string
		set text item delimiters to strOldDelim
		pComma & pDblQuote & strNew & pDblQuote
	else
		if strField contains pComma then
			pComma & pDblQuote & strField & pDblQuote
		else
			pComma & strField
		end if
	end if
end WrapQuote


-- Return a nested applescript list representing the context tree, with attached tasks
on ContextTasks()
	tell application "OmniFocus"
		tell default document
			set lstContexts to contexts
			if length of lstContexts > 0 then
				my ContextTree(lstContexts)
			else
				{}
			end if
		end tell
	end tell
end ContextTasks

-- Translate a collection of contexts to a nested applescript list
on ContextTree(lstContexts)
	using terms from application "OmniFocus"
		set lstTree to {}
		repeat with oContext in lstContexts
			tell oContext
				set lstChiln to contexts
				if length of lstChiln > 0 then set lstChiln to my ContextTree(lstChiln)
				set lstTasks to tasks
				if length of lstTasks > 0 then set lstTasks to my TaskTree(lstTasks)
				set end of lstTree to {name, lstChiln, lstTasks}
			end tell
		end repeat
		lstTree
	end using terms from
end ContextTree

-- Translate a collection of tasks to a nested applescript list
on TaskTree(lstTasks)
	using terms from application "OmniFocus"
		set lstTree to {}
		repeat with oTask in lstTasks
			tell oTask
				set lstChiln to tasks
				if length of lstChiln > 0 then set lstChiln to my TaskTree(lstChiln)
				set oContext to context of oTask
				if oContext is missing value then
					set strContext to ""
				else
					set strContext to name of oContext
				end if
				set oProject to containing project of oTask
				if oProject is missing value then
					set strProject to ""
				else
					set strProject to name of oProject
				end if
				set end of lstTree to {name, lstChiln, strProject, strContext, start date, due date, completion date, estimated minutes, flagged, note}
			end tell
		end repeat
		lstTree
	end using terms from
end TaskTree
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Apple Script or Other controls fmpdan OmniGraffle General 1 2013-02-25 08:57 AM
setting project + context via script (modifying a DEVONthink script) bernd OmniFocus Extras 2 2012-09-08 01:10 PM
I need some help with apple script and excel journeywright AppleScripting Omni Apps 0 2009-05-10 05:37 PM
Viewing Single Actions w/ no context separately? jka33331 OmniFocus 1 for Mac 5 2008-10-13 05:58 PM
OmniFocus QuickPick for Apple Backup 3 carouzal OmniFocus 1 for Mac 0 2007-06-21 08:40 AM


All times are GMT -8. The time now is 06:11 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.