[Share] Copy the first attachment of an item to clipboard #195
Replies: 6 comments 7 replies
-
Sorry, I did not manage to get it working. |
Beta Was this translation helpful? Give feedback.
-
Is there any warning or error when run the action? |
Beta Was this translation helpful? Give feedback.
-
能否不将此快捷键设为全局? |
Beta Was this translation helpful? Give feedback.
-
Is there a way to adapt this to copy all the attachments for an item? |
Beta Was this translation helpful? Give feedback.
-
I am getting an error (under Linux): "Script error: redeclaration of formal parameter item" What am I doing wrong? |
Beta Was this translation helpful? Give feedback.
-
重构了一个代码逻辑更清晰的版本: /**
* 复制Zotero条目的第一个附件到剪贴板
* @author Zotero Community
* @usage 在Zotero中选中一个条目后运行此脚本
*/
// 检查是否有选中的条目
if (!items?.length) return;
/**
* 获取常规条目的附件路径
* @param {Zotero.Item} item Zotero条目对象
* @returns {Promise<string[]>} 附件路径数组
*/
async function getRegularItemAttachments(item) {
const paths = [];
const attachmentIDs = item.getAttachments();
for (let id of attachmentIDs) {
const file = await Zotero.Items.get(id).getFilePathAsync();
if (file) {
paths.push(file);
}
}
return paths;
}
/**
* 获取附件条目的文件路径
* @param {Zotero.Item} item Zotero附件条目
* @returns {Promise<string[]>} 文件路径数组
*/
async function getAttachmentPath(item) {
const paths = [];
const file = await item.getFilePathAsync();
if (file) {
paths.push(file);
}
return paths;
}
/**
* 复制第一个附件到剪贴板
* @param {Zotero.Item} item Zotero条目
* @returns {Promise<string>} 操作结果信息
*/
async function copyFirstAttachment(item) {
try {
if (!item || item.isNote()) {
return "Invalid item type";
}
let paths = [];
if (item.isRegularItem()) {
paths = await getRegularItemAttachments(item);
} else if (item.isAttachment()) {
paths = await getAttachmentPath(item);
}
if (paths.length > 0) {
const clipboardHelper = new Zotero.ActionsTags.api.utils.ClipboardHelper();
clipboardHelper.addFile(paths[0]).copy();
return `${paths[0]} copied`;
}
return "No attachment found";
} catch (error) {
return `Error: ${error.message}`;
}
}
// 执行主函数
copyFirstAttachment(items[0]); |
Beta Was this translation helpful? Give feedback.
-
Description
Copy the first attachment of an item to clipboard.
复制条目的第一个附件到剪贴板,以方便分享。
Event
None
Operation
Script
Data
Anything else
Update the to the latest verion.
更新Zotero Actions & Tags到最新版。
复制条目的所有附件可以使用Quicker动作:https://getquicker.net/Sharedaction?code=093440ee-c70a-4a66-b97a-08dbe7797a52
Beta Was this translation helpful? Give feedback.
All reactions