[Share] Select a random item #261
dschaehi
started this conversation in
Action Scripts
Replies: 2 comments 3 replies
-
Thanks for the code. But why does it execute twice in a row when I run it in the plugin? It works fine when running it in the Zotero JavaScript console (only runs once) |
Beta Was this translation helpful? Give feedback.
3 replies
-
I used Gemini to modify this script to open the selected item for reading: /**
* Selects a random item from the current view and opens its primary attachment for reading.
* @author Jae Hee Lee (Original), Modified by AI Assistant
* @usage Use with Zotero Action Tags plugin triggered globally (not on a specific item).
* @link https://github.com/windingwind/zotero-actions-tags/discussions/
* @see https://github.com/windingwind/zotero-actions-tags/discussions/
*/
const ZoteroPane = require("ZoteroPane");
// Don't run if triggered directly on an item
if (item) return;
var iv = ZoteroPane.itemsView;
// Check if there are any items in the current view
if (iv.rowCount === 0) {
Zotero.Notifier.show("warning", "Select Random and Open", "No items in the current view.");
return;
}
// --- Select Random Item ---
let randomIndex = Zotero.Utilities.rand(0, iv.rowCount - 1);
let row = iv.getRow(randomIndex);
if (!row) {
Zotero.debug("Select Random and Open: Failed to get row at index " + randomIndex);
return; // Safety check
}
let selectedItemID = row.id;
// Select the item visually in the Zotero pane
await iv.selectItem(selectedItemID);
// --- Open Attachment ---
// Get the Zotero item object from the ID
let selectedItem = await Zotero.Items.getAsync(selectedItemID); // Use getAsync for better performance/safety
if (!selectedItem) {
Zotero.debug("Select Random and Open: Failed to get item object for ID " + selectedItemID);
return; // Safety check
}
// Find the best attachment (usually the first PDF or snapshot)
let attachment = await selectedItem.getBestAttachment();
if (attachment) {
// If an attachment was found, open it
try {
await ZoteroPane.viewAttachment(attachment.id);
Zotero.debug(`Select Random and Open: Opened attachment ${attachment.id} for item ${selectedItemID}`);
} catch (e) {
Zotero.Notifier.show("error", "Select Random and Open", `Error opening attachment for "${selectedItem.getField('title') || 'selected item'}": ${e.message}`);
Zotero.debug(`Select Random and Open: Error opening attachment ${attachment.id}: ${e}`);
}
} else {
// If no suitable attachment was found, notify the user
Zotero.Notifier.show("info", "Select Random and Open", `Item "${selectedItem.getField('title') || 'selected item'}" has no suitable attachment to open.`);
Zotero.debug(`Select Random and Open: No suitable attachment found for item ${selectedItemID}`);
} Assigned |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Description
Select a random item from the list. Very useful if you want to get inspired from papers downloaded years ago.
Event
None
Operation
Script
Data
Anything else
No response
Beta Was this translation helpful? Give feedback.
All reactions