As the @red_menace suggests, the nature of how this script is called is unclear. I've made the assumption that it will be called using an Automator workflow that is accessed via the script menu (ie in the User's Library > Scripts folder. Of course, adjustments can be made for different approaches.
The following should be placed inside a Run Applescript action.
use scripting additions
on run {input, parameters}
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell
set oldClipboard to the clipboard
tell application frontApp
activate
tell application "System Events"
tell application process frontApp
keystroke "c" using command down
delay 0.1
end tell
end tell
set theWord to first word of (the clipboard as «class utf8»)
delay 0.1
end tell
-- Log file path
set logPath to (POSIX path of (path to desktop)) & "dictionary_log"
do shell script "touch " & quoted form of logPath
-- Append the word to the log
do shell script "echo " & quoted form of theWord & " >> " & quoted form of logPath
-- Open Dictionary to that word
do shell script "open dict://" & quoted form of theWord
-- Restore clipboard
set the clipboard to oldClipboard
end run
The additions to the OP script are as follows.:
- Includes
use scripting additionsas the clipboard is a focus here - Gets and activates the front application so that the command-c keystroke
keystrokehas something to work on - Truncates after first word so that the log file doesn't collect extraneous verbiage
- Explicitly use utf-8
I tested it on a few apps, including Firefox and Dictionary itself, without issue.