A script to define a word using dict.org

to getDefinition(entry)
	set D to (do shell script "curl dict://dict.org/d:" & entry)
	if D contains "no match" then
		return "No match found by dict.org"
	else
		set {astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "151"}
		set part to last text item of D
		set AppleScript's text item delimiters to "250"
		set def to first text item of part
		set AppleScript's text item delimiters to astid
		return def
	end if
end getDefinition

display dialog getDefinition("concupiscent")

Hi Adam,

Your script only returns the last definition found. You can limit by dictionary and number of definitions returned in the url:

do shell script "curl dict://dict.org/d:<your word here>:wn:1"

Where the wn is the code of one dictionary database (The WordNet® 1.6 Database in this case) (see http://www.dict.org/bin/Dict?Form=Dict4 for other database codes). and the final 1 is the maximum number of records returned.

This was my (not particularly tidy) take on doing this:

set the_word to text returned of (display dialog "Enter Word" default answer "")

-- Do work
set theDef to get_definition(the_word)
-- Extract definition only
try
	set {TIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {return}}
	set theDef to (paragraphs 5 thru -4) of theDef as text
	set AppleScript's text item delimiters to TIDs
on error
	set theDef to "None found"
end try
-- Returns def.
display dialog theDef

on get_definition(the_word)
	try
		-- Note :wn:1 (uses WordNet and collects one def. only)
		return do shell script "cURL " & (quoted form of ("dict://dict.org/d:" & the_word & ":wn:1"))
	on error the_err
		return the_err
	end try
end get_definition

Best wishes

John M

/usr/bin/curl -s dict://dict.org/d:your_word_here | /usr/bin/grep -v '^[0-9]\{3\}'

That will strip out the lines that begin with three number characters. This seems to be fairly accurate. If you translate it to AppleScript, remember to escape the escape characters:

do shell script "/usr/bin/curl -s dict://dict.org/d:" & yourWordVariable & space & "| /usr/bin/grep -v '^[0-9]\\{3\\}'"

We also throw curl’s -s flag so it doesn’t give us data transfer progress updates.

Hi Mikey San,

Your Applescript code returns ‘The command exited with a non-zero status.’ due to an extra space. This seems to work.

do shell script "/usr/bin/curl -s dict://dict.org/d:" & yourWordVariable & space & "| /usr/bin/grep -v '^[0-9]\\{3\\}'"

BTW, it’s a neat solution, I’m really going to catch up on regular expressions.

Best wishes

John M

Tpyos suck. Corrected.

Or to get several (combining some ideas here):

set the_word to "perspicacious"
try
	set D to (do shell script "curl -s dict://dict.org/d:" & the_word & ":gcide:3" & space & "| /usr/bin/grep -v '^[0-9]\\{3\\}'")
	set D2 to (do shell script "curl -s dict://dict.org/d:" & the_word & ":wn:3" & space & "| /usr/bin/grep -v '^[0-9]\\{3\\}'")
	display dialog "The Collaborative International Dictionary says:" & return & D & return & "World Net Dictionary says" & return & D2
on error
	display dialog "Oops"
end try