diff --git a/README.md b/README.md index 3604a1d..70f7762 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ 2. 将所选条目语言字段设置为`en`。 +3. 将附件导出。 + # Usage 1. Remove linked attachment(s) when delete the item(s) or collection. @@ -15,6 +17,8 @@ 2. Set the language field of selected items as `en`. +3. Export attachment(s). + # License The source code is released under GNU General Public License, version 3.0 diff --git a/chrome/content/overlay.xul b/chrome/content/overlay.xul index 53d9470..dec672e 100644 --- a/chrome/content/overlay.xul +++ b/chrome/content/overlay.xul @@ -17,6 +17,10 @@ id="zotero-collectionmenu-delitem" label="&zotero.delitem.delitems.label;" oncommand="Zotero.DelItem.DelColl()"/> + @@ -31,18 +35,15 @@ + + + + - - - - - - - - - - diff --git a/chrome/content/scripts/delitem.js b/chrome/content/scripts/delitem.js index 49bc85b..9c82c93 100644 --- a/chrome/content/scripts/delitem.js +++ b/chrome/content/scripts/delitem.js @@ -1,5 +1,5 @@ Zotero.DelItem = { - + DelItem: async function () {//右击时删除条目调用的函数 var zoteroPane = Zotero.getActiveZoteroPane(); @@ -182,6 +182,112 @@ Zotero.DelItem = { } }, + // 导出附件 + // 右击导出分类调用的函数 + ExpColl: async function () { + var collection = ZoteroPane.getSelectedCollection(); + var items = collection.getChildItems(); + Zotero.DelItem.ExpAtt(items); + }, + + // 右击分类导出附件 + ExpAtts: async function () { + var zoteroPane = Zotero.getActiveZoteroPane(); + var items = zoteroPane.getSelectedItems(); + Zotero.DelItem.ExpAtt(items); + }, + // 导出附件实际调用的函数 + ExpAtt: async function (items) { + var expDir = await this.chooseDirectory(); //得到导出的目录 + var nItems = 0; //导出个数计数器 + var lanUI = Zotero.Prefs.get('intl.locale.requested', true); // 得到当前Zotero界面语言 + var whiteSpace = ' '; + if (lanUI == 'zh-CN') {whiteSpace = ''}; + for (let item of items) { + if (item && !item.isNote()) { //2 if + if (item.isRegularItem()) { // Regular Item 一般条目//3 if + let attachmentIDs = item.getAttachments(); + for (let id of attachmentIDs) { //4 for + let attachment = Zotero.Items.get(id); + var file = await attachment.getFilePathAsync(); + if (file) { // 如果文件存在(文件可能已经被删除) + try { + // await exportAtts(file, expDir); + var baseName = OS.Path.basename(file); //得到文件名 + var destName = OS.Path.join(expDir, baseName); + OS.File.copy(file, destName); // 尝试导出文件 + nItems = nItems + 1; + } catch (error) { // 弹出错误 + alert (Zotero.DelItem.diwaGetString("file.export.error")); + return; // 弹出错误后终止执行 + } + + } + + } //4 for + } // 3 if + if (item.isAttachment()) { //附件条目 5 if + var file = await item.getFilePathAsync(); + if (file) { // 如果文件存在(文件可能已经被删除) + try { + // await exportAtts(file, expDir); + var baseName = OS.Path.basename(file); //得到文件名 + var destName = OS.Path.join(expDir, baseName); + OS.File.copy(file, destName); // 尝试导出文件 + nItems = nItems + 1; + } catch (error) { // 弹出错误 + alert (Zotero.DelItem.diwaGetString("file.export.error")); + return; // 弹出错误后终止执行 + } + } + + }//5if + } //2 if + + } + alert (nItems + whiteSpace + Zotero.DelItem.diwaGetString("file.exported") + whiteSpace + expDir + Zotero.DelItem.diwaGetString("full.stop")); + }, + + // 导出附件函数 + exportAtts: async function (file, expDir) { + var baseName = OS.Path.basename(file); //得到文件名 + var destName = OS.Path.join(expDir, baseName); + OS.File.copy(file, destName); // 尝试导出文件 + }, + + //对话框 Form ZotFile + /** + * Choose directory from file picker + * @return {string} Path to file + */ + chooseDirectory: async function () { + if (Zotero.platformMajorVersion >= 60) { + var FilePicker = require('zotero/filePicker').default; + } + else { + var nsIFilePicker = Components.interfaces.nsIFilePicker; + } + var wm = Services.wm; + var win = wm.getMostRecentWindow('navigator:browser'); + var ps = Services.prompt; + if (Zotero.platformMajorVersion >= 60) { + var fp = new FilePicker(); + fp.init(win, Zotero.DelItem.diwaGetString("file.exp.path"), fp.modeGetFolder); + fp.appendFilters(fp.filterAll); + if (await fp.show() != fp.returnOK) return ''; + return fp.file; + } + else { + var fp = Components.classes["@mozilla.org/filepicker;1"] + .createInstance(nsIFilePicker); + fp.init(win, Zotero.DelItem.diwaGetString("file.exp.path"), nsIFilePicker.modeGetFolder); + fp.appendFilters(nsIFilePicker.filterAll); + if (fp.show() != nsIFilePicker.returnOK) return ''; + var file = fp.file; + return file.path; + } + }, + // 将所有所选条目语言字段设为en chanLanForSel: async function () { var zoteroPane = Zotero.getActiveZoteroPane(); @@ -298,39 +404,62 @@ Zotero.DelItem = { return true;} }, + // 检查是否为链接 + // true为链接或没有附件 + checkItemExp: function (item){ + var num = 0; + if (item && !item.isNote()) { + if (item.isRegularItem()) { // not an attachment already + let attachmentIDs = item.getAttachments(); + //return attachmentIDs.length; // 返回数据中元素个数 + for (let id of attachmentIDs) { + let attachment = Zotero.Items.get(id); + num = num + attachment.attachmentLinkMode; // attachmentLinkMode 链接类型 3为链接 2为文件 + + } + var linkURL = attachmentIDs.length == 1 && num == 3 + if (linkURL || attachmentIDs.length == 0) {return true;} + } + + if (item.isAttachment()) { + //var attType = item.attachmentContentType; + if (item.attachmentLinkMode == 3) {return true;} + } + } + + }, // 是否显示菜单函数 displayMenuitem: function () { // 如果无附件则不显示菜单 var pane = Services.wm.getMostRecentWindow("navigator:browser") .ZoteroPane; var collection = ZoteroPane.getSelectedCollection(); var items = pane.getSelectedItems(); + if (collection) {var items_coll = collection.getChildItems();} //Zotero.debug("**Jasminum selected item length: " + items.length); var showMenuAtt = items.some((item) => Zotero.DelItem.checkItemAtt(item)); // 检查附件 var showMenuSnap = items.some((item) => Zotero.DelItem.checkItemSnap(item)); // 检查快照 - var showMenuNote = items.some((item) => Zotero.DelItem.checkItemNote(item)); // 检查快照 + var showMenuNote = items.some((item) => Zotero.DelItem.checkItemNote(item)); // 检查笔记 var showMenuColl = (collection == false); // 非正常文件夹,如我的出版物、重复条目、未分类条目、回收站,为false,此时返回值为true,隐藏菜单 - //pane.document.getElementById("id-delitem-separator").hidden = !( // 分隔条是否出现 - // showMenuAtt || - // showMenuSnap); - - /*pane.document.getElementById( //总菜单 - // "zotero-itemmenu-delitem-namehandler" - // ).disabled = !( // 总菜单是否可用 - // showMenuAtt || - // showMenuSnap);*/ - /*pane.document.getElementById( //删除条目和附件 - "zotero-itemmenu-delitem" - ).disabled = !( // 删除条目和附件是否可用 - showMenuAtt || - showMenuSnap);*/ + if (collection) { // 如果是正常分类才显示 + var showMenuCollExp = items_coll.some((item) => Zotero.DelItem.checkItemAtt(item));} else { + var showMenuCollExp = false;} // 检查分类是否有附件及是否为正常分类 + var showMenuExpAtt = items.every((item) => Zotero.DelItem.checkItemExp(item)); //检查是否为链接 true为链接或没有附件 + // pane.document.getElementById( // 其他分类菜单是否可见 总的id:zotero-collectionmenu + // "zotero-collectionmenu" + // ).hidden = showMenuColl; // - - pane.document.getElementById( // 删除分类/文件夹菜单是否可见 + pane.document.getElementById( // 删除分类/文件夹菜单是否可见 "zotero-collectionmenu-delitem" - ).hidden = showMenuColl; // 仅删除附件菜单是否可用 - pane.document.getElementById( // 删除分类/文件夹分隔条是否可见 + ).hidden = showMenuColl; // 仅删除附件菜单是否可用 zotero-collectionmenu-delitem + + pane.document.getElementById( // 删除分类/文件夹分隔条是否可见 id-delcoll-separator "id-delcoll-separator" - ).hidden = showMenuColl; // 仅删除附件菜单是否可用 + ).hidden = showMenuColl; // + + pane.document.getElementById( // 导出分类附件是否可见 zotero-collectionmenu-exp-att + "zotero-collectionmenu-exp-att" + ).hidden = showMenuColl; // + pane.document.getElementById( // 仅删除附件菜单 "zotero-itemmenu-delatt" ).disabled = !showMenuAtt; // 仅删除附件菜单是否可用 @@ -342,6 +471,15 @@ Zotero.DelItem = { pane.document.getElementById( // 仅删除笔记菜单 "zotero-itemmenu-delnote" ).disabled = !showMenuNote;// 仅删除笔记是否可用 + + pane.document.getElementById( // 分类导出附件 + "zotero-collectionmenu-exp-att" + ).disabled = !showMenuCollExp ; // 分类导出附件菜单是否可用 + + pane.document.getElementById( // 条目导出附件 + "zotero-itemmenu-exp-att" + ).disabled = !showMenuAtt || showMenuExpAtt; // 条目导出附件菜单是否可用 + }, }; diff --git a/chrome/locale/en-US/diwa.properties b/chrome/locale/en-US/diwa.properties index d2a8e44..1f333d8 100644 --- a/chrome/locale/en-US/diwa.properties +++ b/chrome/locale/en-US/diwa.properties @@ -6,3 +6,7 @@ delete.attachment.only = Are you sure you want to delete the attach delete.snapshot = Are you sure you want to delete the snapshot(s) of the item(s)? delete.note = Are you sure you want to delete the note(s) of the item(s)? file.is.open = The file can not be deleted. Please close the file if you have opened it and try again. +file.export.error = The file exporting error. Please close the file if you have opened it and try again. +file.exported = attachment (s) was (were) exported to +file.exp.path = Please choose the directory to store the files +full.stop = . \ No newline at end of file diff --git a/chrome/locale/en-US/overlay.dtd b/chrome/locale/en-US/overlay.dtd index 182c0e0..cb41b2f 100644 --- a/chrome/locale/en-US/overlay.dtd +++ b/chrome/locale/en-US/overlay.dtd @@ -5,6 +5,10 @@ + + + + diff --git a/chrome/locale/zh-CN/diwa.properties b/chrome/locale/zh-CN/diwa.properties index 92a240c..d53ba9c 100644 --- a/chrome/locale/zh-CN/diwa.properties +++ b/chrome/locale/zh-CN/diwa.properties @@ -6,3 +6,7 @@ delete.attachment.only = 您确定将所选条目的附件移动到 delete.snapshot = 您确定将所选条目的快照移动到回收站? delete.note = 您确定将所选条目的笔记删除? file.is.open = 无法删除文件。如果文件已经打开,请关闭后重试。 +file.export.error = 附件导出出错。如果文件已经打开,请关闭后重试。 +file.exported = 个附件导出到 +file.exp.path = 请选择导出附件存放的目录 +full.stop = 。 diff --git a/chrome/locale/zh-CN/overlay.dtd b/chrome/locale/zh-CN/overlay.dtd index d171d4f..8bc877e 100644 --- a/chrome/locale/zh-CN/overlay.dtd +++ b/chrome/locale/zh-CN/overlay.dtd @@ -5,6 +5,10 @@ + + + + diff --git a/install.rdf b/install.rdf index 6eb2b20..290f72b 100644 --- a/install.rdf +++ b/install.rdf @@ -7,7 +7,7 @@ RDF:about="urn:mozilla:install-manifest" em:id="delitemwithatt@redleaf.me" em:name="Del Item With Att" - em:version="0.0.12" + em:version="0.0.13" em:type="2" em:creator="Minyi Han" em:description="Delete Item(s) With Attachment(s)" @@ -48,7 +48,9 @@ zh-CN delitem + Minyi Han 删除条目的同时删除附件 + https://github.com/redleafnew/delitemwithatt diff --git a/update.rdf b/update.rdf index f948f56..27d6666 100644 --- a/update.rdf +++ b/update.rdf @@ -6,13 +6,13 @@ - 0.0.12 + 0.0.13 zotero@chnm.gmu.edu 5.0.0 5.* - https://github.com/redleafnew/delitemwithatt/releases/download/0.0.12/delitemwithatt.xpi + https://github.com/redleafnew/delitemwithatt/releases/download/0.0.13/delitemwithatt.xpi @@ -21,7 +21,7 @@ juris-m@juris-m.github.io 4.999 5.* - https://github.com/redleafnew/delitemwithatt/releases/download/0.0.12/delitemwithatt.xpi + https://github.com/redleafnew/delitemwithatt/releases/download/0.0.13/delitemwithatt.xpi