Automating Find & Replace in OmniOutliner

I would be very grateful for help in executing multiple Find and Replace operations inside OmniOutliner (OO), using Applescript. The following code runs without error in BBEdit’s integration with Applescript:

tell application “BBEdit”
activate
replace “word press” using “WordPress” searching in text 1 of text window 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}

end tell

I repeat “replace … false}” segment multiple times, and each time there is a different pair of items to find and replace. So, once I tell Script Editor to Run, a very large number of corrections are executed from one command.

On trying the following code inside OmniOutliner I get an error message from the Applescript compiler:
tell application “OmniOutliner”
activate
replace “neighborhood” using “neighbourhood” searching in text 1 of text window 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
end tell

The error message reads: “syntax … Expected end of line, etc. but found identifier.”

I have searched in vain for sample Applescript code that will do multiple Find and Replace operations, and so I’m here seeking help. I am trying to avoid manually invoking the Find-and-Replace procedure N different times while working in OO.

Thanks in advance for looking at my problem and any help you might offer.

BTW. Here Looking for Examples of OmniOutliner Applescripts is the last time this topic seems to have been discussed in our Forum.

Each scriptable app has its own Scripting Dictionary. Many of the command options you are using aren’t available in OO.

For example, using Javascript For Automation, you can try something like:

(() => {
    'use strict';

    // jxaContext :: IO ()
    const jxaContext = () => {
        // main :: IO ()
        const main = () => {
            const
                app = Application("OmniOutliner"),
                text = app.documents[0].selectedRows()[0].topic
            return app.replace(text, {
                //matchingRegularExpression: "neigh*",
                string: "neighborhood",
                replacement: "neighbuorhood"
            })
        };

        // MAIN --
        return main();
    };

    return jxaContext();
})();

I translated an Applescript version by user JKane and adapted to your request.

I’m afraid that you have two issues here.

First, you can’t use smart quotes in applescript. They are the source of the specific error message which you mention, so make sure that all of your quotes are like this: ".

Second, the syntax you are attempting to use is BBEdit’s, not omnioutliner’s. Open up the OO applescript dictionary and look for ‘replace’ and you can see the prescribed syntax. A simple example of multiple replacements would look like this:

tell application "OmniOutliner"
	tell document 1
		replace topic of rows replacement "neighbourhood" string "neighborhood"
		replace topic of rows replacement "colour" string "color"
end tell
end tell

The above would go through each row in document 1, and then replace each string with the corresponding replacement in each row’s topic.

A couple of elaborations:

You can filter the rows to search. This example would restrict the search to top level rows (i.e. children of document 1) and only affect topics containing “Ep.0”.

replace (topic of children of document 1 whose name contains "Ep.0") string "Ep." replacement "Quest."

You can use regular expressions. This example would add the ‘u’ to each instance of ‘neighbor’ but ignore ‘neighborhood’.

replace topic of rows replacement "neighbour" matching regular expression "\\bneighbor\\b"

Finally, you can loop through a list of pairs if you have a lot of replacements to make. I use something like this to capitalize every instance of a team name in the focused section. This isn’t especially fast if you have a large list and document but the filtering makes it tolerably so.

tell application "OmniOutliner"
	tell document 1
		set teamList to {{"Bills", "bills"}, {"Patriots", "patriots"}, {"Dolphins", "dolphins"}, {"Jets", "jets"}}
		set foci to sections whose visible is true and expanded is true
		repeat with focSect in foci
			repeat with team in teamList
				set uName to item 1 of team
				set lName to item 2 of team
				
				replace topic of rows of focSect replacement uName matching regular expression "\\b" & lName & "\\b"
			end repeat
		end repeat
	end tell
end tell

I hope this helps.

1 Like