Paste text to last active window

Hello,

I’d like to paste the clipboard to the most recent window (the active one).

At the moment I’m using this 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

However I’m experiencing some problems with this approach.

In the apps with multiple windows it is not pasted to the correct window where the script 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.

When you start a script from the script editor, the front process is the script editor process. Therefore, it must be hidden:


-- get script editor name
set editorName to name of current application

tell application "System Events"
	-- hide the editor window
	set visible of process editorName to false -- hide the editor window
	-- only now frotmost process is the process you need, keystroke in its window
	keystroke "v" using command down
end tell

NOTE: the process you name “frontmost” should be frontmost before you go to script editor’s window.

As I write scripts which are used from the editor and scripts saved as applet or droplet I often put these instructions at the beginning of the code.

tell application "System Events" -- get frontmost process
		set frontmostProcess to first process where it is frontmost -- this will be the script process
		if name of frontmostProcess is in {"Script Editor", "AppleScript Editor", "Script Debugger"} then
			set visible of frontmostProcess to false -- hide the script process
			repeat while (frontmostProcess is frontmost) -- wait until the script is hidden
				delay 0.1
			end repeat
			set theApp to name of first process where it is frontmost -- get name of frontmost process (ignoring the script process)
			set frontmost of frontmostProcess to true -- unhide the script process
		else
			set theApp to name of frontmostProcess
		end if
	end tell
-- the “real code”

I listed the two spellings used for the editor delivered by Apple because this old code which was written when the app was named “Script Editor” then renamed “AppleScript Editor” then renamed (temporarily ?) “Script Editor”.
Happily I don’t use applications whose name is not the name of their process.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 6 mai 2020 15:24:52

It seems the answer to the OP’s question varies, depending on how the script is run. I ran the following script by way of FastScripts and it worked as desired in my testing with multiple windows or instances of TextEdit, Numbers, Script Editor, Safari, and Soulver.

tell application "System Events"
	tell process (name of first application process whose frontmost is true)
		keystroke "v" using command down
	end tell
end tell

BTW, the OP states the following, which I don’t understand, so perhaps I don’t fully comprehend what he wants to do (an increasingly-frequent occurrence, unfortunately).

The behavior described by the OP matches what we get when we call a script from a script editor.
This is why using “set visible of process editorName to false” is not safe if the script was called thru Fastscript or by a double-click (applet) or a drag & drop (droplet).

It’s true that if the running application has several windows open when the script is called, it’s not guaranteed that the window targeted by the paste action will be the wanted one.
When I know the name of the application, I often use its Windows menu to trigger explicitly the wanted window before pasting.
But as written, this assume that we know exactly which is the app because we must know the name of the dedicated menu which may be “Windows”, the localized string for “Windows” or even anything else. Happily we need no more info because, as far as I remember, the active windows are listed at the very bottom of the named menu.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 6 mai 2020 15:56:04

Hello guys,

thank you so much for your detailed solutions.

Well, I was not so clear in the first post, so here is a better description and explanation of the issue.

I have identified my issue with the help of VoiceOver.

The window that I want to paste the data is a floating one!

So I have this floating window which includes three dropdown menus and an empty text box underneath them.

What I would like to do is when I click inside this text area and the cursor starts blinking, to run my applet through system services menu and paste the result inside this text area where the cursor is.

The applet is pretty simple, prompts to choose a prefix from a names list, finds the full name and copies it to clipboard and then pastes it.


on run {input, parameters}
set theNames to {"aa", "bb"}
	set theExtensions to {"1", "2"}
	
	set thePF to choose from list theNames with prompt "Choose a prefix:"
	if thePF is false then error number -128
	set theExtension to item (getPositionOfItemInList((thePF as string), theNames)) of theExtensions
	return theExtension
	set the clipboard to theExtension

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

end run


on getPositionOfItemInList(theItem, theList)
	repeat with a from 1 to count of theList
		if item a of theList is theItem then return a
	end repeat
	return 0
end getPositionOfItemInList

The problem is that this applet is activated from inside the active floating window. So when the prompt to choose from the list pops-up, the floating window is not anymore active and the cursor is not blinking so the text cannot be pasted.

So one workaround that I just thought about (if it is feasible) is to get the coordinates of the blinking cursor at the beginning and after the execution of all steps, at the end to click the mouse on those coordinates (in order to activate the floating window) and then to paste the text from clipboard.

@epaminos,

After I undertook to help, many comrades appeared who wanted to do the same. Prior to that, they seemed to be not interested in your question. Much has been said and retold. But not the point. 1) Specifically, which application you are using 2) Which application receives focus after you close the script editor.

  1. It matters because you can avoid mouse clicks in many applications. I will show the method if you are interested.

  2. it matters if it is a Finder

Thank you man for your interest!!!

I really appreciate your valuable help! Always!

My apologies, I was not so explanatory in my first post.

Well, it is a specific app designed for my company.

When I have the floating window inside this app active and then I click anywhere else (on desktop or any other app), then I have to click on the specific app to activate it, once more to make the floating window active and one more time in the text field to start typing. I tried to click only twice in the textbox directly but unfortunately it doesn’t work, three clicks are needed.

So this is where I am stuck, as the main window of the app is shown and there is no text field there and thus no data is pasted anywhere.

So,

when you go to other application (or to desktop), the frontmost application became this other application (or the desktop application, that is Finder). You should hide this other application (or, the Finder).

If this doesn’t help, then many times helps this trik – minimize your company application’s window(s) to Dock, then reveal it again.

And, finally, if the above steps doesn’t help, then use clicking choice

Even if I do not click anywhere else, the execution of the script pops up the window to choose from list so after the selection the main app window is returned by default and I have to click 3 times there.

Another one thing that I noticed and I believe it might help is that the text box is in the middle of the floating window.

So I was thinking that if I could get the coordinates of this floating window and then send 3 mouse clicks there it might work.

@epaminos,

tell me, better, 1) what application takes focus when you hide the other application. That is, what application’s name you see at top of screen, 2) tell me, if the textfield takes focus (again, because it should be focused before you go to other application) when you manually minimize to Dock, then reveal your company application’s window(s)

In any case, if you decide to use the mouse clicks, then know that what you call “3 clicks” is most likely the usual double-click used by many applications.

1.) If I have the floating window active and then if I minimize the whole app and restore it, I see the name of this app on the top menu bar. However the floating window becomes inactive!

2.) if the text field is focused before minimizing the app, then after restoring it, it is not focused anymore and have to click multiple times.

It seems, you do not understand me. 1) click textfiled in your app 2) go to other application 3) close other application’s window, 4) what you see?

So, use double-click. I have not yet seen an application that implements a triple mouse click …

Do you think I didn’t try?

I also used the accessibility inspector and realized that this floating window is actually a html page in a pop up window. This is why is not working!

It seems, you do not understand me. 1) click textfiled in your app 2) go to other application 3) close (not minimize!!!) other application’s window, 4) what application’s name you see on the top of screen?

Oh sorry.

Well I see the floating window, but the text field became inactive.

Now it is clear. You have to use a double click (or, simple left click, I don’t know what of them uses your application).

It looks like your application does not use Cocoa objects. Otherwise, you could try using set focused text field … to true. Or, select text field … via GUI scripting

So double click but how and where?

After the Choose from List popup?

Don’t I need coordinates?

And also how to focus to the text field?


...
return theExtension
   set the clipboard to theExtension

-- HERE?

tell application "System Events"
   tell (first application process whose frontmost is true)
       set active_name to name of it
...

I wonder what the following code returns when your window is active?


tell application "System Events" to tell process (active_name) to tell window 1
	get entire contents
end tell

NOTE: replace active_name with real name