Jump to content

Accessing selected text in the active window


Recommended Posts

I want to access the selected text in the currently active window.

$var = ControlCommand(WinGetTitle(""), "", ControlGetFocus(WinGetTitle("")), "GetSelected", "")oÝ÷ Û
+Çâçè­Ê'¶º%²ØZµªÞyØ­rí®lnëbíz¹Þ´LiÞ­ú+{¦¦W¨­æv·¬±¶«Æ§yØ­rí®Sån z+lzWâ²z-jëh×6send("^c", 0)
$var = ClipGet()

works pretty much everywhere, except that it overwrites the Clipboard, which is not so user-friendly.

Is there a third way, or is there a way to save and restore any type of Clipboard contents? As far as I know, ClipGet() and ClipPut() will only save and restore plain text.

Best regards

Nick

Link to comment
Share on other sites

...but in Internet Explorer, for example, only the address bar is an edit control. The Web page itself is not...

See _IEAction in the help file - specifically, the "selectall" and "copy" parms Edited by herewasplato

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

Link to comment
Share on other sites

  • Moderators

Thanks, but this still consumes the Clipboard. I guess there is no way to avoid this.

Regards

Nick

Are you just trying to get the source or something specific?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Are you just trying to get the source or something specific?

What I want to do is use a hotkey to capture the selected text in whatever program is activated at the time. Ideally I would like to avoid using the clipboard to do this, as it would overwrite anything that the user thought they had placed there. So I'm looking for either a way to save and restore the clipboard (allowing send(^c) to be used), or a different way to access whatever is highlighted in the active window, which could be any program.

Thanks for your help

Nick

Link to comment
Share on other sites

  • Moderators

And this doesn't work?

Global $sOldClip, $sNewClip
HotKeySet('{PRINTSCREEN}', '_COPY')
HotKeySet('{ESC}', '_EXITNOW')
While 1
    Sleep(1000000)
WEnd

Func _COPY()
    $sOldClip = ClipGet()
    ControlSend(WinGetHandle(''), '', '', '^c')
    $sNewClip = ClipGet()
    ;Now replace clipboard
    ClipPut($sOldClip)
    MsgBox(64, 'Info', 'Old Clip Stuff:' & @CRLF & $sOldClip & @CRLF & 'New Clip Stuff:' & @CRLF & $sNewClip)
EndFunc

Func _EXITNOW()
    Exit
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

It works with text, not images :P

Well ClipPut() won't work with pics :whistle:

I made a work around for this once before I thought.... Maybe it was just to detect if it was a pic... can't remember.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Well can't say :P I've got the same problem :D And I haven't figured a way around :whistle:

The only thing I can think of at the moment (Not delving into the memory locations and personally not knowing how to store the information), is if it's a picture, you can return before doing the copy procedure and not do anything.
Global $sOldClip, $sNewClip
HotKeySet('{PRINTSCREEN}', '_COPY')
HotKeySet('{ESC}', '_EXITNOW')
While 1
    Sleep(1000000)
WEnd

Func _COPY()
    $sOldClip = _ClipGetEx()
    If @error Then Return SetError(1, 0, 0)
    ControlSend(WinGetHandle(''), '', '', '^c')
    $sNewClip = ClipGet()
    ;Now replace clipboard
    ClipPut($sOldClip)
    MsgBox(64, 'Info', 'Old Clip Stuff:' & @CRLF & $sOldClip & @CRLF & 'New Clip Stuff:' & @CRLF & $sNewClip)
EndFunc

Func _EXITNOW()
    Exit
EndFunc

Func _ClipGetEx()
    Local $vGetClip = ClipGet()
    If Not @error Then Return $vGetClip
    Local $aClipGetEx = DllCall('user32.dll', 'int', 'CountClipboardFormats')
    If @error = 0 And $aClipGetEx[0] > 4 Then Return SetError(1, 0, 0)
    Return SetError(2, 0, 0)
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Not with images and files :whistle:

Well, the cold hard facts... is that's never been an option. But if you run my last example, you'll see that it won't do anything if it's anything other than text.

Edit:

Might see what you can come up with here: http://msdn2.microsoft.com/en-us/library/ms649051.aspx

Edit2:

_ClipPutFile() is an option for the file issue.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...