A service to log Dictionary lookups

Nov 08, '12 07:30:00AM

Contributed by: MacUser06

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

Comments (5)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20121106085330476