Search multi-spaces in Title and replace them with one space? Help needed
Hi,
We have over 28000 entries in our library and more than 1000 have multi-spacing between the words in the titles. This originates from copy-pasting titles from PDF documents into Zotero.
Would it be possible to detect multi-spacings and replace them with one space? Is there a JAVA script ready for this, how can I solve this problem?
Thank you, best regards, Christian
We have over 28000 entries in our library and more than 1000 have multi-spacing between the words in the titles. This originates from copy-pasting titles from PDF documents into Zotero.
Would it be possible to detect multi-spacings and replace them with one space? Is there a JAVA script ready for this, how can I solve this problem?
Thank you, best regards, Christian
var fieldName = "title";
var fieldID = Zotero.ItemFields.getID(fieldName);
var s = new Zotero.Search();
s.libraryID = Zotero.Libraries.userLibraryID;
s.addCondition(fieldName, 'contains', ' ');
var ids = await s.search();
if (!ids.length) {
return "No items found";
}
await Zotero.DB.executeTransaction(async function () {
for (let id of ids) {
let item = await Zotero.Items.getAsync(id);
let mappedFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(item.itemTypeID, fieldName);
let fieldID = mappedFieldID || fieldID;
item.setField(fieldID, item.getField(fieldID).replace(/ {2,}/g, ' '));
await item.save({
skipDateModifiedUpdate: true
});
}
});
return ids.length + " item(s) updated";
(This will work assuming they're regular spaces.)