[Share] Replace Tag #113
windingwind
started this conversation in
Action Scripts
Replies: 3 comments
-
I would like to have a script which does the following for a collection
Could this be done? |
Beta Was this translation helpful? Give feedback.
0 replies
-
Great!!! |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks for sharing! Here I did some modification to better adopt my workflow by adding matching rules with desired tag group and item with empty tag. For anyone who may interested about it: // Replace tags with a matching pattern from a group, even for untagged items
// @author windingwind
// @enhanced by gitnapp
// @link https://github.com/windingwind/zotero-tag/discussions/113
// @usage Shortcut
if (!item) {
return "[Replace Tags] item is empty";
}
// Define the array of tags to match
const tagsToMatch = ["/done", "/pending", "/unread", "/reading"];
// The tag to add after matching and replacing
const tagToAdd = "/tocheck";
// Check if the item is untagged or if it contains any of the tags to match
if (item.getTags().length === 0 || item.getTags().some(obj => tagsToMatch.includes(obj.tag))) {
// If the item is untagged or contains a matching tag, remove the matching tags (if any)
item.getTags().forEach(tagObj => {
if (tagsToMatch.includes(tagObj.tag)) {
item.removeTag(tagObj.tag);
}
});
// Add the new tag
item.addTag(tagToAdd);
// Return a message indicating the replacement
// If the item was untagged, mention that in the message
const untaggedMessage = item.getTags().length === 0 ? " for an untagged item" : "";
return `[Replace Tags] replaced matching tags [${tagsToMatch.join(', ')}] with ${tagToAdd} on ${item.getField("title")}${untaggedMessage}`;
}
return `[Replace Tags] no matching tag found for replacement on ${item.getField("title")}`; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Description
Replace tag.
Please replace the following lines in the action script before applying it:
Show lines to edit
Action Settings
Operation: Script
Data:
Beta Was this translation helpful? Give feedback.
All reactions