[Share] Copy the first attachment of an item to clipboard #195
redleafnew
started this conversation in
Action Scripts
Replies: 7 comments 9 replies
|
Sorry, I did not manage to get it working. |
0 replies
|
Is there any warning or error when run the action? |
4 replies
|
能否不将此快捷键设为全局? |
2 replies
|
Is there a way to adapt this to copy all the attachments for an item? |
0 replies
|
I am getting an error (under Linux): "Script error: redeclaration of formal parameter item" What am I doing wrong? |
0 replies
|
重构了一个代码逻辑更清晰的版本: /**
* 复制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]); |
3 replies
|
如果需要批量复制,怎么办?改了半天,没改成功/(ㄒoㄒ)/~~ |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
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
All reactions