Paste text to last active window

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

A huge list of UI items.

But nowhere is the title of the floating window.

It returned the UI elements from the main window behind the floating one.

I read and reread the entire thread.

It seems that you know which is the target application so, why aren’t you explicitly targeting it thru its name ?

tell application "System Events" to tell process "myProcess" -- EDIT with the real name
	set frontmost to true
	tell (first window whose subrole is "AXFloatingWindow")
		-- work upon this window
	end tell
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 7 mai 2020 12:20:45

So, by using this:


tell application "System Events" to tell process "Salary" -- EDIT with the real name
	set frontmost to true
	tell (first window whose subrole is "AXFloatingWindow")
		get entire contents
	end tell
end tell

I identified the text area of the floating window:

So how can I select this text area and replace the text “Optional” with the chosen result “theExtension” from the list:

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

After the selection from the list, the floating window “Report” is not active and the paste function does not paste any text in the text field of it.

Here is the full script until now:

on run
	
	
	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" to tell process "Salary" 
		set frontmost to true
		tell (first window whose subrole is "AXFloatingWindow")
			tell application "System Events"
				keystroke "v" using command down
			end tell
		end tell
		
		--		
	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

If I understand well, you want to change the value of static texts but I don’t know if the value of these static text is always the same on entry or it may change.
If it’s always the same on entry it’s quite simple.

If it change, maybe these static texts have a property which remains stable, for instance, in a print dialog I see a static text whose some properties are: {title:missing value, help:" Haut 4,23 mm Bas 4,23 mm
Gauche 3,53 mm Droite 2,12 mm"
,value:“210 par 297 mm”, name:“210 par 297 mm”}
If I change the selected paper size from A4 to letter US, these properties become:
{title:missing value, help:" Haut 4,23 mm Bas 4,23 mm
Gauche 3,53 mm Droite 2,12 mm"
,value:“216 par 279 mm”, name:“216 par 279 mm”}

So I may identify the static text by its help and use:

set value of first static text whose help is " Haut 4,23 mm Bas 4,23 mm
 Gauche 3,53 mm Droite 2,12 mm" to myValue

If such stable property doesn’t exist, you must use a table of target indexes.
For instance
your firstValue may have to target : static text 3 of group 2 of text area 1 of group 1 of group 5 of UI element 1 of scroll area 1 of group 1 of group 1 of window “Report” of application process “Salary” of application “System Events”

your secondValue may have to target : static text 7 of group 2 of text area 1 of group 1 of group 5 of UI element 1 of scroll area 1 of group 1 of group 1 of window “Report” of application process “Salary” of application “System Events”

your secondValue may have to target : static text 3 of group 2 of text area 3 of group 1 of group 5 of UI element 1 of scroll area 1 of group 1 of group 1 of window “Report” of application process “Salary” of application “System Events”
and so on.
In such case you may use :

on run
	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
	
	
	tell application "System Events" to tell process "Salary"
		set frontmost to true
		tell (first window whose subrole is "AXFloatingWindow")
			set value of static text 3 of group 2 of text area 1 of group 1 of group 5 of UI element 1 of scroll area 1 of group 1 of group 1 to theExtension
		end tell
		
		--		
	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

I don’t know who designed the target application but I think that using static text as container for a value modified by the user is poor design. Such container would better be text field or combo box.

In my example above, the static text wasn’t modified by the user. It’s the application which rule it according to the selection made in a pop up menu.

I hope that this long message is clear enough.
Let me know if it is.

If you really need to pass thrun the clipboard you need to use something like :

tell application "System Events" to tell process "Salary"
	set frontmost to true
	tell (first window whose subrole is "AXFloatingWindow")
		tell static text 3 of group 2 of text area 1 of group 1 of group 5 of UI element 1 of scroll area 1 of group 1 of group 1
			set {x, y} to its position
			-- you may try to use : click at {x,y} -- but I doubt that it would work
			tell me to do shell script "/usr/local/bin/cliclick c:" & x & "," & y -- third party CLI to click upon the static text
			keystroke "v" using {command down}
		end tell
	end tell
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 7 mai 2020 16:02:27

Is the instruction commented as – IS IT REALLY HERE ? really available in the tested code ?
If it is I may guarantee that nothing will be pasted because the instruction supposed to achieve the task is never reached.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 7 mai 2020 16:35:34

Unfortunately it is still not working! None of the above methods!

Here is the window from my app though to get an idea:
https://imgur.com/a/zRFYCDg

And with Accessibility Inspector I found out that it is in an external HTML that loads in that window named “Report”!

Sorry, it was there from previous trials, amended at later stage to comment.