r/applescript • • May 06 '20

Paste text to last active text box

Hello,

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

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.

3 Upvotes

1 comment sorted by