Remove fixed tag if present

Trying to create a script to check to see if TagA is present, if so remove.

Any help would be appreciated.

I know nothing about scripting and have tried to modify a few different replace tag actions in the directory but keep running into errors and am not sure what the problem is.

Below is what I’ve come up with which doesn’t work…

// remove taga

if (con) {
var items = Draft.query("", “all”, “taga”)
for (var item of items) {
item.removeTag(“taga”);
item.update();
}
}
else {
context.cancel
}

1 Like

Would this alternate, inbuilt approach work for you?

  1. Open the list of drafts (inbox, flagged, archive, trash).
  2. Press and hold select (at the bottom) - this will select all drafts in the list (as determined by what view/workspace you are viewing).
  3. Tap operations (at the bottom).
  4. Tap remove tag from the list of available operations.
  5. Select the tag to remove from the list of available tags.
2 Likes

Just in case you want this for something more, you can always try this:

let tagDefault = "taga";
var promptTag = Prompt.create();

promptTag.title = "Remove Tag";
promptTag.addTextField("tagRemove", "Remove Tag", tagDefault);
promptTag.addButton("OK");

if (promptTag.show())
{
	if (promptTag.buttonPressed == "OK")
	{
		let draftsResults = Draft.query("", "all", [promptTag.fieldValues["tagRemove"]]);
		for (let draftCurrent of draftsResults)
		{
			draftCurrent.removeTag(promptTag.fieldValues["tagRemove"]);
			draftCurrent.update();
		}
	}
}
2 Likes

Thanks for the reply.

That has been my temporary solution but the goal to script it is to stack it with another action.

Use case:
Currently I have a workflow which pulls information into a new draft, and adds TagA.

At a later point, the draft gets processed with an action, which adds TagB.

I’d like to be able to automate the process of removing TagA, while it gets processed.

I figured I could stack the first action with a second action, which says if the draft has TagA, remove it.

Hopefully this makes sense. Thanks again for reaching out.

1 Like

Did you try the script I posted above?

1 Like

Yes, thank you!

I know I’m being picky but the goal was to process without a prompt and just automate “if draft has TagA, then remove, if not, do nothing”.

Your script does work though and gets me much closer than I could myself so thank you. It’s much appreciated.

1 Like

When I post examples, where possible, I try and post something that will get not just the poster to the answer but also offer wider options for other readers too who may be looking for something similar but not quite the same. That’s all.

But doing things like pulling out the tag as a separate variable near the top of the script can be useful for maintenance and potential re-use if you say turned the tag removal into a function call instead.

Anyway. Glad it was enough to resolve your issue. :smiley:

3 Likes

This can be done with just a few lines of code:

// Contains Tag

if (draft.hasTag("tag") == true) {
draft.removeTag("tag");
draft.update();
}

Hope that helps.

1 Like

Thanks Craig!

I actually came here after you advised me on micro.blog with some issues I was running into with my Bear workflow.

Figured I’d give Drafts a go with it’s workspaces and this gets me exactly where I wanted to be.

1 Like

Oh hey, look at that! I keep usernames smaller than actual names in Icro, so I didn’t make the connection. Fun running into you here as well, and funny that I did end up finding a way around your problem in the end.

1 Like

love that example
Thanks a lot