[Share] Auto-tag when keyword detected in title #119
Replies: 9 comments 31 replies
-
Thanks, this feature is very useful. How can I use multiple keywords and tags in only one script? More specifically, if the title contains keywords A or B, add tags named C and D. |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
以下代码可以同时检测标题或摘要中是否含有某个字段,并返回相应标签: // Auto-tag based on specific keywords in the title and abstract
// @author windingwind
// @link https://github.com/windingwind/zotero-actions-tags/discussions/119
// @usage Replace the `KEYWORDS_AND_TAGS` array with your own keywords and corresponding tags.
// @see https://github.com/windingwind/zotero-actions-tags/issues/77
// @see https://github.com/windingwind/zotero-actions-tags/issues/85
if (!item) {
return "[Conditional Tag] item is empty";
}
const KEYWORDS_AND_TAGS = [
{ keyword: "FIELD_A", tag: "TAG_B" },
{ keyword: "FIELD_C", tag: "TAG_D" },
// You can add more keyword-tag pairs as needed
];
const title = item.getField("title");
const abstract = item.getField("abstract");
let tagged = false;
for (const pair of KEYWORDS_AND_TAGS) {
const { keyword, tag } = pair;
if (title.includes(keyword) || (abstract && abstract.includes(keyword))) {
item.addTag(tag, 1);
tagged = true;
}
}
if (tagged) {
return `[Conditional Tag] item "${title}" has been tagged based on keywords in title or abstract.`;
} else {
return `[Conditional Tag] item "${title}" does not match any keywords in the list in title or abstract.`;
} |
Beta Was this translation helpful? Give feedback.
-
感谢大佬回答,不过运行后没有成功添加标签 if (!item) { const KEYWORDS_AND_TAGS = [ const title = item.getField("title"); for (const pair of KEYWORDS_AND_TAGS) { if (tagged) { |
Beta Was this translation helpful? Give feedback.
-
oh I see so this is also only for Z7 |
Beta Was this translation helpful? Give feedback.
-
Hello, |
Beta Was this translation helpful? Give feedback.
-
我感觉是需要脚本维护一个 tags 库,我在想能不能这样:
|
Beta Was this translation helpful? Give feedback.
-
使用这个脚本添加了一堆tags,请问作者有没有批量删除它们的脚本。。。(好像zutilo有此功能) |
Beta Was this translation helpful? Give feedback.
-
Inspired by the code from windingwind and BoBo-YunYun, I wrote the following script: Environment
Features
Goal
// Auto-tag when keyword list detected in title and abstract
// @author windingwind, BoBo-YunYun, thy960112
// @link https://github.com/windingwind/zotero-actions-tags/discussions/119
// @usage Replace the `KEYWORD_GROUPS`
// Configuration: Keywords and corresponding tags
// Add more keyword-tag groups as needed
const KEYWORD_GROUPS = [
{
keywords: ["KWD1", "KWD2", "KWD3"],
tags: ["TAG1", "TAG2"]
},
{
keywords: ["NEW_KWD1", "NEW_KWD2"],
tags: ["#NEW_TAG1"]
}
];
if (!item) {
return "[Conditional Tag] item is empty";
}
const title = item.getField("title").toLowerCase();
const abstract = item.getField("abstractNote").toLowerCase();
let tagsAdded = [];
// Process each keyword group
for (const group of KEYWORD_GROUPS) {
// Check if any keyword from the group is found in title or abstract
if (group.keywords.some(kwd =>
title.includes(kwd.toLowerCase()) ||
(abstract && abstract.includes(kwd.toLowerCase()))
)) {
// Add all corresponding tags for this keyword group
group.tags.forEach(tag => {
item.addTag(tag, 1);
tagsAdded.push(tag);
});
}
}
// Generate return message
if (tagsAdded.length > 0) {
const uniqueTags = [...new Set(tagsAdded)]; // Remove duplicate tags in message
return `[Conditional Tag] item "${item.getField("title")}" has been tagged with: ${uniqueTags.join(", ")}`;
} else {
return `[Conditional Tag] item "${item.getField("title")}" does not match any configured keywords`;
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
Auto-tag when keyword detected in title. See #77 #85.
Action Settings
Event: Create Item
Type: Script
Data:
Beta Was this translation helpful? Give feedback.
All reactions