0

I tried creating a script to automatically search definitions and log the word to a document. Keystroke doesn't seem to work:

on run {input, parameters} -- Save current clipboard content to restore later set oldClipboard to the clipboard -- Copy selected text tell application "System Events" to keystroke "c" using {command down} delay 0.1 -- small wait for clipboard to update set theWord to the clipboard as text -- Safety check (no selection or blank) if theWord is "" then set the clipboard to oldClipboard return end if -- Log file path set logPath to (POSIX path of (path to desktop)) & "dictionary_log.txt" 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 

Only the last word on the clipboard is being searched, and it doesn't log. Permissions were enabled.

New contributor
Rose_Vee is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2
  • Some clarification would be helpful, as I'm not sure what you are trying to do. If this is for an Automator Quick Action, the clipboard and System Events are not even needed, as the system will pass the selected text to the workflow. The dictionary URL scheme looks up the first word passed to it, so I'm not sure what "last word" is supposed to mean, either. Commented yesterday
  • @red_menace last word as in the last word copied (ex. copied for a different application), means that the word intended for the clipboard was not even copied, or passed to the workflow Commented yesterday

2 Answers 2

0

If used as a Quick Action, neither the clipboard nor "System Events" is needed. The usage of the Quick Action would be to select some text and choose the workflow from the Services contextual menu, where it will do its thing with the selection.

For a Quick Action, the header declares what the workflow will receive. Setting this to receive text in any application will determine the context to add the workflow to the Services contextual menu - depending on the settings, it may be added to the main Services menu, but it will be available in one of those Services menus for usage with any selection. You can check the Services Settings menu item to make sure the workflow is enabled.

The "Run AppleScript" action receives the workflow item(s) in its input argument (which will always be a list), so you can get the first word from that for use with the dictionary and log. The results returned from the action will be passed to other actions in the workflow (such as "Copy to Clipboard") for further processing as desired, so the script would be something like:

on run {input, parameters} set theWord to first word of (input as text) -- just one word -- Log file path set logPath to (POSIX path of (path to desktop)) & "dictionary_log.txt" 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 return input -- pass the entire selection to following actions to perform other stuff as desired end run 
0

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 additions as the clipboard is a focus here
  • Gets and activates the front application so that the command-c keystroke has 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.

2
  • Please understand that my edits were made within the guidelines of StackExchange and for web accessibility ( see How do I write an accessible Stack Exchange post?). Code markdown should only be used for code, not emphasis or to "highlight" words. Commented yesterday
  • Then we are in accord. Thanks. Commented 16 hours ago

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.