View Single Post
But here is a rough sketch of what it might look like. Note that Mail must be closed. (The project list in the plist file is only read when Mail is launched).

(OmniFocus doesn't have to be running).

Very experimental code, so back up
~/Library/Preferences/ca.indev.MailTags.plist
and proceed with caution. No guarantees that it wont eat your existing MailTags project list. This draft aims to import the OF projects with the prefix "OF:" to distinguish them from other projects in the Mailtags project list, which will be left intact.

In this draft, if a project is completed in OF, it will be removed from the MailTags prompt list (though not from any tagged emails) when the script is next run.

Code:
property pTitle : "Merge OF Project list into Mailtags Project list"
property pVer : "0.04"

property pstrOFPrefix : "OF:"
-- MUST START WITH THE KEYWORD 'WHERE'  (unless completely blank: "" )
property pstrProjectSet : "where dateCompleted is null"

-- Some snippets illustrating a possible approach to merging the OmniFocus project list with the MailTags project list
-- Intended for reference only

-- 	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
-- 	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
-- 	THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
-- 	IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
-- 	ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-- 	 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
-- 	 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
-- 	 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
-- 	 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

property pstrPListPath : "~/Library/Preferences/ca.indev.MailTags.plist"
property pstrDBPath : "~/Library/Caches/com.omnigroup.OmniFocus/OmniFocusDatabase2"

property pstrProjectSQL : "select \"" & pstrOFPrefix & "\" || name, \"<eor>\" from projectinfo p join task on p.pk=task.persistentidentifier " & pstrProjectSet & " order by name"

property plstMTProjects : {}
property plstOFProjects : {}
property plstUnion : {}
property pEOR : "<eor>"
property pFldDelim : "<fldelim>"

on run
	-- WARN USER IF MAIL IS RUNNING
	tell application id "com.apple.systemevents"
		set blnWarned to false
		set blnWasRunning to false
		repeat while (count of (processes where title = "Mail")) > 0
			if not blnWarned then
				set blnWasRunning to true
				activate
				set varResponse to display dialog "This script requires Mail to be closed ..." buttons {"Esc", "Close Mail"} with title pTitle & "  " & pVer
				if button returned of varResponse = "Esc" then return
				set blnWarned to true
				tell application id "com.apple.mail" to quit
			end if
			do shell script "sleep " & (0.5) as string
		end repeat
		
		-- READ THE EXISTING MAILTAGS PROPERTY LIST
		tell contents of property list file pstrPListPath
			set refPlist to a reference to (property list item "mailTagsProjects")
			set lstRecs to value of refPlist
			if length of lstRecs > 0 then
				set {plstMTProjects, propsOther} to {items 1 thru -2 of lstRecs, item -1 of lstRecs}
			else
				set {plstMTProjects, propsOther} to {{}, {}}
			end if
			repeat with i from 1 to length of plstMTProjects
				set item i of plstMTProjects to |title| of item i of plstMTProjects
			end repeat
			
			-- READ THE FULL OMNIFOCUS PROPERTY LIST (OmniFocus does not need to be running)
			my RunSQL(pstrProjectSQL, "All projects")
			set lngProjects to length of plstOFProjects
			
			-- COMBINE THE TWO LISTS OF NAMES (MAILTAGS & OMNIFOCUS)
			-- 			set plstUnion to my union(plstMTProjects, plstOFProjects)
			set plstUnion to my MergeLists(plstOFProjects, plstMTProjects)
			
			-- AND TURN THEM INTO RECORDS WITH |TITLE| FIELDS
			repeat with i from 1 to length of plstUnion
				set item i of plstUnion to {|title|:item i of plstUnion}
			end repeat
			if length of propsOther > 0 then set end of plstUnion to propsOther
			
			-- THEN PUT THEM BACK INTO THE PLIST FILE
			set value of refPlist to plstUnion
		end tell
		
		activate
		if blnWasRunning then
			set varResponse to display dialog (lngProjects as string) & " OmniFocus Projects now in MailTags project list." buttons {"Esc", "Re-launch Mail"} with title pTitle & "  " & pVer
			if button returned of varResponse = "Esc" then return
			tell application id "com.apple.mail" to activate
		else
			display dialog (lngProjects as string) & " OF Projects now in MailTags project list." buttons {"OK"} with title pTitle & "  " & pVer
		end if
	end tell
end run

on RunSQL(strSQL, strUserQuery)
	set strCmd to "sqlite3 -separator '" & pFldDelim & "' " & pstrDBPath & space & quoted form of strSQL
	set text item delimiters to pFldDelim & pEOR & return -- strip out the end of line \r as well as the record delimiter
	try
		set plstOFProjects to text items 1 thru -2 of ((do shell script strCmd) & return)
		set pblnError to false
	on error strError
		set plstOFProjects to {strUserQuery & return & return & strError}
		set pblnError to true
	end try
end RunSQL

-- Specified OF projects preceded by  any members of MailTags projects that do NOT begin with the OF prefix
on MergeLists(lstOF, lstMT)
	repeat with i from 1 to length of lstMT
		set strProj to item i of lstMT
		if not (strProj begins with pstrOFPrefix) then set beginning of lstOF to strProj
	end repeat
	lstOF
end MergeLists

Last edited by RobTrew; 2011-01-01 at 04:13 AM.. Reason: ver 0.04