Thesaurus Script

When run this script generates a list of synonyms based on the word in the current row of your outline. It’s inspired by http://thesaurus.land. Right now it’s showing “synonym”, but the returned wordJSON contains many more sets of words you can try out.

Note this is written in JavaScript so to run you will need to change Script Editor to Javascript mode. AppleScript is generally easier for scripting Bike directly, but in this case I needed to also work with the wordnik API, and that’s easier in Javascript :slight_smile:

// This script requires a https://wordnik.com API key. 
// You can get a free key by creating a https://wordnik.com account
// and then visiting https://wordnik.com/users/<username>/API

var apiKey = "REPLACE_WITH_YOUR_API_KEY"

var bike = Application("Bike")
var doc = bike.documents.at(0)
var wordJSON = fetchWordJSON(doc.selectionRow().name())

addWords("synonym", "", wordJSON)

function addWords(type, label, wordJSON) {
	let words = wordsOfType(type, wordJSON)
	var container = bike.documents.at(0).selectionRow()

	if (label.length > 0) {
		let labelRow = new bike.Row({ name: label })
		container.rows.push(labelRow)
		container = labelRow
	}
	
	for (let i = 0; i < words.length; i++) {
		container.rows.push(new bike.Row({ name: words[i] }))
	}
}

function wordsOfType(type, wordJSON) {
	for (let i = 0; i < wordJSON.length; i++) {
		if (wordJSON[i]["relationshipType"] == type) {
			return wordJSON[i]["words"]
		}
	}
	return []
}

function fetchWordJSON(word) {
	let app = Application('Script Editor');
	app.includeStandardAdditions = true;
	let url = "https://api.wordnik.com/v4/word.json/" + encodeURI(word) + "/relatedWords?useCanonical=false&limitPerRelationshipType=10&api_key=" + apiKey
	let shellScript = 'curl -X GET --header "Accept: application/json" "' + url + '"'
	return JSON.parse(app.doShellScript(shellScript))
}
2 Likes