Skip to content

Instantly share code, notes, and snippets.

@sjgknight
Created November 15, 2023 00:56
Show Gist options
  • Select an option

  • Save sjgknight/9a1ba937220621abcda38f401dbe7c80 to your computer and use it in GitHub Desktop.

Select an option

Save sjgknight/9a1ba937220621abcda38f401dbe7c80 to your computer and use it in GitHub Desktop.
A javascript snippet to be used in zotero js developer interface, batch select in the UI and it will convert '1 field' names into '2 field' names. Likely to be quite imperfect...
var newFieldMode = 0; // 0: two-field, 1: one-field (with empty first name)
const zoteroPane = Zotero.getActiveZoteroPane();
const selected = zoteroPane.getSelectedItems();
for (const item of selected) {
let creators = item.getCreators();
let newCreators = []; // Initialize newCreators array
for (let creator of creators) {
if(creator.fieldMode == 1){
let oldname = creator.lastName;
creator.fieldMode = newFieldMode;
creator.firstName = oldname.split(' ').slice(0, -1).join(' ');
creator.lastName = oldname.split(' ').slice(-1).join(' ');
}
newCreators.push(creator);
}
item.setCreators(newCreators);
await item.save();
}
return selected.length + " item(s) updated";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment