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

A service to log Dictionary lookups Apps
This Service adds any unique words you look up in the OS X Dictionary app to a TextEdit document that is- saved on your desktop, so that you can review, or repeat them later. Then you can use the same service to look up those words that you have logged already; they won't be added to your log file.

The service is seamlessly integrated with the Dictionary service on your Mac. It is even installed on the same keyboard shortcut: Command-Control-D.

The only limitation is that it only works with selected text, so if you are looking up a word by hovering the cursor over it, in, say, Preview or Safari, then you'll have to choose More in the lower right corner of the Dictionary sheet that pops up to view the full Dictionary window. Then you'll have to press Command-Control-D once more, to "log" the word into the text file (the search word turns up selected in the Dictionary window.)

Avoid the above limitation by selecting the word before Command-Control-D. If you select the word before pressing Command-Control-D, then everything is handled automatically.

Since English is not my native language, this is something I have been wanting for years. So it is mostly made for non-native English speaker, but may also be useful for native anglophones.

How to install:

  1. Open Automator

  2. Choose service and don't check anything

  3. Search for the Run AppleScript action, and add it to your Service

  4. Paste in the script below, replacing everything that is by default in the Run AppleScript action

  5. Save the Service as dictLogger

  6. Quit Automator

  7. Open the Keyboard preferences of the System Preferences pane

  8. Find the service under "Services" and install it with the keyboard shortcut Command-Control-D

  9. After you have successfully looked up a word, look into the file "DictLogger.txt" that should be on your Desktop if everything is working

  10. Select a word in "DictLogger.txt", to see that it works from here

  11. If you use some app other than TextEdit for .txt files, be sure to set the default app of this file to TextEdit, if you want it to open with TextEdit. I don't guarantee that every other text editor will work, though I think it will work with TextWrangler and BBEdit

# © McUsr/MacUser06 2012
on run {input, parameters}
    set glossaryName to "DictLogger.txt"
    set AutomatorIcon to (a reference to file ((path to applications folder as text) & "Automator.app:Contents:Resources:Automator.icns"))

    # checks to see if the current selection contains anything valid
    considering diacriticals
        if first character of (input as text) is not in "abcdefghijklmnopqrstuvwxyz" then
            tell application "System Events" to set appname to name of first process whose frontmost is true
            using terms from application "Finder"
                tell application appname
                    display dialog "The selection you tried to look up a dictionary definiton for contains non-valid characters.

Please copy the selection you used into an empty text document to figure out what is wrong.

Quitting for now…" with title "Dictionary Logging Service" buttons {"Ok"} default button 1 with icon AutomatorIcon

                end tell
            end using terms from
            return input
        end if
    end considering
    try
        open location "dict://" & input
    end try
    tell application "TextEdit"
        try
            set glossaryDoc to its document glossaryName
        on error
            set glossaryDoc to null
        end try
    end tell

    try
        set theF to quoted form of (POSIX path of (path to desktop folder as text) & glossaryName)
    end try

    set foundword to true
    try
        do shell script "test -f " & theF & " || touch " & theF

        set foundword to (do shell script "grep '^" & input & "$' " & theF & ">/dev/null && echo \"true\" || echo \"false\"") as boolean
    end try

    if not foundword then

        if glossaryDoc is not null then
            tell application "TextEdit"
                tell its document glossaryName

                    make new paragraph at beginning of its text with data ((input as text) & linefeed)
                end tell
                save glossaryDoc
            end tell
        else
            try
                do shell script "export TMPFILE=`mktemp /tmp/${tempfoo}.XXXXXX` && cat " & theF & "  >$TMPFILE ; echo " & input & ">|" & theF & " ; cat $TMPFILE >>" & theF
            end try
        end if

    end if
    do shell script "open -b \"com.apple.Dictionary\""

    return input

end run
    •    
  • Currently 3.14 / 5
  You rated: 1 / 5 (7 votes cast)
 
[5,794 views]  

A service to log Dictionary lookups | 5 comments | Create New Account
Click here to return to the 'A service to log Dictionary lookups' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Other dictionaries?
Authored by: magnamous on Nov 10, '12 12:19:48AM

This is actually a really nice hint. Is there any way to get it to work with other dictionaries installed? I would like to use this with dictionaries I have in German and Swedish. Also, to be able to create one of these that would route to the English Thesaurus would be nice. Is there any way to do this?



[ Reply to This | # ]
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 | # ]