Skip to content

Commit

Permalink
0219_1st
Browse files Browse the repository at this point in the history
  • Loading branch information
mugpeng committed Feb 19, 2024
1 parent e7d821e commit 64d0aa3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ignore

# file
Zotlink*
*.xpi

# dir
/图片
60 changes: 60 additions & 0 deletions chrome/content/scripts/zotlink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Zotero.Zotlink = {
// reference from [jlegewie/zotfile at v5.1.1](https://github.com/jlegewie/zotfile/tree/v5.1.1)
// get zotfile settings
moveSupplemantaryFile: function() {
/* moveFile */
// get old path
var win = Services.wm.getMostRecentWindow("navigator:browser");
var sel_attachments = win.ZoteroPane.getSelectedItems()
.map(item => item.isRegularItem() ? item.getAttachments() : item)
.reduce((a, b) => a.concat(b), [])
.map(item => typeof item == 'number' ? Zotero.Items.get(item) : item)
.filter(item => item.isAttachment())
.filter(att => att.attachmentLinkMode !== Zotero.Attachments.LINK_MODE_LINKED_URL)
.map(att => att.id);
var atts = Zotero.Items.get(sel_attachments)
att = atts[0]
old_filepath = att.getFilePath()
old_filepath = OS.Path.normalize(old_filepath)
// get all files in the parent item
parent_item = Zotero.Items.get(att.parentItemID)
attachments = parent_item.getAttachments()
atts_all = Zotero.Items.get(attachments)
// get linked file's dir(conducted by zotfile)
atts_filteer = atts_all.filter(atts_all => atts_all.attachmentLinkMode === 2);
filepath = atts_filteer[0].getFilePath()
// target_folder = filePath.substring(0, filePath.lastIndexOf('\\'))
target_folder = OS.Path.dirname(filepath)
// target_folder;old_filepath
// filename = old_filepath.split('\\').pop()
filename = OS.Path.basename(old_filepath)
// get target path
foldersep = Zotero.isWin ? '\\' : '/'
target_filepath = target_folder + foldersep + filename
target_filepath = OS.Path.normalize(target_filepath)
// target_filepath
// move
OS.File.move(old_filepath, target_filepath)
/* Link and remove old from zotero database */
// linkFromFile
var options = {file: target_filepath, libraryID: parent_item.libraryID, parentItemID: parent_item.id, collections: undefined, saveOptions: {skipSelect: true}};
Zotero.Attachments.linkFromFile(options)
// erase old attachment
att.eraseTx()
},

// test
// selectedItemsTest: function () {
// let selectedItems = ZoteroPane.getSelectedItems();
// if (selectedItems.length > 0) {
// var msg = "";
// for (let item of selectedItems) {
// msg += `${item.itemType} - ${item.getField("title")}`;
// Zotero.debug(`** ${msg}`);
// }
// Zotero.debug(msg);
// alert(msg);
// }
// }
};

0 comments on commit 64d0aa3

Please sign in to comment.