Sign in to follow this  
Followers 0
usser

copy on select

22 posts in this topic

hello

is there a script that will copy text when it is selected?

thanks

Share this post


Link to post
Share on other sites



hello

is there a script that will copy text when it is selected?

thanks

Send("^c")

<_<


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Send("^c")

<_<

not so easy :)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

he means, wait until the text is selected and then automatically copy it.

I know I can't really think of a way. Autoit is an event driven language... I'm pretty sure you have to put a keypress or somehing in there. to that effect, here you go:

HotKeySet("{TAB}", "_copy")
HotKeySet("^+e", "_quit")
While(1)
    Sleep(100)
WEnd
Func _copy()
    Send("^c")
    $copied = ClipGet()
    TrayTip("Copied", '"' & $copied & '" Copied to Clipboard', 10, 1)
EndFunc
Func _quit()
    Exit
EndFunc

I don't know if this helps, but eh...

Edited by koresho

Share this post


Link to post
Share on other sites

Only works if you use the mouse to highlight:

#Include <Misc.au3>
#Include <Constants.au3>
Opt("GUIOnEventMode", 1)
Opt("TrayOnEventMode", 1)
Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 1)
Global $switch = "ON" 
$on = TrayCreateItem("On")
TrayItemSetOnEvent(-1, "turnon")
$off = TrayCreateItem("Off")
TrayItemSetOnEvent(-1, "turnoff")
$exit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "leave")
TrayItemSetState($on, $TRAY_DISABLE)
$lastclip = ClipGet()
While 1
    If _IsPressed(01) And $switch = "ON"  Then
        $lastclip = ClipGet()
        While _IsPressed(01)
            Sleep(10)
        WEnd
        Send("^c")
        $clip = ClipGet()
        If $clip = "" Then ClipPut($lastclip)
    EndIf

    Sleep(10)
WEnd
Func turnon()
    $switch = "ON" 
    TrayItemSetState($on, $TRAY_DISABLE)
    TrayItemSetState($off, $TRAY_ENABLE)
EndFunc   ;==>turnon
Func turnoff()
    $switch = "OFF" 
    TrayItemSetState($off, $TRAY_DISABLE)
    TrayItemSetState($on, $TRAY_ENABLE)
EndFunc   ;==>turnoff
Func leave()
    Exit
EndFunc   ;==>leave

Share this post


Link to post
Share on other sites

Well, that one kinda makes the Ctrl key stick sometimes, do this instead:

#Include <Misc.au3>
#Include <Constants.au3>
Opt("GUIOnEventMode", 1)
Opt("TrayOnEventMode", 1)
Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 1)
Global $switch = "ON" 
$on = TrayCreateItem("On")
TrayItemSetOnEvent(-1, "turnon")
$off = TrayCreateItem("Off")
TrayItemSetOnEvent(-1, "turnoff")
$exit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "leave")
TrayItemSetState($on, $TRAY_DISABLE)
$lastclip = ClipGet()
While 1
    If _IsPressed(01) And $switch = "ON"  Then
        $lastclip = ClipGet()
        While _IsPressed(01)
            Sleep(10)
        WEnd
        Send( "{CTRLDOWN}" )
        Sleep(10)
        Send("c")
        Sleep(10)
        Send( "{CTRLUP}" )
        $clip = ClipGet()
        If $clip = "" Then ClipPut($lastclip)
    EndIf
    Sleep(10)
WEnd
Func turnon()
    $switch = "ON" 
    TrayItemSetState($on, $TRAY_DISABLE)
    TrayItemSetState($off, $TRAY_ENABLE)
EndFunc   ;==>turnon
Func turnoff()
    $switch = "OFF" 
    TrayItemSetState($off, $TRAY_DISABLE)
    TrayItemSetState($on, $TRAY_ENABLE)
EndFunc   ;==>turnoff
Func leave()
    Exit
EndFunc   ;==>leave

Share this post


Link to post
Share on other sites

Ooooh very nice.

And I thought mine was alright <_<

Share this post


Link to post
Share on other sites

selecting text can happen with various ways:

double/triple etc click, ctrl+a, mouse dragging, etc

thus, if we make it "event-driven" the copying must be triggered with each of the above

isn't there a way to make a loop that will identify selected text (the highlighting that makes text appear in white in blue background) and automaticaly copy it in a variable (apart from OS clipboard ofcourse, it shouldn't replace the clipboard data)?

Share this post


Link to post
Share on other sites

selecting text can happen with various ways:

double/triple etc click, ctrl+a, mouse dragging, etc

thus, if we make it "event-driven" the copying must be triggered with each of the above

isn't there a way to make a loop that will identify selected text (the highlighting that makes text appear in white in blue background) and automaticaly copy it in a variable (apart from OS clipboard ofcourse, it shouldn't replace the clipboard data)?

I second that


A decision is a powerful thing

Share this post


Link to post
Share on other sites

Well, that one kinda makes the Ctrl key stick sometimes, do this instead:

#Include <Misc.au3>
#Include <Constants.au3>
Opt("GUIOnEventMode", 1)
Opt("TrayOnEventMode", 1)
Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 1)
Global $switch = "ON" 
$on = TrayCreateItem("On")
TrayItemSetOnEvent(-1, "turnon")
$off = TrayCreateItem("Off")
TrayItemSetOnEvent(-1, "turnoff")
$exit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "leave")
TrayItemSetState($on, $TRAY_DISABLE)
$lastclip = ClipGet()
While 1
    If _IsPressed(01) And $switch = "ON"  Then
        $lastclip = ClipGet()
        While _IsPressed(01)
            Sleep(10)
        WEnd
        Send( "{CTRLDOWN}" )
        Sleep(10)
        Send("c")
        Sleep(10)
        Send( "{CTRLUP}" )
        $clip = ClipGet()
        If $clip = "" Then ClipPut($lastclip)
    EndIf
    Sleep(10)
WEnd
Func turnon()
    $switch = "ON" 
    TrayItemSetState($on, $TRAY_DISABLE)
    TrayItemSetState($off, $TRAY_ENABLE)
EndFunc   ;==>turnon
Func turnoff()
    $switch = "OFF" 
    TrayItemSetState($off, $TRAY_DISABLE)
    TrayItemSetState($on, $TRAY_ENABLE)
EndFunc   ;==>turnoff
Func leave()
    Exit
EndFunc   ;==>leave
thanks, it seems to work exceptionaly well, I 'm impressed, since my previous attempts didn't work, but here are some concerns:

1)

I works very well with all possible ways to select text, apart from one:

While it captures the all the text when I right click and choose "Select all", it doesn't capture it when I hit ctrl+A. Isn't that strange? I mean, selecting all the text from the contextmenu doesn't send a ctrl+A?

2)

To normaly copy text/data you use ctrl+C, ctrl+C, or via the contextmenu. Then the normaly copied text/data is stored in the clipboard and last, it can be pasted with ctrl+V, or via the contextmenu.

I don't want the script to interfere at all in the above procedure.

I mean, what I copy with the normal way (ctrl+C, etc and not by selection) must stay in the clipboard and must be pasted with the normal way.

Copied-after-selection text must be stored in the script in a variable (not in the clipboard) and must be pasted with another key combination, eg ctrl+B

3)

The major problem with this script is when we select text without the intention to copy it, but with the intention to replace it (paste something over it) or delete it

For example, we often select text and then we click PASTE (while having the selected text), in order to replace the selected text with the clipboard content.

This action should not make the selected text to be stored in the script, since its a selection without the intention to copy the text, but to replace it.

I don't know if it's possible to make the script recognize these actions after the selection and not to store this text, or more correctly, to firstly store it as usual, but then delete it and keep the previously stored copied-after-selection text.

4)

Last, is it possible to store each copied-after-selection text is different slots? and if ctrl+B is to paste the copied-after-selection text, clicking ctrl+b multiple times (holding ctrl and clicking b multiple times actualy) should toggle slot 1, 2, 3, etc (to make it more clear, it clicking ctrl+B two times, should firstly paste the text of slot 1 and the second time it should replace the slot 1 pasted text, with slot 2 text). Maybe a GUI to navigate through slots would be convenient.

thanks

Share this post


Link to post
Share on other sites

My Question

The ClipPut($lastclip) doesn't work if the last clipboard item was an image. Does anyone know a workaround?

My Answers to Usser's Questions

thanks, it seems to work exceptionaly well, I 'm impressed, since my previous attempts didn't work, but here are some concerns:

1)

I works very well with all possible ways to select text, apart from one:

While it captures the all the text when I right click and choose "Select all", it doesn't capture it when I hit ctrl+A. Isn't that strange? I mean, selecting all the text from the contextmenu doesn't send a ctrl+A?

The code is saying that if it is "on" and left (mouse) click occurs and then left (mouse) click stops, then it copies. It doesn't work with Ctrl+A because all you're doing is selecting all the text. You can change it to detect that action too with various different methods. If you need help with that, I can help.

2)

To normaly copy text/data you use ctrl+C, ctrl+C, or via the contextmenu. Then the normaly copied text/data is stored in the clipboard and last, it can be pasted with ctrl+V, or via the contextmenu.

I don't want the script to interfere at all in the above procedure.

I mean, what I copy with the normal way (ctrl+C, etc and not by selection) must stay in the clipboard and must be pasted with the normal way.

Copied-after-selection text must be stored in the script in a variable (not in the clipboard) and must be pasted with another key combination, eg ctrl+B

Can you reword this? I've read it like four times and I'm still confused.

3)

The major problem with this script is when we select text without the intention to copy it, but with the intention to replace it (paste something over it) or delete it

For example, we often select text and then we click PASTE (while having the selected text), in order to replace the selected text with the clipboard content.

This action should not make the selected text to be stored in the script, since its a selection without the intention to copy the text, but to replace it.

I don't know if it's possible to make the script recognize these actions after the selection and not to store this text, or more correctly, to firstly store it as usual, but then delete it and keep the previously stored copied-after-selection text.

Why don't you just add a tray option or hotkey that pauses the copy-after-selection part?

4)

Last, is it possible to store each copied-after-selection text is different slots? and if ctrl+B is to paste the copied-after-selection text, clicking ctrl+b multiple times (holding ctrl and clicking b multiple times actualy) should toggle slot 1, 2, 3, etc (to make it more clear, it clicking ctrl+B two times, should firstly paste the text of slot 1 and the second time it should replace the slot 1 pasted text, with slot 2 text). Maybe a GUI to navigate through slots would be convenient.

Yeah, that's possible <_<. You could do a GUI to nav through the "slots," but that seems cumbersome to me. I'd just do keyboard actions, because it would take less time to develop and use. However, that's just me.

Let us know where you need help with all that.


A decision is a powerful thing

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

My Question

The ClipPut($lastclip) doesn't work if the last clipboard item was an image. Does anyone know a workaround?

Example:

Send('!{PRINTSCREEN}')
Sleep(1000*2)
$lastclip = ClipGet()
If @error Then ConsoleWrite('Msg: '& 'clipget error '& @error & @CRLF)
If $lastclip = '' Then ConsoleWrite('Msg: '& 'nothing in lastclip var' & @CRLF)
Run('"C:\Program Files\Windows NT\Accessories\wordpad.exe"',"C:\Program Files\Windows NT\Accessories\")
ClipPut('Test Msg')
$WinTitle='Document - WordPad'
WinWaitActive($WinTitle)
ClipPut($lastclip)
Sleep(1000*3)
ConsoleWrite('Clipboard: '&$lastclip &'-:' & @CRLF)
If $lastclip = '' Then ConsoleWrite('Msg: '& 'nothing in lastclip var' & @CRLF)
ControlSend($WinTitle,'','RICHEDIT50W1','^v')

ClipGet

--------------------------------------------------------------------------------

Retrieves text from the clipboard.

ClipGet ( )

Parameters

None.

Return Value

Success: Returns a string containing the text on the clipboard.

Failure: Sets @error to 1 if clipboard is empty

to 2 if contains a non-text entry.

to 3 or 4 if cannot access the clipboard.

For more information on this specific Question see link: #429336 Edited by JohnBailey

A decision is a powerful thing

Share this post


Link to post
Share on other sites

thanks for your reply

as for 1), another selection method with which the script doesn't work (doesn't copy the selected text) is ctrl+left/right or ctrl+shift+left/right (left/right = keyboard arrows)

as for 2), I mean that the script should save the text in a variable and then we should use another keyboard shortcut to paste it (other than ctrl+v or 'paste' in the context menu)

as for 3), pausing and unpausing the script is possible ofcourse, but it won't be easy to remember to first disable the script and then select text (in the cases mentioned before)

as for 4), yeap keyboard shortcut to toggle between slots would be more handy, but as far as I can imagine, only GUI offers the ability to preview the text of the slots and choose before paste

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

as for 1), another selection method with which the script doesn't work (doesn't copy the selected text) is ctrl+left/right or ctrl+shift+left/right (left/right = keyboard arrows)

Of course not <_< It's not setup that way. It's only setup to copy using the left-mouse click. Does that make sense? You can change it to detect more if you'd like too and it would be fairly simple.

as for 2), I mean that the script should save the text in a variable and then we should use another keyboard shortcut to paste it (other than ctrl+v or 'paste' in the context menu)

It is saving in a variable ( $clip = ClipGet()). $clip is the variable. You can change the part that says what is "pasting" the text, but you need to setup a HotKey that you can turn off and on with turnon() and turnoff().

as for 3), pausing and unpausing the script is possible ofcourse, but it won't be easy to remember to first disable the script and then select text (in the cases mentioned before)

Could you just change how it does the copy-selected? For instance, copy-selection via a hotkey.

as for 4), yeap keyboard shortcut to toggle between slots would be more handy, but as far as I can imagine, only GUI offers the ability to preview the text of the slots and choose before paste

Well, then I suggest that make it like the following. As you press whatever key series you want (eg. ctrl+B ), a GUI appears and automatically selects the first in of the slots and pressing the second key in the series (eg. B ) will cycle through the slot and then when you release the first part of your key series (eg. ctrl) or both then it uses/pastes whatever slot you stopped on. I suggest using a key series like ctrl+alt+v

Does all that make sense and help? Let me know what else we can help you with.

EDIT

fixed glasses smiley. Didn't know that b and ) makes that smily.

EDIT

Example of different keys copying text too:

#Include <Misc.au3>
#Include <Constants.au3>
Opt("GUIOnEventMode", 1)
Opt("TrayOnEventMode", 1)
Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 1)
Global $switch = "ON"
$on = TrayCreateItem("On")
TrayItemSetOnEvent(-1, "turnon")
$off = TrayCreateItem("Off")
TrayItemSetOnEvent(-1, "turnoff")
$exit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "leave")
TrayItemSetState($on, $TRAY_DISABLE)
$lastclip = ClipGet()
While 1
    If $switch = "ON" Then
        If _IsPressed(01) Then
            $key = 01
            copy($key)
        ElseIf _IsPressed(27) Then
            $key = 27
            copy($key)          
        EndIf
    EndIf
    Sleep(10)
WEnd

Func copy($_key)
    $lastclip = ClipGet()
    While _IsPressed($_key)
        Sleep(10)
    WEnd
    Send( "{CTRLDOWN}" )
    Sleep(10)
    Send("c")
    Sleep(10)
    Send( "{CTRLUP}" )
    $clip = ClipGet()
    
    ConsoleWrite('Text: '&$clip &@CRLF)
    If $clip = "" Then ClipPut($lastclip)
EndFunc
    

Func turnon()
    $lastclip = ClipGet()
    $switch = "ON"
    TrayItemSetState($on, $TRAY_DISABLE)
    TrayItemSetState($off, $TRAY_ENABLE)
EndFunc  ;==>turnon
Func turnoff()
    $switch = "OFF"
    ClipPut($lastclip)
    TrayItemSetState($off, $TRAY_DISABLE)
    TrayItemSetState($on, $TRAY_ENABLE)
EndFunc  ;==>turnoff
Func leave()
    ClipPut($lastclip)
    Exit
EndFunc  ;==>leave
Edited by JohnBailey

A decision is a powerful thing

Share this post


Link to post
Share on other sites

Do you want to not use the Clipboard?


A decision is a powerful thing

Share this post


Link to post
Share on other sites

OMG, I thought what I gave you would get you on your way.... I am not going to write the whole thing for you, which wouldn't be very hard in the first place.

1) Of course, read the script, it only works for mouse click down and up. You would need to add additional triggers. Like a trigger for Ctrl-Left/right, one for Ctrl-A, etc. ( I don't know how you would do one for right click context menus... anybody???)

2)So change it... that is elementary

3)Do you want it to auto copy or not.... AutoIt CANNOT detect user intent and never will. (notice the ON/OFF in the sys tray... I put that there for a reason.

4)This would not be hard to do, but I would like it if you started it on your own first.

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

OMG, I thought what I gave you would get you on your way.... I am not going to write the whole thing for you, which wouldn't be very hard in the first place.

1) Of course, read the script, it only works for mouse click down and up. You would need to add additional triggers. Like a trigger for Ctrl-Left/right, one for Ctrl-A, etc. ( I don't know how you would do one for right click context menus... anybody???)

2)So change it... that is elementary

3)Do you want it to auto copy or not.... AutoIt CANNOT detect user intent and never will. (notice the ON/OFF in the sys tray... I put that there for a reason.

4)This would not be hard to do, but I would like it if you started it on your own first.

Usser,

I agree that we should not write your code - where's the learning in that! In the end, it wouldn't help you. The forum big-part exists to help you learn how to use autoit language. Nonetheless, if you're struggling with a part of the code, then ask specific questions. If you've got concepts that you'd like to discuss, then go ahead. Just know that they aren't always answered. Heck, questions on specific script can be some times difficult to get answers for. That's a BIG part of why asking specific questions in relation to the code and showing what you have done is SO helpful for yourself to get answers and more questions that you need. You're doing a decent job of asking questions, but show your code, ask questions about your code, and ask where you can search for answers. We want to help, but not write your code for you (unless we're REALLY board hahahaha). You've been given some cool starting points and code. Do some work with it and present it here, so we can work with what you've done.

Danwilli,

haha

EDIT

geeze. Had to fix the "would" to be "wouldn't" haha a little few on the letters means a lot of difference! <_<

EDIT

crap~ changing someone else's quote by mistake can TOTALLY mean something different!! HAHAHA sorry about that Danwilli - meant to change mine.

Edited by JohnBailey

A decision is a powerful thing

Share this post


Link to post
Share on other sites

since this script uses 'send ctrl+c' and 'clipget' doesn't this mean that it stores the data in the clipboard and then it retrieves it from the clipboard?

if it does that, it replaces the previous clipboard content, doesn't it?

so even if I make it paste the copied-after-selection text with an other than ctrl+v hotkey (eg ctrl+alt+v), it will still have replace the clipboard context and thus I will have lost the data copied with normal way (not after selection)

Share this post


Link to post
Share on other sites

since this script uses 'send ctrl+c' and 'clipget' doesn't this mean that it stores the data in the clipboard and then it retrieves it from the clipboard?

if it does that, it replaces the previous clipboard content, doesn't it?

so even if I make it paste the copied-after-selection text with an other than ctrl+v hotkey (eg ctrl+alt+v), it will still have replace the clipboard context and thus I will have lost the data copied with normal way (not after selection)

Have you looked at the code or is it not making sense? It is capturing the previous clipboard information (as long as it is text) and saving it (the previous clipboard text information) in a variable. From that point it then "puts" new information "into the clipboard" via windows Copy&Paste method. After the temporary new information is used, the old information (our previously saved text) is put back into the clipboard replacing the new info. Make sense? It's really all there in the code.

Do you not like using the clipboard in this situation

If you do not like using the clipboard, a few methods come to mind. One, save the new (highlighted/selected) text into "system memory." Two, save the new text into an array, which would also give you "slots" (as you described). These are the first two things that came to mind that wouldn't (that I can think of) be difficult.

Do you have any code that you've worked on with this?


A decision is a powerful thing

Share this post


Link to post
Share on other sites

OK, I think I know what you mean now... maybe something like this would get you on your way:

#Include <Misc.au3>
#Include <Constants.au3>
Global $clip = ""
Global $lastclip = ClipGet()
Global $paste = ""
HotKeySet("^+v", "paster")  ;  Sets hotkey for YOUR paste,  this one is CTRL-Shift V
Opt("GUIOnEventMode", 1)
Opt("TrayOnEventMode", 1)
Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 1)
Global $switch = "ON" 
$on = TrayCreateItem("On")
TrayItemSetOnEvent(-1, "turnon")
$off = TrayCreateItem("Off")
TrayItemSetOnEvent(-1, "turnoff")
$exit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "leave")
TrayItemSetState($on, $TRAY_DISABLE)

While 1
    If _IsPressed(01) And $switch = "ON"  Then
        $lastclip = ClipGet()
        $timer = TimerInit()
        $mpos1 = MouseGetPos()
        While _IsPressed(01)
            Sleep(10)
        WEnd
        $mpos2 = MouseGetPos()
        $xdif = $mpos1[0] - $mpos2[0] ; Get the difference in X from the mouse initial click, to the mouse click release
        $ydif = $mpos1[1] - $mpos2[1] ; Get the difference in Y from the mouse initial click, to the mouse click release
        If $xdif < 0 Then $xdif = $xdif * -1 ; Make sure the difference is a positive number
        If $ydif < 0 Then $ydif = $ydif * -1 ; Make sure the difference is a positive number
        $totaldif = $xdif + $ydif ; Add the difs together
        If TimerDiff($timer) > 100 and $totaldif > 5 Then   ;  this makes sure there was at least one second of click and release, or else, it will not copy (so it will not detect false positives on CLICKS) -- Also checks if the mouse was 'dragged' or just clicked ( by checking $totaldif )
            Send("{CTRLDOWN}")
            Sleep(10)
            Send("c")
            Sleep(10)
            Send("{CTRLUP}")
            $clip = ClipGet()
            If Not $clip = "" Then
                $paste = $clip
                ClipPut($lastclip)
            Else
                ClipPut($lastclip)
            EndIf
        EndIf
    EndIf
    Sleep(10)
WEnd
Func turnon()
    $switch = "ON" 
    TrayItemSetState($on, $TRAY_DISABLE)
    TrayItemSetState($off, $TRAY_ENABLE)
EndFunc   ;==>turnon
Func turnoff()
    $switch = "OFF" 
    TrayItemSetState($off, $TRAY_DISABLE)
    TrayItemSetState($on, $TRAY_ENABLE)
EndFunc   ;==>turnoff
Func leave()
    Exit
EndFunc   ;==>leave
Func paster()
    ClipPut($paste)
    Send("{CTRLDOWN}{CTRLDOWN}")
    Sleep(10)
    Send("v")
    Sleep(10)
    Send("{CTRLUP}{CTRLUP}")
    Sleep(10)
    ClipPut($lastclip)
EndFunc   ;==>paster

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