Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Other dictionaries?' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Other dictionaries?
Authored by: MacUser06 on Nov 15, '12 07:37:33PM

It might be doable, if you can make your dictionaries show up in Dictionary.app, otherwise you'll have to make something that uses the app that ships with your dictionaries.

The script below should be possible to rework to use against a dictionary that are pure ascii / Mac Roman / Utf-8.

If you can make the app for the dictonary respond to applescript, or if it is scriptable, then you may be able to show the definition of the word in that app by a reworked copy of the script. Permission Granted! ;)

However, most dictionaries are stored in xml, so I believe you would have to use an XML tool, to make a clean list of dictionary words. That is so much easier than converting from one dictionary format to another! :)

Here is the script. Hopefully no parts are amiss! Tell me if it doesn't work!

The script runs well from the script menu, from Quick Silver or similiar, and is great when saved as an app, so you can reach it from Spotlight. It shares the DictLogger.txt file with the Automator Service, And TextEdit!


script DictionaryIndexAndLog
	#  MacUser06/McUsr 2012 in collaboration with Nigel Garvey! You may not post this elsewhere nor infringe it.
	property scriptTitle : "Dictionary Index and log"
	property dictionaryIcon : (a reference to file ((path to applications folder as text) & "Dictionary.app:Contents:Resources:Dictionary.icns"))
	property strictlyForDictionary : false # change this to true if you want only strictlyForDictionary matches for dictionary, AND to avoid typing "^"AND "$" ;) ( you can also change the parameters for grep to allow for Extended or Perl patterns (man grep)
	# This is great for learning the basics of regexp!!!
	property cachespath : ((path to library folder from user domain as text) & "caches:" & "net.mcusr.Dictionary.Websters.lookup")
	property glossaryName : "DictLogger.txt"
	local script_cache
	try
		set script_cache to load script alias cachespath
	on error
		script newScriptCache
			property searchWord : ""
		end script
		set script_cache to newScriptCache
	end try
	
	local formerSearch, searchWord
	set searchWord to script_cache's searchWord
	local failed
	set failed to false
	
	tell application (path to frontmost application as text)
		try
			set searchWord to text returned of (display dialog "Enter a search term, Miniminum one letter. Wildcards where you like them!" with title my scriptTitle default answer searchWord with icon dictionaryIcon)
		on error
			set failed to true
		end try
	end tell
	if searchWord = "" or failed then
		return 0
	else
		if strictlyForDictionary then
			set searchWord to quoted form of ("^" & searchWord & "$")
		else
			set searchWord to quoted form of searchWord
		end if
		local searchresult
		try
			set searchresult to (do shell script "egrep " & searchWord & " /usr/share/dict/web2 ; exit 0 ")
		end try
		if searchresult = "" then
			tell application (path to frontmost application as text)
				try
					display dialog "I didn't find any match" with title my scriptTitle with icon dictionaryIcon buttons {"Ok"} default button 1
				end try
				# set searchWord to formerSearch
			end tell
		else
			# We save the successful search
			if strictlyForDictionary then
				set searchWord to text 3 thru -3 of searchWord
			else
				set searchWord to text 2 thru -2 of searchWord
			end if
			
			set script_cache's searchWord to searchWord
			store script script_cache in cachespath replacing yes
			
			local wordlist
			set wordlist to every paragraph of searchresult
			tell me to activate
			try
				set wordToLookup to choose from list wordlist default items item 1 of wordlist with prompt "Choose word to lookup" with title my scriptTitle
				
			on error
				tell application (path to frontmost application as text)
					try
						display dialog "Too many words to show in list! Please try again!" with title my scriptTitle with icon dictionaryIcon buttons {"Ok"} default button 1
						set wordToLookup to false
					end try
				end tell
				
			end try
			
			if wordToLookup is not false then
				
				local glossaryfile
				set glossaryfile to ((path to desktop folder as text) & glossaryName)
				try
					set theF to quoted form of POSIX path of glossaryfile
				end try
				log theF
				local foundword
				set {foundword, wordToLookup} to {true, wordToLookup as text}
				tell application "Dictionary" to open location "dict:///" & wordToLookup
				try
					do shell script "test -f " & theF & " || touch " & theF
					
					set foundword to (do shell script "grep '^" & wordToLookup & "$' " & theF & ">/dev/null && echo \"true\" || echo \"false\"") as boolean
				end try
				
				if not foundword then
					local glossarydoc
					
					tell application id "com.apple.textedit"
						try
							set glossarydoc to its document glossaryName
						on error
							set glossarydoc to null
						end try
					end tell
					
					if glossarydoc is not null then
						tell application id "com.apple.textedit"
							tell its document glossaryName
								
								make new paragraph at beginning of its text with data ((wordToLookup as text) & linefeed)
							end tell
							save glossarydoc
						end tell
					else
						try
							do shell script "export TMPFILE=`mktemp /tmp/${Dicttempfoo}.XXXXXX` && cat " & theF & "  >$TMPFILE ; echo " & wordToLookup & ">|" & theF & " ; cat $TMPFILE >>" & theF
						end try
					end if
				end if
				do shell script "open -b \"com.apple.Dictionary\""
				
			end if
		end if
	end if
	return
end script
tell DictionaryIndexAndLog to run
Edited on Nov 15, '12 08:19:34PM by MacUser06


[ Reply to This | # ]
Other dictionaries?
Authored by: MacUser06 on Nov 16, '12 06:04:57AM

Hello. I just want to add that the icon that I display in the dialog is copyright Apple. Inc.

I hope I haven't done anything wrong, since I don't charge for the script, and the icon is preinstalled on every machine, and gives a good hint about what the script is doing.

If you share the script as a script, the receipient will have to recompile it, since the icon is stored as a property, holding a reference to the icon on your machine, which it obviously won't find elsewhere.

But that isn't so obvious for a user novice to Apple Script, I should really have stated that above, together with the copyright of Apple for the icon.

Edited on Nov 16, '12 06:09:10AM by MacUser06



[ Reply to This | # ]
Other dictionaries?
Authored by: magnamous on Nov 16, '12 08:51:08AM

Thanks!

I haven't had a chance to try it yet, but I just wanted to clarify that I meant add-on dictionaries for Dictionary.app, such as are mentioned in this comment. I don't know how (or if) that affects the script that you wrote, but I thought I should mention it.



[ Reply to This | # ]
Other dictionaries?
Authored by: MacUser06 on Nov 21, '12 11:33:57AM

Thanks for the heads up, I'll have a second look at it. I have managed to add some dicitonaries myself. (LOLspeak, BabylonEnglish, and American Idioms.)

I found the process hard. I don't know how to set the default language or anything, at this moment, but that is things I would have to take into account.

I hope, and believe that Apple will make the dictionary a multilingual experience. It is one of those details that sets Os X out from rest of the pack!

Edited on Nov 21, '12 11:34:28AM by MacUser06



[ Reply to This | # ]