Trying to use python to add and remove tags from tasks, tags get deleted instead

After pip install pyobjc, and creating a task called “test task” and a tag called “Test Tag”, I tried this:

from ScriptingBridge import SBApplication
from Foundation import NSURL
omniFocus = SBApplication.applicationWithURL_(
    NSURL.URLWithString_("file:///Applications/OmniFocus.app")
)
doc = omniFocus.documents()[0]
doc.inboxTasks().objectWithName_("test task").tags().removeObject_(doc.tags().objectWithName_("Test Tag"))

and nothing happened. If instead, I do this:

doc.inboxTasks().objectWithName_("test task").tags().removeObjectWithID_(doc.ta
gs().objectWithName_("Test Tag").id())

“Test Tag” is deleted from OmniFocus entirely, which was certainly a surprise.

In general manipulating task tags seems flaky and weird in ScriptingBridge, despite the fact that it seems to vaguely work in AppleScript. Is this just a bug or is there some subtlety I’m not seeing here?

Are you running in circles with one line of code? You find the object with the tag and remove the tag from the object all in the same line. What happens when you find the objects with the tag, set that result to an object/variable, and then remove the tags from the objects? IOW, use two lines of code.


JJW

I’ve tried dozens of variations of this, with temporary variables for various collections of things, different lookups by name, by ID, by index, etc; the result always appears to be the same.

OK. I just thought to ask what might have otherwise been obvious to others but not me. Thanks.


JJW

1 Like

In case anyone else was still wondering: this is a bug in ScriptingBridge, and not (as I now understand it, anyway) in OmniFocus or its scripting support.

http://appscript.sourceforge.net is now maintained again (the documentation still says it isn’t, but https://pypi.org/project/appscript/#history has a release last year and another one very recently this year.

So, I tried this out using appscript, whose model much more directly maps to the expected AppleScript verbs, and this example, in addition to being much more brief and clear, also works and doesn’t accidentally destroy tags in the whole DB:

from appscript import app

omni_focus = app("OmniFocus")
doc = omni_focus.default_document
test_tag = doc.tags['Test Tag']

omni_focus.remove(test_tag, from_=doc.inbox_tasks['test task'].tags)
1 Like