Sign in to follow this  
Followers 0
Nevercom

Getting Selected Text from active window

13 posts in this topic

Hello everybody,

I'm new in here and actually don't know much of AutoIt.

I have made a Dictionary using Multimedia Builder (MMB), I wanted to add an ability to my Dictionary to get the selected (Highlighted) Text from Active window when a Key Combination is pressed, It couldn't be done using MMB itself.

A friend of mine advised me to take a look at AutoIt, but i really don't know how to use it :P

So, would you guys help me about this ? Is it possible to make an application with AutoIt to copy the highlighted text from active window to ClipBoard ? (Just like Babylon)

I have searched the AutoIt Help file and found some Functions Like: _GUICtrlEdit_GetText, but don't have any idea how to use it and beside it, I need to get highlighted text from any control of any window, anything user has selected e.g. Icon Caption in Windows Desktop...

Thanks in Advance,

Nevercom


[left] [/left]

Share this post


Link to post
Share on other sites



Activate the window and than use Send('^c')...

If you want to be a bit more professional use a ClipGet() right before and then set the original clipboard data back with ClipPut as soon as you're done getting the text from the window.


My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites

I Think i should be more detailed,

well, My dictionary is running and is minimized to Tray, User is for example reading an article on the internet using Firefox and needs to translate a word, he/she selects the word and presses Specified Key Combination e.g. Ctrl+T, then my Dictionary will run the AutoIt compiled application, the AutoIt app should look up the fireFox window and detect the highlighted text and then copy it to the clipboard, then my dictionary will search its database for the word and...

The matter is how to make an app to send the Highlighted text in any window to the clipboard (I'm not sure sending Ctrl+C will work for all windows)


[left] [/left]

Share this post


Link to post
Share on other sites

I Think i should be more detailed,

well, My dictionary is running and is minimized to Tray, User is for example reading an article on the internet using Firefox and needs to translate a word, he/she selects the word and presses Specified Key Combination e.g. Ctrl+T, then my Dictionary will run the AutoIt compiled application, the AutoIt app should look up the fireFox window and detect the highlighted text and then copy it to the clipboard, then my dictionary will search its database for the word and...

The matter is how to make an app to send the Highlighted text in any window to the clipboard (I'm not sure sending Ctrl+C will work for all windows)

Hmmm, I think Ctrl C works for most windows... at least any windows that let you actually select text.. Maybe not games. But since almost all text input/output allows you to select, I think Ctrl C will work for most anything. It would be easy to test. Just use a Send('^c') right after you enter you're Ctrl^T function...

I don't know if there would be another way to do this though...


My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites

Thanks Achilles,

And another question, Can we send CTRL+C Key Stroke to Windows Desktop ? I mean can we act Win desktop just like any other window ? Does it have its own handle and will it accept keystrokes ?

And..., What would final code like in AutoIt ? :P (When run=>Send CTRL+C to active Window, e.g. Win Desktop, then close)

Thanks in Advance,

Nevercom


[left] [/left]

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thanks Achilles,

And another question, Can we send CTRL+C Key Stroke to Windows Desktop ? I mean can we act Win desktop just like any other window ? Does it have its own handle and will it accept keystrokes ?

And..., What would final code like in AutoIt ? :P (When run=>Send CTRL+C to active Window, e.g. Win Desktop, then close)

Thanks in Advance,

Nevercom

This should work on the desktop just like any other window.

The code will kind of look like this (untested):

HotKeySet('^t', '_Translate')

While 1 
Sleep(200)
WEnd

Func _Translate()
$oldClip = ClipGet(); get the data that is currently in the clipboard
Send('^c'); copy the selected text to the clipboard
$phrase = ClipGet(); get the data from the clipboard (which is now the selected word)
ClipPut($oldClip); put the old data back in the clipboard

; now phrase is the word or phrase you want to translate
EndFunc

EDIT: Welcome to the AutoIt forums! I didn't see that you were new here until just now..

Edited by Achilles

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites

This should work on the desktop just like any other window.

The code will kind of look like this (untested):

HotKeySet('^t', '_Translate')

While 1 
Sleep(200)
WEnd

Func _Translate()
$oldClip = ClipGet(); get the data that is currently in the clipboard
Send('^c'); copy the selected text to the clipboard
$phrase = ClipGet(); get the data from the clipboard (which is now the selected word)
ClipPut($oldClip); put the old data back in the clipboard

; now phrase is the word or phrase you want to translate
EndFunc

EDIT: Welcome to the AutoIt forums! I didn't see that you were new here until just now..

Thanks Achilles, you helped me a lot !

I have tested your Code and it worked just great, really thanks.

But, I don't want CTRL+C hotkey to be registered for AutoIt app, because in this case AutoIt app needs to be running and its Unnecessary use of System Resources.

Otherwise, i would register CTRL+T for my Dic to run AutoIt App with or without Parameters (If needed ?)

But how to change this code to:

1. If it has "GetText" Parameter when running, do the following steps otherwise, Close.

2. Send CTRL+C to the Window [send('^c')]

3. Get the Data from Clipboard [$phrase = ClipGet()]

4. when it gets the data from clipboard (Step 3), then close.

and i need Tray Icon NOT to be shown (I think i must put #NoTrayIcon declare or something like this at the beginning of the Code ?)

and a question, What is [$phrase = ClipGet()] for ? isn't it enough just to send CTRL+C ? i think when this keystroke is sent, the selected word is copied to the clipboard, this part of the code means to me like this: Put the Clipboard data into $phrase variable(String); but this variable is not used anywhere in the code after that, am i right ?

Thanks in Advance,

Nevercom


[left] [/left]

Share this post


Link to post
Share on other sites

Thanks Achilles, you helped me a lot !

I have tested your Code and it worked just great, really thanks.

But, I don't want CTRL+C hotkey to be registered for AutoIt app, because in this case AutoIt app needs to be running and its Unnecessary use of System Resources.

Otherwise, i would register CTRL+T for my Dic to run AutoIt App with or without Parameters (If needed ?)

But how to change this code to:

1. If it has "GetText" Parameter when running, do the following steps otherwise, Close.

2. Send CTRL+C to the Window [send('^c')]

3. Get the Data from Clipboard [$phrase = ClipGet()]

4. when it gets the data from clipboard (Step 3), then close.

and i need Tray Icon NOT to be shown (I think i must put #NoTrayIcon declare or something like this at the beginning of the Code ?)

and a question, What is [$phrase = ClipGet()] for ? isn't it enough just to send CTRL+C ? i think when this keystroke is sent, the selected word is copied to the clipboard, this part of the code means to me like this: Put the Clipboard data into $phrase variable(String); but this variable is not used anywhere in the code after that, am i right ?

Thanks in Advance,

Nevercom

I think you might be misunderstanding what the function Send('^c') does. It doesn't set that hotkey to autoit or anything similar, it just presses Ctrl + C, like if you pushed Ctrl c on you computer right now.

I'm also not sure if you understand how simple this can be. using the code I gave you above, you can just go ahead and translate the word and then display it in a messagebox or do what you want. That's all you need.

You're right, just add a #noTrayIcon at the top of your script to get rid of the tray icon. The line "$phrase = ClipGet()" actually gets the text from the clipboard. Using just ctrl + c just puts the data in the clipboard. But what you actually want is the actually string, so you use ClipGet() to get that.

The reason that $phrase is not used anywhere is because I left that for you to translate. For example, in my code say someone pushes Ctrl + T when they have "AutoIt" selected. It will run through the code I gave you under the _Translate() function and then $phrase will be equal to AutoIt. Then you can display that Msgbox(0, 'Selected text', $phrase) or translate it.

I hope that makes more sense.. let me know if I'm not understanding what you're saying...


My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites

Thanks Achilles,

I know that Send('^c') function simulates pressing of CTRL+C, and pressing CTRL+C will copy the selected text to the clipboard, right ?

And what i want is just to selected text be copied to the clipboard, and then i have nothing to do with AutoIt App. so i don't need $phrase, because i'm not gonna use it in AutoIt (I Can get the clipboard Data straight from MMB, but i can not send keystrokes to the window by MMB itself).

and as i said there is no need to register CTRL+T hotkey for AutoIt App.

but my question is, how to make the app to work only when it is run with Specific Parameter ? e.g "Run App.exe GetText" will do the job, but if it is run with no parameter(or wrong Parameter), then it must be closed.

and after copying Data to the clipBoard is finished, the AutoIt App must be closed.


[left] [/left]

Share this post


Link to post
Share on other sites

I was quite interested in this topic. Some searching brought me to many possible solutions including OCR, DrawText hooking, IAccessible and more, even some few grands cost ActiveX libraries etc..

I think you may want to read this, it's quite sufficient for most applications that support this interface. I think, making this function and calling it from a DLL may be nice add-on for your dictionary tool.

And finally inspired by the simplicity of Achilles's suggestion I've made this simple thing that in most case (except for links, files, etc..) will not require word selection:

#include <Misc.au3>

HotKeySet('{ESC}', '_EXIT')
Global $hDLL = DllOpen('user32.dll')


While 1
    If _IsPressed('12', $hDLL) Then ;ALT
        If _IsPressed('02', $hDLL) Then ;Right mouse button
            _GetText()
        EndIf
    EndIf
    Sleep(50)
WEnd


Func _EXIT()
    DllClose($hDLL)
    Exit
EndFunc


Func _GetText()
    
    Local $PrevClip = ClipGet()
    Local $MPos = MouseGetPos()
    
    MouseClick('left', $MPos[0], $MPos[1], 2, 0)
    Send('^c')
    ConsoleWrite(ClipGet() & @LF)
    ClipPut($PrevClip)
    
EndFunc

Inside the _GetText function you can use StringRegExpReplace to remove and non-word character, then pass it to the parser function...

Share this post


Link to post
Share on other sites

... text be copied to the clipboard, and then i have nothing to do with AutoIt App. ...

So, compile these two lines and test it.

#NoTrayIcon
Send('^c')
The AutoIt script will copy the selected text to the OS clipboard and then will exit.

If you find an app that ctrl-c does not work on then you are moving into a much more complicated realm.


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Thank you guys.

Authenticity, Your CODE was really useful, Specially the link you had suggested, thanks.

I Hope i could learn this really lovely and easy to use AutoIt language ASAP.

Thanks in Advance,

Nevercom


[left] [/left]

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Hello Everyone,

I've mixed Authenticity's CODE with some codes that a friend has made with AutoIt to work with MMB

#NoTrayIcon
#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Add_Constants=n
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.12.1
 Author: Nevercom

 Script Function:
    This will copy the text under Mouse Position to the Clip Board and will
    run such a script in MMB:
    Data$ = 'Selected Text'; Selected text is value of $Data in AutoIt
    RunScript("GetData")

#ce ----------------------------------------------------------------------------

#include <MMBLib.au3>

if @Compiled AND $CmdLine[0] == 0 then
    MsgBox(16,"Error","Missing CMDLine parameters!")
    Exit
endif

if @Compiled then
    $title = $CmdLine[1]; Get window title from CMD
Else
;$title = "Welcome!"; only for debugging
endif
;$title = 'Welcome!'; Title of MMB window
;$script = 'Message("Test","")'; MMB Script
;SendScriptMMB($title,$script)
Func _GetText()
   
    Local $PrevClip = ClipGet()
    Local $MPos = MouseGetPos()
   
    MouseClick('left', $MPos[0], $MPos[1], 2, 0)
    Send('^c')
  ;ConsoleWrite(ClipGet() & @LF)
    $Data = ClipGet()
    
    $script = 'Data$ = ' & $Data
    SendScriptMMB($title,$script)
    $script = 'RunScript("GetData")'
    SendScriptMMB($title,$script)
    ClipPut($PrevClip)
   
EndFunc

while 1
    if $CmdLine[2] == 'GetText' Then; I will send parameters to AutoIt like this: MMBWindowTitle GetText
        _GetText()
    EndIf
    Sleep(100)
WEnd

My Problem is how to mix AutoIt variables to work with MMB Style variables,

$Data = ClipGet()
$script = 'Data$ = ' & $Data
SendScriptMMB($title,$script)
$script = 'RunScript("GetData")'
SendScriptMMB($title,$script)

if the text is copied to the clipboard equals to "ClipContent"

I want to send this code to MMB:

Data$ = 'ClipContent'
RunScript("GetData")

but i dont know how to make final code is sent to MMB look like this, i also tried:

$script = 'Data$ = ' & $Data @CRLF & 'RunScript("GetData")'
SendScriptMMB($title,$script)

but when the code is sent to MMB, it causes some errors and will not work.

BTW, SendScriptMMB($title,$script) is a function that send a Script code to MMB App, Using MMBLib Library (I've Attached it).

My AU3 Code: Final_Func.au3

MMBLib Library: MMBLib.rar

Edited by Nevercom

[left] [/left]

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0