Skip to content

Instantly share code, notes, and snippets.

@IdoBar
Last active January 26, 2020 12:02
Show Gist options
  • Save IdoBar/9add04d14adaedce8a5743b2a0a0eb71 to your computer and use it in GitHub Desktop.
Save IdoBar/9add04d14adaedce8a5743b2a0a0eb71 to your computer and use it in GitHub Desktop.
Find species names (based on provided array of genus names) and add html tags for italics font
// See these instruction to enable js scripting in Zotero:
// https://forums.zotero.org/discussion/comment/324734/#Comment_324734
var fieldName = "title";
var plantsPathogens = ["Cryptobranchus", "Fusarium", "Mycosphaerella", "Pochonia", "Pogona", "Nannizziopsis", "Armillaria", "Parastagonospora", "Pseudoperonospora", "Phytophthora", "Heterobasidion", "Cochliobolus", "Rhynchosporium", "Aspergillus", "Ascochyta", "Sclerotinia", "Botrytis", "Cicer", "Lens","Oryza", "Eucalyptus", "Arabidopsis", "Vigna", "Phaseolus", "Vicia"];
var aquaticSpecies = ["Oreochromis", "Dicentrarchus", "Pagrus", "Gonostoma","Tursiops", "Gadus", "Danio", "Acanthosentis", "Oryzias", "Seriola", "Cyprinus", "Euthynnus", "Solea", "Nibea", "Thunnus thynnus","Argyrosomus", "Thunnus", "Misgurnus", "Oncorhynchus", "Epinephelus", "Rachycentron", "Clarias", "Tetraodon", "Proterocara", "Latimeria", "Oithona", "Sarda", "Glaucosoma", "Scomberomorus", "Allium", "Aulopus", "Plecoglossus", "Anguilla", "Hoplias", "Poecilia", "Sardinops", "Cyprinus", "Acanthopagrus", "Mus", "Rana", "Chlamys", "Anas", "Nerita", "Nodipecten", "Megalobrama", "Squalus","Mytilus", "Scomber", "Acipenser", "Torpedo", "Takifugu", "Sparus","Morone", "Lepomis","Cichlasoma", "Limanda", "Polyprion","Chelon","Paralichthys" ,"Litopenaeus" ,"Rutilus","Ictalurus" ,"Pelteobagrus","Macrobrachium","Amphiprion","Arapaima", "Huso", "Sciaenops", "Lutjanus", "Perca", "Pangasius", "Latris","Scophthalmus", "Macquaria", "Pleuronectes", "Lates", "Clarias", "Salmo", "Pisum", "Pinctada", "Cristaria", "Mugil", "Chanos", "Pangasius", "Halichoeres"];
// Combine arrays and remove duplicates (see https://stackoverflow.com/a/1584377/5346827)
function arrayUnique(array) {
var a = array.concat();
for (var i=0; i<a.length; ++i) {
for (var j=i+1; j<a.length; ++j) {
if (a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
}
// Merges both arrays and gets unique items
var speciesArray = arrayUnique(plantsPathogens.concat(aquaticSpecies));
for (var i=0; i < speciesArray.length; i++) {
var subs=0;
var oldValue = speciesArray[i];
var fieldID = Zotero.ItemFields.getID(fieldName);
var s = new Zotero.Search;
s.addCondition(fieldName, 'contains', oldValue);
var ids = s.search();
if (ids) {
for (index = 0; index < ids.length; ++index) {
var item = Zotero.Items.get(ids[index]);
var mappedFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(item.itemTypeID, fieldName);
var tempTitle = item.getField('title');
var regExFilt = "(.+[^>])(" + oldValue + " \\w+)(([^w]*\\w?$))";
var re = new RegExp(regExFilt , "g");
// alert( "regex match: " + tempTitle.match( re ) );
if ( !!tempTitle.match( re ) ){
subs++;
}
var newValue = tempTitle.replace(re, '$1<i><span class="nocase">$2</span></i>$3');
item.setField(mappedFieldID ? mappedFieldID : fieldID, newValue);
item.save();
}
if ( subs>0 ){
alert(subs + " items updated for genus: " + oldValue);
} // else {
// alert("No items found");
// }
}
}
@IdoBar
Copy link
Author

IdoBar commented Jan 25, 2020

Updated to add tags to retain letter case (avoid capitalisation of species name) in titles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment