Search OO with Applescript

In my haste to learn Applescript, and apply it to OO, I didn’t check to see if this was a core function. I checked the default Find Window, and although I didn’t see it there it doesn’t rule out the possibility that I’ve missed it somehow.

This script will take your query and search the text of the topic cells for a match. On finding a match it collapses all rows not containing the query, and then selects the rows which do.

set query to the text returned of (display dialog "Enter Your Search Term" default answer "QUERY" with title "Search OmniOutliner Front Document" with icon path to resource "OmniOutliner.icns" in bundle (path to application "OmniOutliner"))

tell application id "OOut"
	
	try
		set doc_name to name of front document
	on error
		activate
		display dialog "No Document Open"
	end try
	
	set the_result to {}
	set every_row to every row of front document
	
	repeat with r in every_row
		set row_text to text of topic cell of r
		if query is in row_text then
			set end of the_result to r
			collapseAll rows of front document
		end if
	end repeat
	
	repeat with i in the_result
		set the_ancestors to the ancestors of i
		repeat with j in the_ancestors
			set expanded of j to true
		end repeat
	end repeat

	set index of window 1 where name contains doc_name to 1
	activate window 1
	
	if the_result is {} then
		activate
		display dialog "The word " & query & " was not found." with icon path to resource "OmniOutliner.icns" in bundle (path to application "OmniOutliner")
	else
		tell front document to select items of the_result
	end if

end tell
3 Likes

Oh, wow! This is such a helpful script for really long outlines. I love that it highlights rather than just expands results.

Now I can combine some of my research outlines without fear of being able to find something.

Very useful.

Here, FWIW is another way of writing it. The main difference is that it remembers the previous search term, to allow for editing or adjustment. It may also be fractionally faster, particularly with large documents.

property pstrQuery : "search term"

tell application "OmniOutliner"
    
    set ds to (documents as list)
    if (length of ds) > 0 then
        
        activate
        set oo to it
        set strQuery to text returned of ((display dialog "Search:" default answer pstrQuery with title "Search OmniOutliner front document" with icon path to resource "OmniOutliner.icns" in bundle (path to oo)))
        
        if strQuery is not "" then
            set pstrQuery to strQuery
            
            tell item 1 of ds
                
                set refRows to a reference to (rows where topic contains strQuery)
                
                if (count of refRows) > 0 then
                    collapseAll rows
                    
                    tell refRows
                        set expanded of ancestors to true
                        set expanded to true
                        select
                    end tell
                    
                else
                    display dialog "Search term not found: " & strQuery with title "Search OmniOutliner front document" with icon path to resource "OmniOutliner.icns" in bundle (path to oo)
                end if
                
            end tell
        end if
    end if
end tell
1 Like