Mobile citation autocompletion guide using QuickAdd and Various Complements

Nvm, I managed to do that. I’m also setting up your bulk import code to automatically import a literature note while opening obsidian. Thanks for these amazing scripts!!

I updated the script to add only the updated items rather than all the items, as it takes quite some time to launch. Exporting zotero collections in .bib format doesn’t have any data about the data/time of the last modification of the attachment. So I changed it to JSON and here’s the updated script if anybody needs it.

const fs = require('fs');
const path = require('path');

// Function to get the absolute path of the JSON file
function getAbsolutePath(relativePath) {
    let resourceUrl = app.vault.getResourcePath(app.workspace.getActiveFile());
    let vaultPath = decodeURIComponent(resourceUrl.replace(/app:\/\/[^\/]+\//, '/').split('?')[0]);
    let vaultRoot = vaultPath.split(app.workspace.getActiveFile().path)[0]; // Get the root directory of the vault
    return path.join(vaultRoot, relativePath);
}

// Replace 'path/to/betterbibtex/json/file' to the relative path of the BetterBibtex exported JSON file inside the obsidian vault
const relativeJsonFilePath = "path/to/betterbibtex/json/file";
// Replace path/to/json/file with relative path of another JSON file to save previously imported citekeys with timestamp
const relativeImportedCitekeysFilePath = "path/to/json/file";

const jsonFile = getAbsolutePath(relativeJsonFilePath);
const importedCitekeysFile = getAbsolutePath(relativeImportedCitekeysFilePath);

// Extract citekeys and their modification times from the JSON file
function extractCitekeysWithTimestamps(jsonFile) {
    const jsonData = JSON.parse(fs.readFileSync(jsonFile, 'utf-8'));
    const items = jsonData.items;
    let citekeysWithTimestamps = [];

    for (const item of items) {
        const citekey = item.citationKey || item.key;
        const timestamp = new Date(item.dateModified);
        citekeysWithTimestamps.push({ citekey, timestamp });
    }

    return citekeysWithTimestamps;
}

function loadImportedCitekeys(file) {
    if (fs.existsSync(file)) {
        const data = fs.readFileSync(file, 'utf-8');
        return JSON.parse(data);
    }
    return [];
}

function saveImportedCitekeys(file, citekeysWithTimestamps) {
    fs.writeFileSync(file, JSON.stringify(citekeysWithTimestamps, null, 2), 'utf-8');
}

// Usage
const currentCitekeysWithTimestamps = extractCitekeysWithTimestamps(jsonFile);
const previousCitekeysWithTimestamps = loadImportedCitekeys(importedCitekeysFile);

// Find new or updated citekeys that need to be imported
const newOrUpdatedCitekeys = currentCitekeysWithTimestamps.filter(currentEntry => {
    const previousEntry = previousCitekeysWithTimestamps.find(prev => prev.citekey === currentEntry.citekey);
    return !previousEntry || new Date(currentEntry.timestamp) > new Date(previousEntry.timestamp);
});

if (newOrUpdatedCitekeys.length > 0) {
    let itemCount = newOrUpdatedCitekeys.length;
    new Notice(`Importing ${itemCount} items`, 5000);
    for (const { citekey } of newOrUpdatedCitekeys) {
        itemCount--;
        // Replace 'Create Literature Note' with the name of your import format
        app.plugins.getPlugin('obsidian-zotero-desktop-connector').runImport('Create Literature Note', citekey);
        // If you are importing from a group library, use the appropriate libraryID as explained before
        // app.plugins.getPlugin('obsidian-zotero-desktop-connector').runImport('Testing ground', citekey, 2);
        new Notice(`Imported ${citekey}, ${itemCount} remaining`);
    }
    new Notice("Import complete!", 3000);

    // Update the stored list of imported citekeys with timestamps
    saveImportedCitekeys(importedCitekeysFile, currentCitekeysWithTimestamps);
} else {
    new Notice("Nothing to import");
}

Thank you @Feralflora!

1 Like