AppleScript to add/remove specific tags in OmniFocus with keyboard shortcuts

In OmniFocus, there are a handful of tags that I use regularly, and I’d love to use Keyboard Maestro to assign separate keyboard shortcuts for toggling each of about 10 tags (adding or removing them). This function is built in for the “Forecast” tag with ⌃⌘-L (which is fantastic), but I’d like to have this for more of my regularly-used tags.

Ideally, this would also allow one to apply this to multiple selections. I’m guessing that a witch’s brew of “repeat” and “if - else” operations would do the job, but I’m too new to AppleScript to get it to work. Anyone already done this or have ideas how to do it?

See if this thread about adding tags gives you the starting point you need.

I’d already looked at that, but it’s really for a very different purpose, namely, adding a day-of-the-week tag.

Perhaps it’s best to just start with something much simpler: I simply want to add a specific tag (that is already in use with other tasks). So, I select a task and run this script:

tell application “OmniFocus”
activate
tell front document
set theTag to ( first flattened tag where its name is “campus”)
add theTag to tags of theTask
end tell
end tell

But then I get this execution error:
“The variable theTask is not defined. (-2753)”

I’ve looked at about a dozen examples here and on the OmniFocus site, but I can’t seem to find a simple explanation of how to do this. Seems like something that would be useful to a lot of people.

I don’t use OmniFocus, but from the script above you can see that you use ‘theTask’, but haven’t defined what it is in OmniFocus.

Looking at this, it would suggest that you need to specify it with maybe something like this:

tell application "OmniFocus"
    tell front window
        set my_sel to selected trees of content
        set theTask to value of item 1 of my_sel
    end tell
end tell

Now the article I referenced above is quite old, and I’m sure OF3 potentially has lots of new ways of doing things; maybe including this. Because I’m not an OF user I can’t test it, but I strongly suspect that you need to have something like this to do the specification of what the variable ‘theTask’ represents.

Joel,
I have the same problem! Seeing your post inspired me to write this script which I can call from a keyboard shortcut using FastScripts or add as a script in the OmniFocus tool bar.

You can get it from my github repo.
osx_scripts_folder/Toggle Tag.scpt at master · bgebhardt/osx_scripts_folder

Hope this helps.

I would also love if the OmniFocus team could bind tags to certain key combinations. I would use that quite heavily. But for now I have this script. Automation and AppleScript for the win!

Hey Bryan,

Thanks for this. Terrific work. The option of picking tags to toggle is great, but what I’m looking for is speed, so I’d prefer to make individual scripts for individual tags, without the intermediary step. It shouldn’t be too hard for me to just eliminate the selection steps, but if you have a stripped down version as well (say, for just toggling “Today”), I’d be much obliged!

Joel

It’s not obvious, but if you put in the script just a list of one item it won’t bring up the list picker. You can just duplicate the script with different single tag lists and assign to different hot keys to do it for single tags.

1 Like