Automating Actions Through Flagged Text

I’m looking to see if it’s possible to programmatically create a task in Omnifocus. I was thinking it would be nice to be able to prepend some sort of flag to the front of a line of text and an Omnifocus task would be created. I wasn’t sure if there was a potential to script such action based on some flag within the text.

For example:
@task: remember to pick up milk

Hopefully that makes sense.

TIA!

While we have nothing specifically set up for that task, yes the potential exists using the On Rename event trigger in a smart rule.

Here’s an example…

Also, here’s a quick smart rule version of the Script menu > Reminders > Add as To Do to Omnifocus script…

Add as To Do to OmniFocus_SR.scptd.zip (18.4 KB)

  1. In the Finder, select the ​Go ​menu while holding the ​Option key​, then choose ​Library​.
  2. Unzip the attached file and drop the script file into Application Scripts/com.devon-technologies.think3/Smart Rules.
  3. Quit and relaunch DEVONthink.

It can now be used with an Execute Script > External action in the smart rule.

Note: This is not triggering on a specific character, but on any renaming. If you are so inclined, you could edit the script to check for the presence of specific character.


Here’s a variation using smart rule criteria…

image

However, note the files won’t match until they are renamed with the @.
Also, this will be the name displayed in OmnifFocus, again unless you choose to edit the script. But this should give you a start on things.
:slight_smile:

Appreciate the assist! This is heading in the right direction, for sure.

I am, however, having some issues with the script.
08%20PM

Also, with this config would it understand if there are multiple “@task:” lines? The scenario here is as I’m taking notes during a meeting I would like to easily flag a task to follow-up on within Omnifocus.

43%20PM

Thanks again for the assist!

Make sure DEVONthink has Automation permissions in System Preferences > Security & Privacy > Privacy.
My example is not reading the content of the file. It was reading the filename.

The script could likely be modified, by yourself or as time permits.

Appears it has Automation permissions. Anything else I might be missing?

That doesn’t show an entry for OmniFocus. You should see an entry like this…

You should have had an Automation permission request from macOS. Is it possible you denied it?

I found the problem. After adding the quotes to “@task” the script is now working as expected. I don’t think it was actually finding the file using the filters.

37%20AM

The problem I’m seeing now is although the file is now showing within the Smart Rule folder it only seems to import into OmniFocus when I click “Apply Rule”. Shouldn’t this just be executing upon the rename of the file?

Thanks for all your help so far!!

You’re welcome :slight_smile:

Did you rename a matched file?

PS: When testing, I would not target Databases, at least not to start with.
It’s always good to start with a more specific location, then broaden out from there, if needed.

Makes sense, I narrowed down to the target folder.

Yes, I’ve renamed and edited the file to meet parameters.

Is there a way to iterate better on these scripts. I seem to have to keep restarting Devonthink in order for the changes to the script to take. I’m attempting to try and read the content of the file it finds and not having too much success.

Tried to make some small tweaks to the rules and still not see the auto execution :\

Hold the Option key and choose Help > Report bug. (Don’t send the email, unless you want to take this off to the side and start a support ticket.)

If you double-click the Console.log, it will open in Console.app and should report any AppleScript errors beting thrown.

Don’t see any errors for today and still having to right-click and Apply Rule to get the OmniFocus task created. The file does show up in the Smart Rules folder/filter, but the script isn’t running until I apply the rule.

Go ahead and open a support ticket with the aforementioned mechanism. Please include a screencast of you showing the smart rule criteria and actions, then attempting a rename on a file you feel should trigger the smart rule. Thanks!

Submitted a bug report, thank you!

While that’s working through, could you point me in the direction to be able to read the content of a file within the Automation Script? I’m having trouble finding how I can read the content of a file. My guess is I need to be able to read the content and create an array based on the number of “@task:” strings within the text.

Example:
@task: Get Milk
@task: Get Cookies
@task: Get Plate
@task: Set out for Santa

Each of the above tasks I would like to create an OmniFocus task for.

Additionally, I’m finding it pretty cumbersome to actually iterate on the script. Seems every time I make a change to the script I have to restart DevonThink. Is there a way I can iterate more quickly? I was thinking sample data within Script Editor, but it’s using an Object for ‘theRecord’ and I’m not overly familiar with Apple Script to see the content of the object, more of a Python guy myself :wink:

Appreciate the assist!

You’re actually talking about something beyond the scope of the script I sent you. The attached script is just making an OmniFocus entry for the selected file with a link back to it in DEVONthink.

Re: Reading the text, you could merely get the plain text of the record and scrape in a variety of ways. If you’re a shell guy, you could potentially do this with a do shell script command in AppleScript.

However, if you are changing the file and reprocessing it, you would have to do checks in OmniFocus too or you could easily end up with redundant tasks.

Re: Changes and having to restart, @cgrunenberg would have to respond on that.

Re: Testing in a separate script, you can use a core snippet like so in Script Editor (and no, you don’t need Script Debugger, though it can be fun to play around with too. I have never used it in a professional script writing capacity, since I find Apple’s editor to be sufficient. Your call)

tell application id "DNtp"
	set sel to item 1 of (selection as list)
	my performSmartRule(sel)
end tell

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			-- do stuff
		end repeat
	end tell
end performSmartRule

This allows you to modify the smart rule script section and run it on a selection as if the smart rule was triggering internally.

PS: I’m curious why you’re using RTF for the source. Plain text would be sufficient from what I’m seeing.

And for a little leg up, here is a bit of a core snippet using AppleScript (as we feel it’s friendlier and easier for most to understand)

tell application id "DNtp"
	set sel to item 1 of (selection as list)
	my performSmartRule(sel)
end tell

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set recordText to plain text of theRecord
			repeat with thisParagraph in (every paragraph of recordText)
				if thisParagraph begins with "@" then
					set scrapedTask to (characters 8 thru -1 of thisParagraph) as string
--- This is a little hardcoded for my taste, but if you are being uniform in entering the tasks, this will work.
					log scrapedTask -- This is only for debugging and shown in the Replies of Script Editor.
					my createOmniFocusTask(scrapedTask)
				end if
			end repeat
		end repeat
	end tell
end performSmartRule

on createOmniFocusTask(task)
	tell application "OmniFocus"
		--do stuff
	end tell
end createOmniFocusTask

Is that helpful?

plain text is exactly what I was looking for. I ended up finding the parameters I in the below link. I’m guessing I have overlooked some documentation somewhere.

https://www.rational-pi.be/2016/10/devonthink-pro-osx-automation-interface-dictionary/

I was able to adjust the script to accomplish what I was looking to do.

The script now:

  1. Creates an array from the note using a \n delimiter
  2. Locates lines containing @task:
  3. Removes the lines of whitespaces and bullets
  4. Creates tasks in OmniFocus for each finding

Still missing the automation of kicking off the script and hopefully the bug report can help resolve that.

Add as To Do to OmniFocus_SR_TDG.zip (24.6 KB)

Thanks again for all your help! Great to have the two tools integrated now!

PS. I use Rich Text because I like bullets. That’s the one thing that has always driven me crazy about DevonThink is the lacking ease of use of bullets. That’s pretty much how I take notes.

You should check out the dictionary for DEVONthink 3 in Script Editor. There are plenty of changes and updates since that web page was posted.

Also, don’t forget my favorite chapter in the built-in HelpAutomation. :slight_smile:


Why not use Markdown? It’s still plain text and can render bullets easily.

- Software  
    - Awesome  
        - DEVONtechnologies

renders as…

  • Software
    • Awesome
      • DEVONtechnologies

I see what you’re saying. I will give it a go! I hadn’t seen the side-by-side view around the Markdown. Looks a lot better than I remember. I think I made that decision a couple years ago based on font and bullets. Quite possible I just missed some of these features.

Thanks!

Lots of great stuff to discover! :slight_smile: