I've just been having a play around with the API on their website, which features an interactive utility (as most do) that allows you to make test calls to each of the endpoints and retrieve sample data.
Oxford Dictionaries API Documentation
So, in fact, it even constructs the cURL
request for you, which makes it simple.
It seems you're right, in that there doesn't seem to be a document checker that can spell check chunks of text all in one go. But there will be other APIs out there that provide that function, I've no doubt, so I'll just do a short demonstration here to address your AppleScript queries.
This is the endpoint I'll be testing:
LEMMATRON
/inflections/{source_lang}/{word_id}
"Check a word exists in the dictionary and retrieve its root form"
The URL takes two parameters: {source_lang}
and {word_id}
, corresponding respectively to the language choice (en
in our case) and the word selection (for which I'll choose maestro
).
To save a headache, I'm just going to copy the cURL request from their online utility:
curl -X GET --header "Accept: application/json" \
--header "app_id: d0552f68" \
--header "app_key: ac4a6bedb474f5f321b41c09a2c6cd01" \
"https://od-api.oxforddictionaries.com/api/v1/inflections/en/maestro"
To try it out, you can copy/paste this into Terminal using your own app_id
and app_key
. I immediately get this response back:
{
"metadata": {
"provider": "Oxford University Press"
},
"results": [
{
"id": "maestro",
"language": "en",
"lexicalEntries": [
{
"grammaticalFeatures": [
{
"text": "Singular",
"type": "Number"
}
],
"inflectionOf": [
{
"id": "maestro",
"text": "maestro"
}
],
"language": "en",
"lexicalCategory": "Noun",
"text": "maestro"
}
],
"word": "maestro"
}
]
}
So we know it works. If I perform the same cURL request using a different word_id
for a word that shouldn't exist, I can preview the response in that situation too:
curl -X GET --header "Accept: application/json" \
--header "app_id: d0552f68" \
--header "app_key: ac4a6bedb474f5f321b41c09a2c6cd01" \
"https://od-api.oxforddictionaries.com/api/v1/inflections/en/foozlebum"
which returns:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>No lemmas found matching supplied source_lang and word</p>
Getting Keyboard Maestro to perform these calls for you is easy enough. All of this is shell script, so these commands can actually plonk straight into an Execute a Shell Script action and the result can be stored in a variable:
An Example Keyboard Maestro Word Checker Macro
Result:
But, it can also be done quite easily in AppleScript as well:
set word_id to "example"
set cURL to "curl -X GET" & ¬
" --header 'Accept: application/json'" & ¬
" --header 'app_id: 0b57ad70'" & ¬
" --header 'app_key: 8aca301f5bddd190bc7c3a5fcd9ce78d'" & ¬
" 'https://od-api.oxforddictionaries.com/api/v1/inflections/en/" & word_id & "'"
do shell script cURL
if the result contains "\"results\": [" then
return true
else
return false
end if
The AppleScript Executing in Script Editor
To finish, I think we can say that this specific API isn't properly suited to word checking an entire document, but the principles are the same across different APIs that are available on the web.
Although, if you were forced to check each word individually, it would very easy to implement, but take a bit of time due to bottlenecking. AppleScript conveniently has some functions that can deal with text quite nicely. If your document is on the clipboard, one can command:
repeat with w in every word of (get the clipboard)
# Execute your cURL request here
end repeat
Hope this helps. APIs are pretty damn awesome, and I use many for different things: one to automate taking screenshots of webpages; one that retrieves geolocation info based on my IP address; one that updates my web domain dynamically to point to my external IP address; one to automatically download media from websites; and one that provides access to lots of scientific and mathematical data resources.