A way of having Zotero use "~" for $HOME paths to avoid git diffs on different machines?

I use Zotero to manage a bibliography that autoupdates a .bib file from a number of different machines. I also commit the .bib file in a git repo.

If the home directory is different on the different machines, the path changes. For example: /home/myname/Zotero on Linux and /Users/myname/Zotero on MacOS.

I see Preferences in Zotero for changing various things to do with the base directory but so far can not see a way of telling Zotero to save the string "~/Zotero" in the .bib file instead of "/Users/myname/Zotero".

Is there an easy way of doing this?
  • Assuming you are not saving attachments, only paths, a BBT postscript like this should do it:

    if (Translator.BetterTeX && !Translator.options.exportFileData && item.attachments && item.attachments.length) {
    for (const att of item.attachments) {
    if (att.localPath) {
    att.localPath = att.localPath.replace(/^((\/home\/myname\/)|(\/Users\/myname\/))/, '~/')
    }
    }
    reference.add({ name: 'file', value: item.attachments, enc: 'attachments' })
    return { cache: false }
    }
  • Thanks, I have figured out how to debug this and see the change in the logs but am still seeing the old paths being displayed. This is what I am doing:

    ```js
    if (Translator.BetterTeX && !Translator.options.exportFileData && item.attachments && item.attachments.length) {
    for (const att of item.attachments) {
    att.localPath = att.localPath.replace(RegExp("^\/.*?\/.*?\/"), "~/")
    }
    Zotero.debug("HERE: " + JSON.stringify(item.attachments))
    reference.add({ name: 'file', value: item.attachments, enc: 'attachments' })
    return { cache: false }
    }
    ```

    I see there is something in the attachments called defaultPath as well which has no root dir. When I change that nothing changes either, still the same old behaviour.

    Is there something wrong with the reference.add? I do not understand why that is there. I do not understand what is the actual record that ends up in the .bib ... I suspect this script does not actually change anything to do with that and it is merely printing things.
  • edited February 5, 2022
    It's there to add the modified file field to the reference. What does the line written to the debug log say?

    The defaultPath is only used when exportFileData is true, but you don't want to do this path modification when exporting attachments. The attachments will first be saved to the default location, and then BBT will error out as it will try to re-save to the modified paths.
  • This is an example from the log

    ```
    (3)(+0000002): HERE: [{"key":"S4GIB757","version":2965,"itemType":"attachment","title":"Snapshot","url":"https://www.hindawi.com/journals/mpe/2018/7928953/","accessDate":"2021-04-11T09:24:06Z","parentItem":"T22FQXZW","linkMode":"imported_url","contentType":"text/html","charset":"utf-8","filename":"7928953.html","tags":[],"relations":{},"dateAdded":"2021-04-11T09:24:06Z","dateModified":"2021-04-11T09:24:06Z","uri":"http://zotero.org/users/121204/items/S4GIB757","localPath":"~/Zotero/storage/S4GIB757/7928953.html","defaultPath":"files/2759/7928953.html"},{"key":"M8QH63FF","version":2965,"itemType":"attachment","title":"Full Text PDF","url":"https://downloads.hindawi.com/journals/mpe/2018/7928953.pdf","accessDate":"2021-04-11T09:24:02Z","parentItem":"T22FQXZW","linkMode":"imported_url","contentType":"application/pdf","charset":"","filename":"Zhu and Yin - 2018 - Stochastic Optimal Control of Investment and Divid.pdf","tags":[],"relations":{},"dateAdded":"2021-04-11T09:24:02Z","dateModified":"2021-04-11T09:24:02Z","uri":"http://zotero.org/users/121204/items/M8QH63FF","localPath":"~/Zotero/storage/M8QH63FF/Zhu and Yin - 2018 - Stochastic Optimal Control of Investment and Divid.pdf","defaultPath":"files/2760/Zhu and Yin - 2018 - Stochastic Optimal Control of Investment and Divid.pdf"}]
    ```

    Unrelated but I'm not sure how you got code blocks in these forums. Must be some other flavour of markdown.
  • You have to use <code>...</code>. This forum (unfortunately) does not support markdown.
  • There was a bug in BBT. A new version is going through the test suite and will drop in half an hour or so, but in the interim, this will work:

    if (Translator.BetterTeX && !Translator.options.exportFileData && item.attachments && item.attachments.length) {
    for (const att of item.attachments) {
    att.localPath = att.localPath.replace(/^\/.*?\/.*?\//, '~/')
    }
    reference.remove('file')
    reference.add({ name: 'file', value: item.attachments, enc: 'attachments' })
    return { cache: false }
    }
  • I grabbed the updated bibtex and it works now! (the old postscript). Awesome! Thanks for that ... small things but significant quality of life improvement as different Zotero's syncing to a .bib file should now largely avoid creating git diffs.
  • Found that this was needed to catch all the entries. Some attachments do not have localPath attributes and the filter was bailing on those:



    if (Translator.BetterTeX && !Translator.options.exportFileData && item.attachments && item.attachments.length) {
    // let text = JSON.stringify(item)
    // if (text.includes("DJ3X5TE7")) {
    // Zotero.debug("HERE: " + text)
    // }
    for (const att of item.attachments) {
    if (att.localPath) {
    att.localPath = att.localPath.replace(RegExp("^\/.*?\/.*?\/"), "~/")
    }
    }
    // Zotero.debug("HERE: " + JSON.stringify(item.attachments))
    reference.add({ name: 'file', value: item.attachments, enc: 'attachments' })
    return { cache: false }
    }



    Also, for future humans landing here, note that to debug this kind of thing it is best to create a small collection of problematic items and manually export that (and view the logs). Much faster iterations.
  • Good catch, those are URL-attachments, they have no localPath.

    Feel free to add it to https://retorque.re/zotero-better-bibtex/exporting/scripting/
Sign In or Register to comment.