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:
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.
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.
The line open location "dict://" & input
should be enclosed in a tell application "Dictionary"
block to make sure the Dictionary app is targeted.
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.
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.
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.

try
statement without itson error
section will silently eat errors.