Run a shortcut (or app itself) when OmniFocus task is clicked done

I tried searching the forums and maybe I didn’t type the correct search but here is what I want: I have a sequential list of things I do in the morning and the evening (This is very helpful to ADHD folks BTW for those wondering). Some of those tasks include (in the notes field) the shortcut url to run a given app, i.e. my Blood Pressure app in the morning. Right now I have to tap on the task, tape on Notes, then tap on the url to get the shortcut to run. I could also find the app url scheme I suppose but I don’t think it would be any better as there still would be the same number of taps. What I want is to be able to tap a task done, causing it to launch the blood pressure app. That may seem a bit backwards as I really should tap it as done once I’ve actually taken my blood pressure. So I’m open to other ways if that workflow doesn’t make sense as I want it. Either way, it would require OmniFocus to run an app or shortcut, then return to the list in OmniFocus. I know there is OmniAutomation using JavaScript which I can figure out. I am just not sure what part of the API to use or if it’s even possible. Edited to add that I do, in fact, have a shortcut that creates Med alarms for me to take them throughout the day, so it would need to run both shortcuts and apps. Thanks in advance!

I’m afraid the answer is to start from somewhere else: Make ticking off the task part of the larger workflow you want. Rather than expecting an event-driven firing of the rest of your automation.

One option could be Omni Automation. In fact that’s probably closer to what you want than anything as it starts off in OmniFocus - with the task selected,

1 Like

Thanks @Martin_Packer! I had a feeling but wanted to make sure before I go down that Rabbit Hole. I’m a developer so I can figure it out eventually. I just thought maybe someone else may have already written the JavaScript or at least knew which part of the API to start at. I’ll take a look and I’ll report back if/what I find out.

So I figured out the JavaScript to automate this but there is no Event Loop I can tap into so it’s no better than tapping the actual url in the Note field. I wish Omni would give me an Event Loop where I can wait for a task to be checked and then fire off my plugin or whatever automatically. I do understand that there are security concerns about this, though, so I do get why they haven’t. There probably is a secure way to do this though, but I’m not sure what mechanism they could use, at least not in JavaScript. Anyway, thanks for the idea. At least I understand Omni Automation a bit.

I don’t think it’s an event loop. It’s an Omni Automation action against the selected task(s).

I might get time to do a sample today - though marking a task completed is not something I’ve done in OmniJS before.

OK. How about this as a basis @rgottlieb ?

// COPY & PASTE into editor app. EDIT & SAVE with “.omnifocusjs” file extension.
/*{
	"type": "action",
	"targets": ["omnifocus"],
	"author": "Martin Packer",
	"identifier": "com.mglp.enhancedcompletion",
	"version": "1.0",
	"description": "Augments task completion",
	"label": "OmniFocus Enhanced Completion",
	"shortLabel": "Enhanced Completion",
	"paletteLabel": "Enhanced Completion",
	"image": "gearshape"
}*/
(() => {
	var action = new PlugIn.Action(function(selection, sender){
		// action code
		// selection options: tasks, projects, folders, tags
		selection.tasks.forEach(function(task){
			// Mark the selected task complete
			task.markComplete()
			
			/***********************************************/
			/* Alter the following to do the other actions */
			/* on completion.                              */
			/***********************************************/

			// Copy the task title to the clipboard
			Pasteboard.general.string = task.name
			
			// Log the task's title
			console.log(task.name)
		})
	});

	action.validate = function(selection, sender){
		// validation code
		// selection options: tasks, projects, folders, tags
		return (selection.tasks.length > 0)
	};
	
	return action;
})();

You can see where to add extra function.

In my case I want the task’s name on the clipboard - as I will paste it into Drafts for my daily journal - as a completed task. It’s just the task name, not a reference - but that’s all I want for my journal and I can edit from there.

(I tried to think of a practical use for an “augmented complete”. The above is the best I’ve come up with. I’d be interested in others’ use cases.)

I’ve also added a console.log() for good measure. I’ve taken it out of my “Production” version.

2 Likes

Yes, this does look like it should work. I do get now the paradigm of working on the selection, and that should be easy enough to write with what you have above, thanks! Edited to add the code I use to run the shortcut I run after grabbing it from the note. I know it’s faulty in terms of not checking for other text, etc, but I specifically only put one shortcut in each of my tasks on purpose:

	note.startsWith("shortcuts") // alternate x-url schemes could be used here too I suppose
	task.markComplete()
	url = URL.fromString(note)
	url.open()
1 Like

Actually that’s useful to me:

Knowing just to use the shortcuts: URL scheme with open() creates possibilities for me.

(BTW I didn’t spot a colon in your filter; That might be one small tightening you need.)

But thanks for coming back with your tweak!

1 Like

Good, it’s nice to be able to contribute once in a while! :slight_smile:
The colon doesn’t seem to matter. I was thinking more along the lines of airmail: mail: etc, so I could search for a colon and anything before it woud tell me if it’s a shortcut or an x-url. If it’s an x-url I could just run it like I have above as well, i.e. I don’t have to keep mine to shortcuts if all they do is run the application provided it has an x-url I can hit to run it. And the bonus of the x-urls is that you can give them parameters (especially email related) so subject, body, etc. Drafts probably has exactly that, though I haven’t played with it for sure.

So if by missing the colon you mean I need to see if one exists, yes that would be an improvement regardless of how I run this, but it can also tell me that there’s something more there and that I might want to give it more information. So in JavaScript I could use an if branch to decide if it’s a simple shortcut or something more. If something more, then I’ll need to figure out how to deal with parameters if there are any. I’d almost need a dictionary of possible x-urls and their corresponding parameters and either fill them in or not. Definitely a lot can be done here!

1 Like

Thanks. The point about the colon is that it’s quite likely you might use the word “shortcuts” without it being a URL scheme signifier.

But then again consider the sentence “This is how I use shortcuts:” . :slight_smile:

Unlikely “shortcuts://” is something other than the beginning of a URL.

You’re welcome! And yeah good point! I’ll make that change when I get a chance, thanks!

1 Like

To solve these exact same problems I use Due instead of OmniFocus. When a URL is included in the reminder name, Due will offer to launch that URL as an option when the reminder is completed. Launching is even possible when completing the reminder directly from the notification on the home screen, the app doesn’t need to be open. I find it incredibly useful. One of my reminders is titled as follows:

Blood pressure


shortcuts://run-shortcut?name=Log%20Bloodpressure

The 2 blank lines help only to tidy up the name, keeping the URL out of the list of upcoming reminders.

I’ve lodged a feature request with OmniFocus for the same sort of “URL launch on item complete” functionality.

1 Like