Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
dev:client_coding:javascript_api [2020/04/16 16:03] – [Running Ad Hoc JavaScript in Zotero] dstillman | dev:client_coding:javascript_api [2022/07/02 18:22] (current) – [Executing the search] dstillman | ||
---|---|---|---|
Line 36: | Line 36: | ||
Non-window scope applies to lower-level code that doesn' | Non-window scope applies to lower-level code that doesn' | ||
- | Overlays and windows | + | Windows |
- | To access Zotero functionality from your own extension, you will need access to the core '' | + | To access Zotero functionality from your own extension, you will need access to the core '' |
<code html> | <code html> | ||
Line 87: | Line 87: | ||
] | ] | ||
); | ); | ||
- | var itemID = await item.save(); | + | var itemID = await item.saveTx(); |
return itemID; | return itemID; | ||
</ | </ | ||
Line 216: | Line 216: | ||
s.addCondition(' | s.addCondition(' | ||
s.addCondition(' | s.addCondition(' | ||
+ | </ | ||
+ | |||
+ | === Search by creator === | ||
+ | |||
+ | <code javascript> | ||
+ | var name = ' | ||
+ | s.addCondition(' | ||
</ | </ | ||
Line 236: | Line 243: | ||
results: | results: | ||
- | <code javascript> | + | <code javascript> |
This returns the item ids in the search as an array. The next thing to do is to get the Zotero items for the array of IDs: | This returns the item ids in the search as an array. The next thing to do is to get the Zotero items for the array of IDs: | ||
- | <code javascript> | + | <code javascript> |
==== Managing citations and bibliographies ==== | ==== Managing citations and bibliographies ==== | ||
Line 340: | Line 347: | ||
return fulltext; | return fulltext; | ||
</ | </ | ||
+ | |||
+ | ==== File I/O ==== | ||
+ | |||
+ | === Getting the contents of a file === | ||
+ | |||
+ | <code javascript> | ||
+ | var path = '/ | ||
+ | var data = await Zotero.File.getContentsAsync(path); | ||
+ | </ | ||
+ | |||
+ | === Saving data to a file === | ||
+ | |||
+ | <code javascript> | ||
+ | var path = '/ | ||
+ | var data = "This is some text."; | ||
+ | await Zotero.File.putContentsAsync(path, | ||
+ | </ | ||
+ | |||
==== To Do === | ==== To Do === | ||
Line 356: | Line 381: | ||
Before proceeding, back up your [[: | Before proceeding, back up your [[: | ||
+ | |||
+ | All examples operate on the currently selected library. | ||
==== Example: Item Field Changes ==== | ==== Example: Item Field Changes ==== | ||
Line 367: | Line 394: | ||
var fieldID = Zotero.ItemFields.getID(fieldName); | var fieldID = Zotero.ItemFields.getID(fieldName); | ||
var s = new Zotero.Search(); | var s = new Zotero.Search(); | ||
- | s.libraryID = Zotero.Libraries.userLibraryID; | + | s.libraryID = ZoteroPane.getSelectedLibraryID(); |
s.addCondition(fieldName, | s.addCondition(fieldName, | ||
var ids = await s.search(); | var ids = await s.search(); | ||
Line 397: | Line 424: | ||
var s = new Zotero.Search(); | var s = new Zotero.Search(); | ||
- | s.libraryID = Zotero.Libraries.userLibraryID; | + | s.libraryID = ZoteroPane.getSelectedLibraryID(); |
s.addCondition(' | s.addCondition(' | ||
var ids = await s.search(); | var ids = await s.search(); | ||
Line 422: | Line 449: | ||
return ids.length + " item(s) updated";</ | return ids.length + " item(s) updated";</ | ||
- | ==== Example: | + | ==== Example: |
- | < | + | Note that this will change all instances of the tag in the library. |
- | <code javascript> | + | Replace " |
- | var ids = []; | + | |
- | var allTags | + | <code javascript> |
- | tags = tags.map(tag => tag.toLowerCase()); | + | var s = new Zotero.Search(); |
- | for (var id in allTags) { | + | s.libraryID |
- | if (tags.indexOf(allTags[id].name.toLowerCase()) | + | s.addCondition('tag', ' |
- | | + | var ids = await s.search(); |
- | | + | if (!ids.length) { |
+ | | ||
} | } | ||
- | Zotero.Tags.erase(ids); | + | await Zotero.DB.executeTransaction(async function () { |
- | </ | + | for (let id of ids) { |
+ | let item = Zotero.Items.get(id); | ||
+ | | ||
+ | await item.save({ | ||
+ | skipDateModifiedUpdate: | ||
+ | }); | ||
+ | } | ||
+ | }); | ||
+ | return ids.length + " tag(s) updated"; | ||
==== Example: Delete Tags By Part of Name ==== | ==== Example: Delete Tags By Part of Name ==== |