Help with Applescript - Filtering Results by Context

I’m trying to export specific tasks to a markdown file. Need to grab the ones with a specific context and I’m getting the syntax wrong. Here’s the chunk of code I’m starting with:

on OmniFocus_task_list()
set endDate to (current date) + (1 * days)
set startDate to (current date) - (1 * days)
set CurrDate to date (short date string of (startDate))
set CurrDatetxt to short date string of date (short date string of (current date))
set endDatetxt to date (short date string of (endDate))
tell application id "com.omnigroup.OmniFocus2"
	tell default document
		set refDueTaskList to a reference to (flattened tasks where (flagged is true and completed is false and (due date ≥ CurrDate and due date < endDatetxt)))
		set {lstName, lstContext, lstProject} to {name, name of its context, name of its containing project} of refDueTaskList
		set strText to "To Do List for " & CurrDatetxt & ":" & return & return
		repeat with iTask from 1 to count of lstName
			set {strName, varContext, varProject} to {item iTask of lstName, item iTask of lstContext, item iTask of lstProject}
			set strText to strText & "▢ " & strName
			if varContext is not missing value then set strText to strText & " @" & varContext
			if varProject is not missing value then set strText to strText & " (" & varProject & ")"
			set strText to strText & return
		end repeat
	end tell
end tell
end OmniFocus_task_list

I want to add something like “context is PhoneCalls” to the parenthetical phrase:

(flagged is true and completed is false and (due date ≥ CurrDate and due date < endDatetxt)))

What is the correct syntax for specifying a context? Been trying variations but nothing works so far. Thanks in advance.

Have you tried and name of context is "PhoneCalls" or something along those lines? Just saying context of will likely give you an object to work with as opposed to a string.

Thanks - that got me closer – I tried
and name of its context is "@calls"
(this is the actual name of the context I’m using) and I don’t get an error message anymore with the added “and name of its” bit. But it’s not filtering. I still get actions with other contexts. Still missing something and can’t figure out what it is.

Also tried:
and ("calls" is in name of its containing project)
but that didn’t do the trick either.

Just had a look at your “Daily Task Report” on Github. Your code looks cleaner. I think I could get most of what I need using your script, but I still need to add the context filtering. Thanks for sharing your scripts BTW! Good stuff.

1 Like

Finally got it to work, in case anyone hits this later and is looking for the answer:

set refDueTaskList to a reference to (flattened tasks where (flagged is true and completed is false and (name of its context contains "calls") and (due date ≥ CurrDate and due date < endDatetxt)))

The one change you might make is to use context id values. This will also allow you to work directly at the top level of nested contexts. Here is a working example of code that I use to toggle through my contexts.

set ContextList to every item of (complete nameofTopContext as context)
repeat with ContextValue in ContextList
    set idofIt to id of ContextValue
    .... do something with this context ...
end repeat


I like it – can you give us a little more code? My scripts are mostly things written by others that I’ve tweaked, so I’m learning by doing and have trouble creating from scratch.
For example, by “do something with this context” what would that look like – how would you call “idofIt” and send it to some kind of action? And when you say “toggle through my contexts” – does that mean the script allows you to choose in an interactive way? That would be fantastic. I was trying to get something going like that where the script would ask “What context are you working in today?” and then I could choose, and it would run the script accordingly. For example, if I was ready to make a bunch of phone calls, I would choose that context and it would run all my “@calls” tasks.

Here are some examples that I use to do stuff with or on contexts …