-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
170fe54
commit e0cef62
Showing
2 changed files
with
78 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Open Tools-Developer-Run Javascript | ||
* Check "Run as async function" | ||
* Past this file into "Code" | ||
* Click "run" | ||
The results screen should now show the number of items modified | ||
*/ | ||
|
||
let items = [] | ||
for (const lib of libraries = Zotero.Libraries.getAll()) { | ||
items = items.concat(await Zotero.Items.getAll(lib.libraryID)) | ||
} | ||
|
||
const fieldID = { | ||
archiveLocation: Zotero.ItemFields.getID('archiveLocation'), | ||
extra: Zotero.ItemFields.getID('extra'), | ||
} | ||
|
||
async function moveArchiveLocation(item) { | ||
if (! Zotero.ItemFields.isValidForType(fieldID.extra, item.itemTypeID)) return | ||
|
||
let save = false | ||
let extra = (item.getField('extra') || '').split('\n') | ||
if (extra.length == 1 && !extra[0]) extra = [] | ||
|
||
if (Zotero.ItemFields.isValidForType(fieldID.archiveLocation, item.itemTypeID)) { | ||
const archiveLocation = item.getField('archiveLocation') || '' | ||
if (archiveLocation) { | ||
item.setField('archiveLocation', '') | ||
extra.push(`EdTechHub.ItemAlsoKnownAs: ${archiveLocation}`) | ||
save = true | ||
} | ||
} | ||
|
||
extra = extra.map(line => { | ||
const renamed = line.replace(/^archiveLocation:/, 'EdTechHub.ItemAlsoKnownAs:') | ||
save = save || renamed !== line | ||
return renamed | ||
}) | ||
|
||
if (save) { | ||
item.setField('extra', extra.join('\n')) | ||
await item.save() | ||
} | ||
return save | ||
} | ||
|
||
let saved = 0 | ||
await Zotero.DB.executeTransaction(async () => { | ||
for (const item of items) { | ||
if (await moveArchiveLocation(item)) saved += 1 | ||
} | ||
}) | ||
return saved |