Script to read local file, rename the abbr field #374
-
DescriptionIn this script, I want to rename the item's field "Journal Abbr" based on the item's field Publication or Proceedings Title, and a local csv file recording the full names and the short names. However, it doesn't work. I'm totally new to JavaScript and Zotero API. So, I'd like to know where to start fix it. EventNone OperationScript Data/**
* A description of this script.
* @author Zotero Community
* @usage
* @link https://github.com/windingwind/zotero-actions-tags/discussions/
* @see https://github.com/windingwind/zotero-actions-tags/discussions/
*/
const fs = require("fs");
const { Zotero, Zotero_Tabs, console, alert } = require('window');
function loadTable(path) {
const tableData = fs.readFileSync(path, 'utf8').split('\n').map(line => line.split(','));
const table = {};
for (const row of tableData) {
const [key, value] = row;
table[key] = value;
}
return table;
}
function FindAbbr(journal, table) {
return table[journal] || "Unknown";
}
if (item) {
const path = "E:\\OneDrive - sjtu.edu.cn\\Others\\Zotero\\JournalAbbr.csv";
const table = loadTable(path);
const journal = item.getField("Publication");
const abbr = FindAbbr(journal, table);
item.setField("Journal Abbr", abbr);
return item.getField('title');
} else {
return `[Info] No item!`
} Anything elseNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
Zotero is not a Node environment and |
Beta Was this translation helpful? Give feedback.
-
Updated script, which can:
async function loadTable(path) {
try {
const fileContent = await Zotero.File.getContentsAsync(path);
const journalTable = new Map();
const conferenceTable = new Map();
const rows = fileContent.split('\n');
for (const row of rows) {
// Use a regular expression to match the CSV format, including quoted strings
const match = row.match(/^(journal|conference),[ "]*([^"]*)"?,(.+)/);
// console.log(match)
if (match) {
const [_, type, key, value] = match;
if (type && key && value) {
if (type === 'journal') {
journalTable.set(key.trim().toLowerCase(), value.trim());
} else if (type === 'conference') {
conferenceTable.set(key.trim().toLowerCase(), value.trim());
}
}
}
}
return {
journalTable,
conferenceTable
};
} catch (error) {
Zotero.debug("Error reading file: " + error);
return {
journalTable: new Map(),
conferenceTable: new Map()
};
}
}
function FindJournalAbbr(journal, journalTable) {
return journalTable.get(journal.trim().toLowerCase()) || "Unknown"; // Return "Unknown" if the journal is not found
}
function FindConferenceAbbr(conf, conferenceTable) {
// Iterate through the conferenceTable to find an exact match
for (let [fullTitle, abbreviation] of conferenceTable.entries()) {
if (conf.trim().toLowerCase().includes(fullTitle)) {
return abbreviation;
}
}
return "Unknown"; // Return "Unknown" if the conference is not found
}
const path = "E:\\OneDrive - sjtu.edu.cn\\Others\\Zotero\\JournalAbbr.csv";
const {
journalTable,
conferenceTable
} = await loadTable(path);
var items = Zotero.getActiveZoteroPane().getSelectedItems();
for (let item of items) {
if (item.isRegularItem() && !(item instanceof Zotero.Collection)) {
if (item.itemType == 'journalArticle') {
var journal = item.getField("publicationTitle");
var abbr = FindJournalAbbr(journal, journalTable);
if(abbr == "Unknown"){abbr = journal}
item.setField("archive", abbr);
}
else if (item.itemType == 'conferencePaper') {
var proceedingsTitle = item.getField("proceedingsTitle");
if (proceedingsTitle == "") {
var proceedingsTitle = item.getField("conferenceName");
}
const yearPattern = /\b(19|20)\d{2}\b/g;
var yearMatch = proceedingsTitle.match(yearPattern);
var year = yearMatch ? yearMatch[0] : "000";
var confAbbr = FindConferenceAbbr(proceedingsTitle, conferenceTable);
var abbr = confAbbr + year;
item.setField("archive", abbr);
}
else if (item.itemType == 'preprint'){
abbr = item.getField("repository");
item.setField("archive", abbr);
}
else{
item.setField("archive", "Unknown");
}
}
} |
Beta Was this translation helpful? Give feedback.
Also, as you return at the first execution of the for loop, only the first item will be processed