Paste text to last active window

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.

In your message #23 you wrote:

So, if as you wrote, the window “Report” is a floating window,
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” really exists.

I carefully wrote:

It seems that you missed the important words : For instance
I wrote 3 as I would have written 987 because as I am not a sooth sayer I can’t guess which is the real index of the static text to set.
Now, given your screenshot, I assume that the true index is 1, so, may you try to use this subset of the script

set theExtension to "applescript"
tell application "System Events" to tell process "Salary"
	set frontmost to true
	tell (first window whose subrole is "AXFloatingWindow")
		set itsName to its name
		log its properties -- ADDED to get infos upon the window
		if exists 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 then
			set value of static text 1 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
		else
			error "there is no 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 in the window “" & itsName & "”"
		end if
	end tell
end tell

and let me know if it correctly replace the word “Optional” by the word “applescript”.
If it doesn’t, let me know what is reported in the log history.

You may also try this alternate version in which I assume that you have a single window named “Report”

set theExtension to "applescript"
tell application "System Events" to tell process "Salary"
	set frontmost to true
	tell window "Report"
		log its properties -- ADDED to get infos upon the window
		if exists 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 then
			set value of static text 1 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
		else
			error "there is no 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 in this window “Report”"
		end if
	end tell
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 7 mai 2020 18:14:32

Here are the results:

However the greyed out text “Optional” from the text box has not been replaced.

Try to run:

set theExtension to "applescript"
tell application "System Events" to tell process "Salary"
	set frontmost to true
	tell window "Report"
		log its properties -- ADDED to get infos upon the window
		if exists 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 then
			tell text area 1 of group 1 of group 5 of UI element 1 of scroll area 1 of group 1 of group 1
				class of UI elements --> it's supposed to return at least {group, group}
				tell group 1
					class of UI elements --> I have no idea of what it's supposed to return {???}
				end tell -- group 1
				tell group 2
					class of UI elements --> it's supposed to return at least {static text}
					properties of every static text
					set value of static text 1 to theExtension
					delay 0.2
					properties of static text 1
				end tell -- group 2
			end tell -- text area 1…
		else
			error "there is no 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 in this window “Report”"
		end if
	end tell --  window "Report"
end tell -- "System Events"…

and return the entire log history.

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

Thank you.

The static text doesn’t obey to the instruction “set value”
Must try to use paste or keystroke.

Download Cliclick from www.bluem.net
and install it as : “/usr/local/bin/cliclick”
By default, the folder “Macintosh HD:usr:” is invisible.
but you may open it with :

set p2usr to POSIX file "/usr/"
tell application "Finder" to open p2usr

If I remember well, a subfolder named “local” exists
but you will have to create its subfolder “bin” to get the hierarchy:
“Macintosh HR:usr:local:bin:” where you will store “cliclick”.

-- version 1

property useClipboard : true -- try both settings
-- true --> use the clipboard
-- false --> doesn't use the clipboard

set theExtension to "applescript"
if useClipboard then set the clipboard to theExtension
tell application "System Events" to tell process "Salary"
	set frontmost to true
	tell window "Report"
		tell 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
			-- class of UI elements --> {static text}
			set {x, y} to position of static text 1
			-- 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
			if useClipboard then
				keystroke "v" using {command down}
				keystroke return
			else
				keystroke theExtension & return
			end if
			delay 0.2
			class of UI elements --> {static element} + maybe a 2nd element
			properties of text items
		end tell
	end tell
end tell
-- version 2

property useClipboard : true -- try both settings
-- true --> use the clipboard
-- false --> doesn't use the clipboard

set theExtension to "applescript"
if useClipboard then set the clipboard to theExtension
set theExtension to "applescript"
tell application "System Events" to tell process "Salary"
	set frontmost to true
	tell window "Report"
		tell 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
			-- class of UI elements --> {static text}
			set {x, y} to position of static text 1
		end tell
		tell me to do shell script "/usr/local/bin/cliclick c:" & x & "," & y -- third party CLI to click upon the static text
		if useClipboard then
			keystroke "v" using {command down}
			keystroke return
		else
			keystroke theExtension & return
		end if
		delay 0.2
		class of UI elements --> {static element} + maybe a 2nd element
		properties of text items
		
	end tell
end tell

I will be back tomorrow.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 7 mai 2020 22:07:37