Paste text to last active window

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

Thank you for your time today!

Unfortunately for both versions:

Full History Log:

Hi again. I was at work. So, you found static text in the entire contents of floating window. But as remember, one static text is like label, so its value doesn’t change. Should be some text field or button, which corresponds to this static text.
2) you sad me that when you close other application’s window, you see at the top of the screen the name of floating window. That is, this window is already focused (selected). You should try to select not the floating window, but text field or button corresponding to your static text. And, the click, showed by Yvan Koenig in his last script, maybe double-click depending on your application’s realization.

NOTE: if selecting text field (or, button) works (select text field…blabla, or select button…blabla) or setting text field’s or button’s property focused to true, works, then you can click it without cliClick.

Hello sir and thank you for your reply.

No worries, I was at work as well.

I will see that again later today, now it is too late and I will go to sleep.

It become odd.

In your message #32 we may read:

and in message #34 we read:

Of course, I guess that the name written at the very end : “Report k” is just a typo but it’s puzzling to read that the object which existed (with an embedded text field) in first case doesn’t exist in second case.

What changed between these two attempts ?

Is it a way to get a copy of the application ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 8 mai 2020 10:31:46

Hey,

I do not know man! It drives me crazy!

I tried it again just now and both versions populate the same error:

(the extra k had been typed by accident when I pasted the log here and was editing it in quotes)

The Script Editor highlights though the word “position” of

set {x, y} to position of static text 1

I guess because the text area is not focused?

My apologies, unable to share as this is company’s internal software with a lot of financial confidential data.

Are you sure that the window has the same contents during every tests ?

My understanding is that in some conditions which I can’t guess - but you are supposed to know them - the window contains the wanted UI element and that in other conditions it doesn’t.

Your message #30 was clear enough:

The “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 1 of process “Salary”” was available and it contained a static text whose value was “Optional”
In other attempts such object wasn’t available.
I wonder how you were able to ran the script without seeing such difference.

I apologize, it’s your duty to take care of what is available in your window before trying to execute a given task.

So, please look carefully at what is available in your window before trying to run the script.
If the wanted text area it’s useless to try to edit its contents.

Here is a slightly modified release of the script version 1.
If the window doesn’t match the wanted item, it will not issue an error message to warn you, it will use a display dialog"

-- 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"
		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 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}
				value of static text 1
				set {x, y} to position of static text 1
				-- you may try to use : click static text 1 -- 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
		else
			display dialog "No need to continue, the window “Report” doesn't contain the wanted text area" buttons {"Oops"}
		end if
	end tell
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 8 mai 2020 17:22:41

Here is an expanded version which is supposed to return in the log history complementary informations which, I hope, will tell me what you failed to see.


set theExtension to "applescript"
tell application "System Events" to tell process "Salary"
	set frontmost to true
	
	name of every window
	subrole of every window
	
	tell (first window whose subrole is "AXFloatingWindow")
		get its properties
	end tell
	
	tell window "Report"
		set itsName to its name
		get its properties -- ADDED to get infos upon the window
		class of UI elements --> {group, ???, ???}
		tell group 1
			class of UI elements --> {group, ???, ???}
			tell group 1
				class of UI elements --> {scroll area, ???, ???}
				tell scroll area 1
					class of UI elements --> {UI element, ???, ???}
					tell UI element 1
						class of UI elements --> {group, group, group, group, group, ???, ???}
						try
							log "group 1"
							tell group 1
								class of UI elements --> {group, ???, ???}
								tell group 1
									class of UI elements --> {text area, ???, ???}
									tell text area 1
										class of UI elements --> {group, group, ???, ???}
										tell group 1
											class of UI elements --> {???, ???}
										end tell -- group 1
										tell group 2
											class of UI elements --> {static text, ???, ???}
											tell static text 1
												its properties --> 
											end tell -- static text 1
										end tell -- group 2
									end tell -- text area 1
								end tell -- group 1
							end tell -- group 1
						end try
						try
							log "group 2"
							tell group 2
								class of UI elements --> {group, ???, ???}
								tell group 1
									class of UI elements --> {text area, ???, ???}
									tell text area 1
										class of UI elements --> {group, group, ???, ???}
										tell group 1
											class of UI elements --> {???, ???}
										end tell -- group 1
										tell group 2
											class of UI elements --> {static text, ???, ???}
											tell static text 1
												its properties --> 
											end tell -- static text 1
										end tell -- group 2
									end tell -- text area 1
								end tell -- group 1
							end tell -- group 2
						end try
						try
							log "group 3"
							tell group 3
								class of UI elements --> {group, ???, ???}
								tell group 1
									class of UI elements --> {text area, ???, ???}
									tell text area 1
										class of UI elements --> {group, group, ???, ???}
										tell group 1
											class of UI elements --> {???, ???}
										end tell -- group 1
										tell group 2
											class of UI elements --> {static text, ???, ???}
											tell static text 1
												its properties --> 
											end tell -- static text 1
										end tell -- group 2
									end tell -- text area 1
								end tell -- group 1
							end tell -- group 3
						end try
						try
							log "group 4"
							tell group 4
								class of UI elements --> {group, ???, ???}
								tell group 1
									class of UI elements --> {text area, ???, ???}
									tell text area 1
										class of UI elements --> {group, group, ???, ???}
										tell group 1
											class of UI elements --> {???, ???}
										end tell -- group 1
										tell group 2
											class of UI elements --> {static text, ???, ???}
											tell static text 1
												its properties --> 
											end tell -- static text 1
										end tell -- group 2
									end tell -- text area 1
								end tell -- group 1
							end tell -- group 4
						end try
						try
							log "group 5"
							tell group 5
								class of UI elements --> {group, ???, ???}
								tell group 1
									class of UI elements --> {text area, ???, ???}
									tell text area 1
										class of UI elements --> {group, group, ???, ???}
										tell group 1
											class of UI elements --> {???, ???}
										end tell -- group 1
										tell group 2
											class of UI elements --> {static text, ???, ???}
											tell static text 1
												its properties --> 
											end tell -- static text 1
										end tell -- group 2
									end tell -- text area 1
								end tell -- group 1
							end tell -- group 5
						end try
					end tell -- UI element 1
				end tell -- scroll area 1
			end tell -- group 1
		end tell -- group 1
		
		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 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
				set oldValue to value of static text 1
			end tell -- 
		else
			display dialog "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 & "”" buttons {"Oops"}
		end if
	end tell -- window 1
end tell -- System Events"

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 8 mai 2020 17:54:00

Good morning and thank you for your dedication!

So here is the full history log: