Saturday, July 4, 2020

#1 2020-05-03 05:54:33 pm

epaminos
Member
Registered: 2019-10-18
Posts: 57

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:

Applescript:

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:

Applescript:


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.

Last edited by epaminos (2020-05-03 05:58:12 pm)

Offline

 

#2 2020-05-06 05:40:49 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Paste text to last active window

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

Applescript:


-- 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.

Last edited by KniazidisR (2020-05-06 05:48:54 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#3 2020-05-06 07:25:29 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: Paste text to last active 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.

Applescript:

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

Offline

 

#4 2020-05-06 07:45:11 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 530

Re: Paste text to last active window

epaminos wrote:

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



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.

Applescript:

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).

In the apps with multiple windows it is not pasted to the correct window where the script was recalled from.

Last edited by peavine (2020-05-06 07:54:19 am)


2018 Mac mini - macOS Catalina

Offline

 

#5 2020-05-06 08:00:50 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: Paste text to last active window

peavine wrote:

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 of TextEdit, Numbers, Script Editor, and Soulver.

Applescript:

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).

In the apps with multiple windows it is not pasted to the correct window where the script was recalled from.



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

Offline

 

#6 2020-05-06 04:41:30 pm

epaminos
Member
Registered: 2019-10-18
Posts: 57

Re: Paste text to last active window

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.

Applescript:


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.

Offline

 

#7 2020-05-06 06:37:10 pm

epaminos
Member
Registered: 2019-10-18
Posts: 57

Re: Paste text to last active window

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.

Offline

 

#8 2020-05-06 09:36:12 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Paste text to last active window

@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

Last edited by KniazidisR (2020-05-06 09:37:56 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#9 2020-05-06 09:49:37 pm

epaminos
Member
Registered: 2019-10-18
Posts: 57

Re: 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.

Offline

 

#10 2020-05-06 10:06:14 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Paste text to last active window

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

Last edited by KniazidisR (2020-05-06 10:11:38 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#11 2020-05-06 10:11:32 pm

epaminos
Member
Registered: 2019-10-18
Posts: 57

Re: Paste text to last active window

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.

Offline

 

#12 2020-05-06 10:16:40 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Paste text to last active window

@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.

Last edited by KniazidisR (2020-05-06 10:28:29 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#13 2020-05-06 10:29:50 pm

epaminos
Member
Registered: 2019-10-18
Posts: 57

Re: Paste text to last active window

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.

Offline

 

#14 2020-05-06 10:32:38 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Paste text to last active window

epaminos wrote:

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!


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?

epaminos wrote:

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.



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

Last edited by KniazidisR (2020-05-06 10:39:35 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#15 2020-05-06 10:38:30 pm

epaminos
Member
Registered: 2019-10-18
Posts: 57

Re: Paste text to last active window

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!

Last edited by epaminos (2020-05-06 10:41:14 pm)

Offline

 

#16 2020-05-06 10:41:00 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: 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 (not minimize!!!) other application's window, 4) what application's name you see on the top of screen?

Last edited by KniazidisR (2020-05-06 10:44:02 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#17 2020-05-06 10:45:26 pm

epaminos
Member
Registered: 2019-10-18
Posts: 57

Re: Paste text to last active window

Oh sorry.

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

Offline

 

#18 2020-05-06 10:51:05 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Paste text to last active window

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

Last edited by KniazidisR (2020-05-06 11:19:24 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#19 2020-05-06 11:14:39 pm

epaminos
Member
Registered: 2019-10-18
Posts: 57

Re: Paste text to last active window

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?


Applescript:


...
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
...

Last edited by epaminos (2020-05-06 11:16:13 pm)

Offline

 

#20 2020-05-06 11:19:41 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Paste text to last active window

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

Applescript:


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

Last edited by KniazidisR (2020-05-06 11:22:07 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#21 2020-05-06 11:32:33 pm

epaminos
Member
Registered: 2019-10-18
Posts: 57

Re: Paste text to last active window

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.

Last edited by epaminos (2020-05-06 11:33:09 pm)

Offline

 

#22 2020-05-07 04:21:08 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: 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 ?

Applescript:

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

Offline

 

#23 2020-05-07 04:53:59 am

epaminos
Member
Registered: 2019-10-18
Posts: 57

Re: Paste text to last active window

So, by using this:

Applescript:


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:

static text "Optional" 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"




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

Applescript:

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

Last edited by epaminos (2020-05-07 04:56:07 am)

Offline

 

#24 2020-05-07 05:22:44 am

epaminos
Member
Registered: 2019-10-18
Posts: 57

Re: Paste text to last active window

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:

Applescript:

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

Offline

 

#25 2020-05-07 08:02:32 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: Paste text to last active window

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:

Applescript:

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 :

Applescript:

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 :

Applescript:

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

Last edited by Yvan Koenig (2020-05-07 08:31:39 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)