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?
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?
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 }
}
```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.
file
field to the reference. What does the line written to the debug log say?The
defaultPath
is only used whenexportFileData
istrue
, 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.```
(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.
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 }
}
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.
Feel free to add it to https://retorque.re/zotero-better-bibtex/exporting/scripting/