1

After several times of having my MacBook shutdown and losing the several tabs I had opened of the words I looked up, I've started to look for a solution.

I'm currently trying to implement a script that looks up a word in the Dictionary app on Mac and extracts its definition and example, then saves it in a file called Dictionary Look Up Service History Log File.txt. However, I am encountering some issues with the script and would appreciate any help that I can get.

Here's what the script should do: the user enters a word, the script checks if the word contains any invalid characters, opens the Dictionary app, looks up the word, extracts its definition and example and saves it in a file, formats the result, and displays it in a dialog box.

I have tried to run the script, but I keep getting errors. I suspect that I might have made a mistake in the syntax, but I am not sure where. I would be grateful if someone could take a look at my script and help me identify any errors that I might have made.

on run {input, parameters}
    try
        considering diacriticals
            if first character of (input as text) is not in "abcdefghijklmnopqrstuvwxyz" then
                tell current application
                    activate
                    display dialog "The selected text starts with a non-valid character." & return & return & ¬
                        "Make a different selection and try again." buttons {"OK"} default button 1 ¬
                        with title "Dictionary Look Up Logging Service"
                end tell
                return
            end if
        end considering
        open location "dict://" & input
        set theDictionaryHistoryFilename to "Dictionary Look Up Service History Log File.txt"
        set theTargetFilename to quoted form of (POSIX path of (path to documents folder as string) & theDictionaryHistoryFilename)
        set foundSelectedWord to (do shell script "grep '^" & input & "$' " & theTargetFilename & " > /dev/null; echo $?") as integer
        if foundSelectedWord is greater than 0 then
            do shell script "echo \"" & input & "\" >> " & theTargetFilename
        end if
    end try
end run

It's very crucial that the definitions stored are also organized like this (for e.g.):

enter image description here

Thank you in advance for your help.

1
  • 1
    The Dictionary.app is not scriptable - is that what you are asking about? What errors are you getting? Note that a try statement without its on error section will silently eat errors.
    – red_menace
    Mar 5 at 4:45

1 Answer 1

-1

TL;DR: You can't script the Dictionary.

Read on for my journey of discovery. I started looking at the script and error checking it before I found out that Dictionary isn't scriptable. Ah well… Hopefully my help wasn't in vein.

I hope I understand the issue correctly. Are you trying to run this script from within Safari via the Scripts menu in the menu bar?

I placed it in ~/Library/Scripts/.

Just by looking at your script, it looks like you're on the right track with your code. Here are some issues I see in your code that could be causing errors:

  1. The on run handler does not have a return value specified. Also, if you're not passing any input or parameters to the script, you can simply remove the {input, parameters} from the on run line. Correct me, if I'm missing something here, of how you call the script. Maybe you can detail where you place the script.

  2. The line if first character of (input as text) is not in "abcdefghijklmnopqrstuvwxyz" then should be changed to if the first character of input is not in "abcdefghijklmnopqrstuvwxyz" then to compare the first character of the input directly, without needing to convert it to text.

  3. The line open location "dict://" & input should be enclosed in a tell application "Dictionary" block to make sure the Dictionary app is targeted.

  4. The line set theTargetFilename to quoted form of (POSIX path of (path to documents folder as string) & theDictionaryHistoryFilename) should be changed to set theTargetFilename to quoted form of (POSIX path of (path to documents folder) & theDictionaryHistoryFilename) since the path to documents folder command already returns a POSIX path.

  5. The line do shell script "echo "" & input & "" >> " & theTargetFilename should be changed to do shell script "echo " & quoted form of input & " >> " & theTargetFilename to properly handle input words with spaces or special characters.

  6. As red_menace mentioned, the on error handler should be added to the end of the try block. If an error occurs during the execution of the code within the try block, the on error handler will be triggered, and the error message and error number will be displayed in a dialog box. You can customise the error message and formatting as needed. You can also add multiple on error handlers to handle specific types of errors if needed. For example, you can add a on error number -1712 handler to handle errors that occur when the Dictionary app is not open.

With this the revised script with these fixes is:

on run
    try
        considering diacriticals
            if the first character of input is not in "abcdefghijklmnopqrstuvwxyz" then
                tell current application
                    activate
                    display dialog "The selected text starts with a non-valid character." & return & return & ¬
                        "Make a different selection and try again." buttons {"OK"} default button 1 ¬
                        with title "Dictionary Look Up Logging Service"
                end tell
                return
            end if
        end considering
        tell application "Dictionary"
            activate
            open location "dict://" & input
            set theDictionaryHistoryFilename to "Dictionary Look Up Service History Log File.txt"
            set theTargetFilename to quoted form of (POSIX path of (path to documents folder) & theDictionaryHistoryFilename)
            set foundSelectedWord to (do shell script "grep '^" & input & "$' " & theTargetFilename & " > /dev/null; echo $?") as integer
            if foundSelectedWord is greater than 0 then
                do shell script "echo " & quoted form of input & " >> " & theTargetFilename
            end if
        end tell
        display dialog "Definition and example for " & input & ":" & return & return & ¬
            (do shell script "osascript -e 'tell application \"Dictionary\" to ¬
            set d to first result whose title = \"" & input & "\"' -e 'get \"    Definition: \" & definition of d & return & return & \"    Example: \" & (example of d)'")
    on error errorMessage number errorNumber
        tell current application
            activate
            display dialog errorMessage & return & return & "Error Number: " & errorNumber buttons {"OK"} default button 1 ¬
                with title "Dictionary Look Up Logging Service"
        end tell
    end try
end run

This revised code should properly check for invalid characters, open the Dictionary app, look up the input word, save it in a file, format the result, and display it in a dialog box.

I didn't get it to run as I wasn't sure how you run your existing script, but it may also be that Dictionary isn't scriptable as red_menace suggests. So I did another search and found this source:

You can obtain an initial overall survey of the situation by means of Apple's Script Editor program (it's in /Applications/AppleScript): choose File > Open Dictionary, which displays a list of applications present on your computer that the Script Editor thinks are scriptable. You should, however, regard this list with a bit of suspicion, and confirm that a particular application really is or is not scriptable. To do so, choose Window > Library, and in the Library window, press the "+" button (or Control-click to get the contextual menu, and choose Add). You'll see a standard Open dialog. Navigate to an application, select it, and press Open. One of two things will happen:

The application is reported as not scriptable

An error dialog may appear, stating: "Unable to add the application or extension because it is not scriptable." In this case, the application is definitely not scriptable and that's the end of that.

The application is added to the Library window

This means that the application might be scriptable. But you might have a false positive. To find out, double-click the application's listing in the Library window. This should open the application's dictionary display (see Figure 2-2). Even if it does, you still might have a false positive. Explore the dictionary, clicking the various Suites (in the first column of the browser) and looking through the classes and commands (in the second column) to make sure that these actually do something appropriate to the function of that particular application.

A good example is the application called, appropriately enough, Dictionary (located in /Applications). In Script Editor, you can open the Dictionary application's dictionary display; this dictionary has about two dozen entries. But it's a false positive; the Dictionary application is not really scriptable.

screenshot of Script Editor showing Dictionary is not scriptable

2
  • 1
    The script appears to be from Automator’s Run AppleScript, which would get its input from the previous action. The Dictionary tell statement isn’t going to do anything, as there aren’t any app commands in it (even if it were scriptable).
    – red_menace
    Mar 5 at 22:16
  • Right! Before I noticed that Dictionary isn't scriptable I only looked at the script per se and noticed a few things that could've been incorrect. But, yes, there are no app specific commands. Thanks. Mar 6 at 3:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .