[Share]用其他程序打开PDF(Open PDF with specific application) #132
Replies: 9 comments 27 replies
-
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Thank you very much! So something similar to this: https://github.com/retorquere/zotero-open-pdf |
Beta Was this translation helpful? Give feedback.
-
您好,这个动作只能“选中PDF条目-执行”才有效,能不能改进一下,使得“选中父条目-执行”也可以打开PDF? |
Beta Was this translation helpful? Give feedback.
-
楼主好 方案1: 方案2: if(item) return;
const window = require("window");
const ZoteroPane = require("ZoteroPane");
async function getPDFAttachmentPath(item) {
// 如果是PDF,直接获取路径
if (item.isAttachment() && item.attachmentContentType === 'application/pdf') {
return await item.getFilePathAsync();
}
// 如果是父条目,查找其PDF附件
else if (item.isRegularItem() && !item.isAttachment()) {
let attachments = await item.getAttachments();
for (let attachmentID of attachments) {
let attachment = await Zotero.Items.getAsync(attachmentID);
if (attachment.attachmentContentType === 'application/pdf') {
return await attachment.getFilePathAsync();
}
//break;
}
}
return null;
}
async function openPDF(item) {
let path = await getPDFAttachmentPath(item);
if (!path) {
Zotero.alert(window, "打开失败", "未找到PDF附件");
return "未找到PDF附件";
}
let exePath = Zotero.Prefs.get("extensions.actionsTags.mypdfapp2", true);
if (!exePath) {
exePath = window.prompt("输入打开PDF的exe程序路径");
Zotero.Prefs.set("extensions.actionsTags.mypdfapp2", exePath, true);
}
// Example: exe path string in windows, C:\\QuickLook\\QuickLook.exe
Zotero.launchFileWithApplication(path, exePath);
// 减少提示干扰
// return "打开成功";
}
// 你需要获取当前选中的条目
return await openPDF(items[0]);
|
Beta Was this translation helpful? Give feedback.
-
I modified the Quick Look code in #132 (comment) for MacOS: if(item) return;
const window = require("window");
const ZoteroPane = require("ZoteroPane");
async function getPDFAttachmentPath(item) {
if (item.isAttachment() && item.attachmentContentType === 'application/pdf') {
return await item.getFilePathAsync();
}
else if (item.isRegularItem() && !item.isAttachment()) {
let attachments = await item.getAttachments();
for (let attachmentID of attachments) {
let attachment = await Zotero.Items.getAsync(attachmentID);
if (attachment.attachmentContentType === 'application/pdf') {
return await attachment.getFilePathAsync();
}
}
}
return null;
}
async function openPDF(item) {
let filePath = await getPDFAttachmentPath(item);
if (!filePath) {
Zotero.alert(window, "Failed to open.", "PDF attachment not found.");
return "PDF attachment not found.";
}
let applicationPath = "/usr/bin/qlmanage"
let args = ['-p', filePath]
Zotero.Utilities.Internal.exec(applicationPath, args);
}
return await openPDF(items[0]); |
Beta Was this translation helpful? Give feedback.
-
@dschaehi For Linux and sushi quicklook, not only pdf, but also including doc, image, html, music, txt, epub. etc if(item) return;
const window = require("window");
const ZoteroPane = require("ZoteroPane");
async function getAttachmentPath(item) {
if (item.isAttachment() && !item.isNote()) {
return await item.getFilePathAsync();
}
else if (item.isRegularItem() && !item.isAttachment()) {
let attachments = await item.getAttachments();
for (let attachmentID of attachments) {
let attachment = await Zotero.Items.getAsync(attachmentID);
return await attachment.getFilePathAsync();
}
}
return null;
}
async function openFile(item) {
let filePath = await getAttachmentPath(item);
if (!filePath) {
return false;
}
let applicationPath = "/usr/bin/sushi"
let args = [filePath]
Zotero.Utilities.Internal.exec(applicationPath, args);
}
return await openFile(items[0]); |
Beta Was this translation helpful? Give feedback.
-
Type Space bar and QuickLook, Thank you to the following two people for this discussion: @windingwind @iiwenwen const ZoteroPane = require("ZoteroPane");
const Zotero_Tabs = require("Zotero_Tabs");
const document = require("document");
var lastItem = "";
async function getAttachmentPath(item) {
if (item.isAttachment() && !item.isNote()) {
return await item.getFilePathAsync();
}
else if (item.isRegularItem() && !item.isAttachment()) {
let attachments = await item.getAttachments();
for (let attachmentID of attachments) {
let attachment = await Zotero.Items.getAsync(attachmentID);
return await attachment.getFilePathAsync();
}
}
return null;
}
async function openFile(item) {
let filePath = await getAttachmentPath(item);
if (!filePath) {
return false;
}
// macbook path : -p /usr/bin/qlmanage
// linux path : /usr/bin/sushi
let applicationPath = "/usr/bin/sushi"
let args = [filePath]
Zotero.Utilities.Internal.exec(applicationPath, args);
}
async function oneKey(event) {
var key = String.fromCharCode(event.which);
let newItem = ZoteroPane.getSelectedItems()[0];
if (Zotero_Tabs.selectedID === "zotero-pane") {
if ((key == ' ' && !(event.ctrlKey || event.altKey || event.metaKey)) || (key == 'y' && event.metaKey && !(event.ctrlKey || event.altKey))) {
if(newItem) {
lastItem = newItem;
return await openFile(newItem);
}
else {
return await openFile(lastItem);
}
}
}
}
document.getElementById('zotero-items-tree').addEventListener("keydown", oneKey, false); |
Beta Was this translation helpful? Give feedback.
-
@YaoLiMuMu Can this code be run on windows, I can't get it to work when I try to run it. |
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.
-
使用指定程序打开PDF,在首次打开时会检测配置变量,如果配置名称
extensions.actionsTags.mypdfapp
不存在则跳出窗口,可以窗口中粘贴指定程序的exe路径(windows下火狐的路径是这样,C:\\Program Files\\Mozilla Firefox\\firefox.exe
,注意空格和分隔符)如果首次设置好PDF阅读器路径,那后期如果要改,建议直接到高级 -> 拉到最下方编辑器 -> 搜索配置名称
extensions.actionsTags.mypdfapp
进行手动修改You can open PDF with a specific PDF application when you right click on a PDF attachment.
Event: None
Operation: Script
Shortcut: None
Name: Open PDF
Beta Was this translation helpful? Give feedback.
All reactions