Copy selected text to the clipboard

I try the use the following AppleScript in the “New Item” tab to copy a selected text with Hook:

set theSelectedText to ""
set savedClipboard to the clipboard
-- Copy selected text to clipboard:
set thePasteboard to current application's NSPasteboard's generalPasteboard()
set theCount to thePasteboard's changeCount()
-- Copy selected text to clipboard:
tell application "System Events" to keystroke "c" using {command down}
-- Check for changed clipboard:
repeat 20 times
    if thePasteboard's changeCount() is not theCount then exit repeat
    delay 0.1
end repeat

set theSelectedText to the clipboard

-- Restore clipboard:
set the clipboard to savedClipboard

It seems that Hook is the frontmost app so no selected text is there to copy. Any ideas how to solve this?

Not at the moment. I think Hook would need to expose the name of the frontmost app at the time it was invoked as a special variable in the scripting framework. I’ve asked internally.

I’ve not tried this, but you could hide the Hook window, or change its window index to “count windows”, then copy the text.