IlanMS Posted July 18, 2021 Share Posted July 18, 2021 (edited) General purpose: The selected window has selected text. The script will process the text, and then replace the selected text with the processed text. Current purpose: Get the selected text from the selected window. I found 3 answers in the forum, each with it's problems:Solution A: Basically copy to clipboard. Problems: does not work in all cases (Ctrl-C does not allways work. OP managed to improve this but not to 100% success). Also goes through the clipboard instead of directly read the text into a string variable.Solution B: Use AutoIt built-in ControlCommand. Problem: Requires to know the control in which the selected test is. I couldn't find how to do this.Solution C : Use some VB code found in http://support.microsoft.com/kb/163434. Problems: I don't know how to incorporate VB code and how take the variable oText from the VB sub to my script: So now I have to do 1. decide which solution to use. 2. Understand how to use it. Here follows the solutions: A: Send ("{CTRLDOWN}") ;Try various methods until we get text on the clipboard Switch $rand Case 1 Send("{INS}") Case 2 ControlSend($window_title, "", "", "{INS}") Case 3 Send("c") Case 4 ControlSend($window_title, "", "", "c^") EndSwitch B: (I know how to get hWnd): <ControlCommand ( "hWnd", "My string variable?", Have no idea how to get this, "GetSelected", "My string variable here?"> C : Sub GetSelectedText() On Error Resume Next Err.Clear Dim oText As TextRange ' Get an object reference to the selected text range. Set oText = Activewindow.Selection.TextRange ' Check to see whether error occurred when getting text object ' reference. If Err.Number <> 0 Then MsgBox "Invalid Selection. Please highlight some text " _ & "or select a text frame and run the macro again.", _ vbExclamation End End If ' Display the selected text in a message box. If oText.Text = "" Then MsgBox "No Text Selected.", vbInformation Else MsgBox oText.Text, vbInformation End If End Sub Which solution to use, and how? Edited July 18, 2021 by IlanMS correction Link to comment Share on other sites More sharing options...
water Posted July 18, 2021 Share Posted July 18, 2021 First problem: The MS site you linked to does not exist (returned 404) My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Nine Posted July 18, 2021 Share Posted July 18, 2021 Second problem, you did not specify the source of the selection. (Web page ? Word document ? 3rd party Application ? Else ? All of the above ?) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
IlanMS Posted July 18, 2021 Author Share Posted July 18, 2021 5 minutes ago, Nine said: Second problem, you did not specify the source of the selection. (Web page ? Word document ? 3rd party Application ? Else ? All of the above ?) Any source. Could be MS Word, form in browser, text in url line, notepad, or anything else. Link to comment Share on other sites More sharing options...
IlanMS Posted July 18, 2021 Author Share Posted July 18, 2021 16 minutes ago, water said: First problem: The MS site you linked to does not exist (returned 404) Can't help with that if MS removed it. However the code is in my message (option C). Link to comment Share on other sites More sharing options...
Nine Posted July 18, 2021 Share Posted July 18, 2021 Are all those sources in an available window (non-hidden, non-minimized) ? If available, is it an ACTIVE window ? Can you create a replicate of the problem ? Your AutoIt code is not really optimal BTW. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
IlanMS Posted July 18, 2021 Author Share Posted July 18, 2021 12 minutes ago, Nine said: Are all those sources in an available window (non-hidden, non-minimized) ? If available, is it an ACTIVE window ? Can you create a replicate of the problem ? Your AutoIt code is not really optimal BTW. Always active window (can get its handle with $hWnd = WinGetHandle("[active]")) I can't help with "not optimal". I am a noob. I do my best to get answers by searching the forum, but naturally I can't understand most of the scripts and I try to treat them as encapsulated black boxes. Link to comment Share on other sites More sharing options...
water Posted July 18, 2021 Share Posted July 18, 2021 Code in option C is MS application related (Word, PowerPoint ...). Did you search the forum for your problem? IIRC this has been discussed before. Can't remember if there was a solution or just a "can't be done" 🤔 My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
IlanMS Posted July 18, 2021 Author Share Posted July 18, 2021 (edited) 9 minutes ago, water said: Code in option C is MS application related (Word, PowerPoint ...). Did you search the forum for your problem? IIRC this has been discussed before. Can't remember if there was a solution or just a "can't be done" 🤔 I did search (wrote so in this topic opening message) and put here everything I thought relevant. At least option A is viable but not a total solution. As for B I honestly don't know (hoped to get answers here), and your answer here renders option C as useless. At least we narrowed it down to two options, A which is less than a total solution (but I will use if I can't find anything better) and B which I have no idea how to use. Edited July 18, 2021 by IlanMS Link to comment Share on other sites More sharing options...
Nine Posted July 18, 2021 Share Posted July 18, 2021 (edited) Try this : #include <Constants.au3> Sleep(5000) ; SELECT THE WINDOW YOU WANT TO COPY THE SELECTED TEXT FROM ClipPut("") Local $sTitle = WinGetTitle("[ACTIVE]") If Not $sTitle Then Exit MsgBox($MB_SYSTEMMODAL, "", "No Window Selected") ConsoleWrite($sTitle & @CRLF) Send("^c") Local $sText = ClipGet() If Not $sText Then Exit MsgBox($MB_SYSTEMMODAL, "", "No text selected") ConsoleWrite($sText & @CRLF) If you find a situation where there is selected text and it is not working then tell us what is the source application ... Edited July 18, 2021 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
IlanMS Posted July 18, 2021 Author Share Posted July 18, 2021 6 minutes ago, Nine said: Try this : #include <Constants.au3> Sleep(5000) ; SELECT THE WINDOW YOU WANT TO COPY THE SELECTED TEXT FROM ClipPut("") Local $sTitle = WinGetTitle("[ACTIVE]") If Not $sTitle Then Exit MsgBox($MB_SYSTEMMODAL, "", "No Window Selected") ConsoleWrite($sTitle & @CRLF) Send("^c") Local $sText = ClipGet() If Not $sText Then Exit MsgBox($MB_SYSTEMMODAL, "", "No text selected") ConsoleWrite($sText & @CRLF) If you find a situation where there is selected text and it is not working then tell us what is the source application ... What should be the content of Constants.au3? Link to comment Share on other sites More sharing options...
Nine Posted July 18, 2021 Share Posted July 18, 2021 Just use the code as is. It is a package installed component. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
water Posted July 18, 2021 Share Posted July 18, 2021 BTW: When you want to answer, please do not quote the post. Simply add your reply at the end of the thread. As we know what we have posted quotes are rarely needed. They just clutter the thread and make it hard to read. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
IlanMS Posted July 19, 2021 Author Share Posted July 19, 2021 (edited) Thanks Water. Will do. I just can't find simple "reply" button... do you get notification to all threads once you reply to them? Edited July 19, 2021 by IlanMS Added question Link to comment Share on other sites More sharing options...
Nine Posted July 19, 2021 Share Posted July 19, 2021 Look at the very end of the thread there is a reply box with you avatar on the left. Just click the box... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
leuce Posted July 19, 2021 Share Posted July 19, 2021 (edited) 23 hours ago, IlanMS said: Any source. Could be MS Word, form in browser, text in url line, notepad, or anything else. I've had to do this many times in a variety of programs and windows, and I have discovered that some windows are simply resistant to copying. I have settled on just using the clipboard for this (you can try to save the existing clipboard to a variable and then restore it later). The keyboard shortcut for copying text isn't the same in all programs, but as you may know, Ctrl+C works 90% of the time. As for AutoIt's various Control functions, they only work in some windows. Java programs, for example, often have no "controls" that AutoIt can detect. Also, some programs require you to make the text pane active somehow, e.g. by clicking inside it. In some programs, if the user had selected text but then moved away from the window (so that it is no longer the active window) and returns to the window again (so that it is the active window again), pressing Ctrl+C will not manage to copy the selected text until you have put the focus back inside the pane where the text is selected. (And it's a bit of a catch-22, because as soon as you click inside that pane, the text gets deselected.) But sometimes one can get the focus back to such a pane by e.g. right-clicking in the pane (and cancelling the right-click menu), or by pressing Tab and then Shift+Tab again, or by some other functionality inside the program which, after you've used it, puts the focus back in the relevant pane. But as you can see, there is no universal method that works in all programs, as far as I know. My scripts that copy text are all customized for specific programs. Edited July 19, 2021 by leuce Link to comment Share on other sites More sharing options...
Solution IlanMS Posted July 22, 2021 Author Solution Share Posted July 22, 2021 I ended up using option A. Works fine. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now