content: -- Finds and focuses on projects containing a string supplied by the user
  -- WITH LAUNCHBAR:
  -- 		-- You can pass a search string to the script via LaunchBar (by selecting the script and pressing the space bar)
  -- WITHOUT LAUNCHBAR:
  --		-- The user will be prompted for a search string
  
  property pTitle : "Find OmniFocus Projects Fast"
  property pVersion : "0.8"
  property pstrDBPath : "~/Library/Caches/com.omnigroup.OmniFocus/OmniFocusDatabase2"
  property pstrFieldDelimiter : "~|~"
  property plstObjects : {}
  property pSearch : ""
  property pCreateProj : "Create project"
  property pReSearch : "Search again"
  property pblnNewWin : true
  
  -- Ver 0.1 adds alphabetic sorting
  -- Ver 0.2 defaults to opening a new window [this can be reset by editing pblnNewWin above to false]
  -- Ver 0.3 brings OmniFocus to the front
  -- Ver 0.4 fixes the handling of search strings and project names which contain double quotes
  -- Ver 0.5 offers to create a project if no match found
  -- Ver 0.6 also offers repeat search if no match found
  -- Ver 0.7 checks that the window is in Project view
  -- Fixes focus problem (Dialogs appearing behind other windows when used with LaunchBar)
  
  on handle_string(strSearch)
  	if length of strSearch > 0 then FindParas(strSearch)
  end handle_string
  
  on run
  	tell application id "sevs"
  		activate
  		set strSearch to text returned of (display dialog "Enter a phrase in the name of the project(s)" default answer pSearch with title pTitle & "  Ver " & pVersion)
  	end tell
  	set pSearch to strSearch
  	if length of strSearch > 0 then FindParas(strSearch)
  end run
  
  on FindParas(strSearch)
  	set blnCreated to false
  	set blnFound to false
  	
  	-- LOOP UNTIL SOMETHING FOUND OR CREATED, OR UNTIL THE USER TAPS "ESC"
  	repeat while (blnCreated is false and blnFound is false)
  		set strQuery to "select t.name from task t where projectInfo is not null and t.name like \"%" & EscapeQuote(strSearch) & "%\" order by t.name;"
  		--tell application "Finder" to set the clipboard to strQuery
  		set plstObjects to {}
  		try
  			set plstObjects to paragraphs of runquery(strQuery)
  		on error
  			tell application id "sevs"
  				activate
  				display dialog "The SQL schema for the OmniFocus cache may have changed in a recent update of OF." & return & return & ¬
  					"Look on the OmniFocus user forums for an updated version of this script." buttons {"OK"} default button {"OK"} with title pTitle & "  Ver " & pVersion
  			end tell
  			return
  		end try
  		
  		if plstObjects is {} then -- NO MATCHFOUND 
  			-- OFFER: ESC, CREATE PROJECT, OR RUN AMENDED SEARCH
  			tell application id "sevs"
  				activate
  				set varResponse to display dialog "No matches found ..." & ¬
  					return & return & "Try a modified search, or create a project of this name ?" default answer strSearch buttons {"Esc", pCreateProj, pReSearch} default button {pReSearch} with title pTitle & "  Ver " & pVersion
  				set strButton to button returned of varResponse
  			end tell
  			if strButton is pCreateProj then
  				tell application id "com.omnigroup.omnifocus"
  					tell default document
  						set oProj to make new project with properties {name:strSearch}
  						set oWin to make new document window
  						tell oWin
  							set focus to {oProj}
  							tell sidebar
  								select {oProj}
  							end tell
  							tell content
  								select {oProj}
  							end tell
  						end tell
  						set oTask to make new task at end of tasks of oProj
  						tell oWin to tell content to select {oTask}
  					end tell
  				end tell
  				tell application id "com.apple.finder" to activate front window of application id "com.omnigroup.omnifocus"
  				return
  			else if strButton is pReSearch then
  				set strSearch to text returned of varResponse
  			else
  				return
  			end if
  		else -- At least one match found
  			set blnFound to true
  			set lngMatches to length of plstObjects
  			if lngMatches > 1 then
  				tell application id "sevs"
  					activate
  					set varChoice to choose from list plstObjects with prompt (lngMatches as string) & " projects where name contains " & quote & strSearch & quote & return & return & ¬
  						"Select one or more:" default items {first item of plstObjects} with title pTitle & "  Ver " & pVersion with multiple selections allowed
  				end tell
  				if varChoice is false then return
  				varChoice
  				
  				set strQuery to "select persistentidentifier from task where "
  				repeat with oChoice in varChoice
  					set strQuery to strQuery & "name like \"" & EscapeQuote(oChoice) & "\"" & " or "
  				end repeat
  				set strQuery to text 1 thru -5 of strQuery
  				set strQuery to strQuery & ";"
  				
  				set plstObjects to paragraphs of runquery(strQuery)
  			else
  				set plstObjects to {runquery("select persistentidentifier from task where name like \"" & EscapeQuote(first item of plstObjects) & "\";")}
  			end if
  		end if
  	end repeat
  	
  	-- If we have got this far, something must have been found 
  	-- SELECT MATCHED/CREATED/SELECTED PROJECT(S) IN NEW (OR EXISTING) OF GUI WINDOW
  	tell application id "com.omnigroup.omnifocus"
  		tell front document
  			repeat with iPara from 1 to length of plstObjects
  				set item iPara of plstObjects to project id ((item iPara of plstObjects) as string)
  			end repeat
  			
  			if pblnNewWin then
  				set oWin to make new document window
  			else
  				try
  					set oWin to front document window
  				on error
  					set oWin to make new document window
  				end try
  			end if
  			
  			tell oWin
  				if selected view mode identifier is not "project" then set selected view mode identifier to "project"
  				set focus to plstObjects
  				tell sidebar
  					try
  						select plstObjects
  					on error
  						set selected smart group identifier to "all-projects"
  						select plstObjects
  					end try
  				end tell
  			end tell
  		end tell
  	end tell
  	
  	tell application id "com.apple.finder"
  		activate front window of application id "com.omnigroup.omnifocus"
  	end tell
  end FindParas
  
  on runquery(strQuery)
  	set strCmd to "sqlite3 -separator '" & pstrFieldDelimiter & "' " & pstrDBPath & space & quoted form of strQuery
  	-- tell application "Finder" to set the clipboard to strQuery
  	do shell script strCmd
  end runquery
  
  on EscapeQuote(strSearch)
  	FindReplace(strSearch, "\"", "\"\"")
  end EscapeQuote
  
  on FindReplace(strText, strFind, strReplace)
  	if the strText contains strFind then
  		set AppleScript's text item delimiters to strFind
  		set lstParts to text items of strText
  		set AppleScript's text item delimiters to strReplace
  		set strText to lstParts as string
  		set AppleScript's text item delimiters to space
  	end if
  	return strText
  end FindReplace
  