Script to read text file and add to OO doc

I am trying to put together an AppleScript that will read a plain text file and, depending on its contents, find an existing row in a document and add a child, plus optional note (from the text file), or create a new row and add child to it.

This is really a workaround for OmniOutliner for iOs, since it doesn’t support x-callback-urls. I create a quick text file with Drafts and save it to DropBox, then, using Hazel on my office desktop, an AppleScript is automatically run on the new text file to add its contents to an existing OO document.

I understand OO will have x-callback features sometime in the future, but I just can’t depend upon, or wait for, OG ‘future additions’ which really should have been integrated in the app given its base price (never Pro upgrade price structure). Anyway, I digress, what I really need some guidance on accomplishing the above :)

Thanks for taking the time to read my post.

1 Like

I’ve been itching for something like this. I’m using every app you listed, but I’m not familiar with AppleScript. I hope you succeed in developing this script! I love Drafts and Hazel. :)

Here’s a bare bones script using OS X’s built-in Folder Action.

In Script Editor preferences be sure ‘Show Script menu in menu bar’ is checked.

Copy-paste script into Script Editor (in Applications > Utilities), save with a meaningful name, and in Finder hold down ‘option’, choose Go from the menu, and navigate to Library > Scripts > Folder Action Scripts. Move the script into that folder.

Then choose Folder Actions > Configure from the scripts menu (the “curly scroll” icon in the menu bar), add the Dropbox (or other) folder you want to watch on the left and the script you’ve just saved on the right, and check the Enable Folder Actions.

This can be embellished with error-checking and pre-processing to check fileName and/or fileText and place the new row in different locations in OmniOutliner. Here it expects a From Drafts row in the front document.

SG

--Folder Action that inserts name and contents of a file into OmniOutliner

on adding folder items to thisFolder after receiving newItem
	set text item delimiters to ":"
	set fileName to (newItem as text)'s text items's item -1
	set fileText to (read (newItem) as «class utf8»)
	
	tell application "OmniOutliner"
		tell document 1
			set tgtRow to row named "From Drafts"
			set newChild to make new row at tgtRow
			tell newChild
				set topic to fileName
				set note to fileText
			end tell
			set tgtRow's expanded to true
		end tell
	end tell
	
	display notification "New row added to OO" with title "From Drafts"
	
end adding folder items to
1 Like

SGIII, thanks for posting this action, it has given me a good starting point for what I’m trying to achieve.

I have never really used the built-in folder actions, as I find Hazel much easier to use and more powerful. Your script was just what I needed to get me started with understanding how to use OminGroup’s AppleScript extensions to add new content. Right now I’m trying to figure out how to open a specific file, as your example assumes that OO will open with the desired document, but, unless you’re only working on the one document, this is rarely the case.

After a little digging, I found that one actually has to use a variable that references your OO file in POSIX format, as in:

set myDoc to a reference to file ":Users:myself:Documents:Outlines:desiredfile.oo3"

Once I have managed to get this working I will have Hazel run the AppleScript on new files (I would like to see if I can have different lines in the file set styles/attributes of the new entry ), then archive them automatically.

Anyway, when have some spare time I will see what is possible. Thanks for sending me off in the right direction :)