Paste text to last active window (floating/panel)

Hello,

I have created a workflow as system service that calculates some data and pastes them in clipboard.

Then when recalled, it pastes them to the app’s window where it was recalled from.

At the moment I’m running this AppleScript code to achieve the task:


tell application "System Events"
tell (first application process whose frontmost is true)
set active_name to name of it
end tell
set Name_App to item 1 of (get name of processes whose frontmost is true)
end tell
tell application active_name
activate

tell application "System Events" to tell process(active_name) to keystroke "v" using command down

end tell

It works in most of apps, however I’m experiencing some problems with this approach.

In the apps with floating windows/panels it is not pasted to the correct window where the service was recalled from.

I tried also this:


tell application "System Events"
set frontmost of process "active_name" to true
keystroke "v" using command down
end tell

without luck.

Could you please help? Thanks.