Developing a plugin (with help), unable to get preferences

69 views
Skip to first unread message

Scott Cooper

unread,
May 28, 2025, 4:40:38 PMMay 28
to zotero-dev
With the help of ChatGPT (hopefully that isn't cringe, LOL), I'm trying to build a Zotero 7 plugin. I'm not really a competent developer, though I'm capable and learning. The documentation I've found is thin and I've looked at other plugins, which has helped some but there isn't much consistency, especially with some that work with 6/7.

It's a plugin to automate the generation of tags for items. I have a basic script that works in the "Run Javascript" console and have been able to integrate it into an installable plugin with a working preference pane and menu item (the menu from right-clicking on an item) that runs the script, but I have not been able to retrieve the preferences I have set from the script. ChatGPT helped me with adding logging for troubleshooting but everything I try to retrieve with Zotero.Prefs.get is empty. ChatGPT seems to think the proper window isn't in scope, or something like that but nothing I've tried works.

I'm hoping someone can help me figure this out. Here are some snippets of the code:

In bootstrap.js:
async function startup({ id, version, rootURI }) {
log("Starting: " + myVersion);
Zotero.PreferencePanes.register({
src: rootURI + 'prefs.xhtml',
scripts: [rootURI + 'prefs.js']
});
Services.scriptloader.loadSubScript(rootURI + "itemtagger.js");
ItemTagger.addToAllWindows();
}

In my main plugin script (itemtagger.js):
var ItemTagger = {
menuId: "itemtagger-generate-tags",

addToWindow(win) {
let doc = win.document;
let menu = doc.getElementById("zotero-itemmenu");
if (!menu) return;
// Prevent duplicate
if (doc.getElementById(this.menuId)) return;

let menuitem = doc.createXULElement("menuitem");
menuitem.id = this.menuId;
menuitem.setAttribute("label", "Generate Tags for Item");
menuitem.setAttribute("class", "menuitem-iconic");
menuitem.setAttribute("image", "chrome://zotero/skin/tag.svg");
menuitem.addEventListener("command", async (event) => {
try {
await ItemTagger.runOnSelected(win);
} catch (e) {
Zotero.alert(win, "Item Tagger", "Error: " + e.message);
}
});
menu.appendChild(menuitem);
},

...

async runOnSelected(win) {
let pane = win.ZoteroPane;
let items = pane.getSelectedItems();
if (!items || !items.length) throw new Error("No item selected.");
let selItem = items[0];

let tagThreshold = Zotero.Prefs.get('extensions.itemtagger.tag_threshold', true);
log('Log Zotero font:', Zotero.Prefs.get('font.size'));
log('Log pref:', Zotero.Prefs.get('extensions.itemtagger.tag_threshold', true));

Michael Freeman

unread,
May 29, 2025, 8:18:04 AMMay 29
to zoter...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "zotero-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zotero-dev+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/zotero-dev/a232eef5-4629-4442-b8a4-871b2dcaa36dn%40googlegroups.com.

Scott Cooper

unread,
May 29, 2025, 7:45:49 PMMay 29
to zotero-dev
Thanks. Those didn't help much but I finally got it working. There seemed to be a few issues but mainly, it wouldn't work until I added preferences in a prefs.js file.
Reply all
Reply to author
Forward
0 new messages