Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[function] Virtual clipboards+


  • Please log in to reply
28 replies to this topic
Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009

WARNING: This is old thread. It is continued here.

Virtual clipboards+ by Learning one
- preserves clipboard
- can copy to and paste from virtual clipboard(s) - basic usage
- can do some extra stuff
- capable to support unlimited number of virtual clipboards, but limited to 30
- every task/command returns virtual clipboard's contents
- no globals

Nothing revolutionary, but handy IMO. If your goal is just to get selected text or file(s) in
Windows explorer, but preserve clipboard; you can use this shorty - gst().

License: public domain

Function:


VirClip(Task,ClipNum=1,Value="") ; by Learning one
{
static Clip1, Clip2, Clip3, Clip4, Clip5, Clip6, Clip7, Clip8, Clip9, Clip10, Clip11, Clip12, Clip13, Clip14, Clip15
, Clip16, Clip17, Clip18, Clip19, Clip20, Clip21, Clip22, Clip23, Clip24, Clip25, Clip26, Clip27, Clip28, Clip29, Clip30
if ClipNum not between 1 and 30
Return
IsClipEmpty := (Clipboard = "") ? 1 : 0
if (task = "c" or task = "ca" or task = "x" or task = "xa" or task = "Copy" or task = "CopyAll" or task = "Cut" or task = "CutAll")
{
ClipboardBackup := ClipboardAll
While !(Clipboard = "") {
Clipboard =
Sleep, 10
}
if (task = "c" or task = "ca" or task = "Copy" or task = "CopyAll")
Send, ^c
Else
Send, ^x
if (task = "c" or task = "x" or task = "Copy" or task = "Cut") {
ClipWait, 0.5
if !(Clipboard = "")
Clip%ClipNum% := Clipboard
}
Else {
ClipWait, 0.5, 1
if !(Clipboard = "")
Clip%ClipNum% := ClipboardAll
}
Clipboard := ClipboardBackup
if !IsClipEmpty
ClipWait, 0.5, 1
Return Clip%ClipNum%
}
else if (task = "v" or task = "vt" or task = "Paste" or task = "PasteText") {
if (Clip%ClipNum% = "")
Return
ClipboardBackup := ClipboardAll
While !(Clipboard = "") {
Clipboard =
Sleep, 10
}
Clipboard := Clip%ClipNum%
ClipWait, 0.5, 1
Sleep, 40
if (task = "vt" or task = "PasteText") {
Clipboard := Clipboard
ClipWait, 0.5
Sleep, 30
}
Send, ^v
Sleep, 200
While !(Clipboard = "") {
Clipboard =
Sleep, 10
}
Clipboard := ClipboardBackup
if !IsClipEmpty
ClipWait, 0.5, 1
Return Clip%ClipNum%
}
else if (task = "e" or task = "Empty") {
Clip%ClipNum% =
Return
}
else if (task = "ea" or task = "EmptyAll") {
Loop, 30
Clip%A_Index% =
Return
}
else if (task = "g" or task = "Get")
Return Clip%ClipNum%
else if (task = "s" or task = "Set") {
Clip%ClipNum% := Value
Return Clip%ClipNum%
}
else if (task = "a" or task = "Append") {
Clip%ClipNum% .= Value
Return Clip%ClipNum%
}
else if (task = "p" or task = "Prepend") {
Clip%ClipNum% := Value Clip%ClipNum%
Return Clip%ClipNum%
}
else if (task = "as" or task = "ps" or task = "AppendSelected" or task = "PrependSelected") {
ClipboardBackup := ClipboardAll
While !(Clipboard = "") {
Clipboard =
Sleep, 10
}
Send, ^c
ClipWait, 0.5
if !(Clipboard = "") {
if (Clip%ClipNum% = "")
Clip%ClipNum% := Clipboard
Else {
if (task = "as" or task = "AppendSelected")
Clip%ClipNum% .= value Clipboard
Else
Clip%ClipNum% := Clipboard Value Clip%ClipNum%
}
}
While !(Clipboard = "") {
Clipboard =
Sleep, 10
}
Clipboard := ClipboardBackup
if !IsClipEmpty
ClipWait, 0.5, 1
Return Clip%ClipNum%
}
else if (task = "uc" or task = "UpperCase") {
StringUpper, Clip%ClipNum%, Clip%ClipNum%
Return Clip%ClipNum%
}
else if (task = "lc" or task = "LowerCase") {
StringLower, Clip%ClipNum%, Clip%ClipNum%
Return Clip%ClipNum%
}
else if (task = "tc" or task = "TitleCase") {
StringUpper, Clip%ClipNum%, Clip%ClipNum%, T
Return Clip%ClipNum%
}
}

/* VirClip tasks:
"c" or "Copy" ; copies just text. (Clipboard)
"ca" or "CopyAll" ; copies all data; text, pictures, formatting. (ClipboardAll)
"x" or "Cut"
"xa" or "CutAll"
"v" or "Paste"
"vt" or "PasteText" ; pastes only text from virtual clipboard.

"e" or "Empty"
"ea" or "EmptyAll"
"g" or "Get"
"s" or "Set"
"a" or "Append"
"p" or "Prepend"
"as" or "AppendSelected" ; Appends selected text, not pictures etc.
"ps" or "PrependSelected"

"uc" or "UpperCase"
"lc" or "LowerCase"
"tc" or "TitleCase"
*/


/*
; Example 1 - basic
SelectedText := VirClip("c") ; this line: 1) copies selected text to virtual clipboard, 2) stores it in variable "SelectedText" and 3) preserves (original) clipboard. 4) It can also get full path of selected file(s) in Windows explorer.
*/


/*
; Example 2 - basic
1::VirClip("ca") ; copy selected to virtual clipboard. Copies all data; text, pictures, formatting. (ClipboardAll)
2::VirClip("v") ; paste all data from virtual clipboard. (Text, pictures, formatting)
3::VirClip("vt") ; paste just text from virtual clipboard.
*/


/*
; Example 3 - some sort of log
1::VirClip("as",4, "|") ; append selected text to virtual clipboard number 4. "|" serves as delimiter in this case (optional)
2::VirClip("v",4) ; paste from virtual clipboard number 4
*/

/*
; Example 4 - some other features
1::
VirClip("s",3,"set text") ; set "set text" to virtual clipboard number 3
VirClip("a",3," appended text") ; append " appended text" to virtual clipboard number 3
VirClip("tc",3) ; convert text of virtual clipboard number 3 to title case
VirClip("v",3) ; paste from virtual clipboard number 3
Return
*/

VirClip tasks:


VirClip tasks:
"c" or "Copy" ; copies just text. (Clipboard)
"ca" or "CopyAll" ; copies all data; text, pictures, formatting. (ClipboardAll)
"x" or "Cut"
"xa" or "CutAll"
"v" or "Paste"
"vt" or "PasteText" ; pastes only text from virtual clipboard.

"e" or "Empty"
"ea" or "EmptyAll"
"g" or "Get"
"s" or "Set"
"a" or "Append"
"p" or "Prepend"
"as" or "AppendSelected" ; Appends selected text, not pictures etc.
"ps" or "PrependSelected"

"uc" or "UpperCase"
"lc" or "LowerCase"
"tc" or "TitleCase"


Examples:


; Example 1 - basic
SelectedText := VirClip("c") ; this line: 1) copies selected text to virtual clipboard, 2) stores it in variable "SelectedText" and 3) preserves (original) clipboard. 4) It can also get full path of selected file(s) in Windows explorer.
; Example 2 - basic
1::VirClip("ca") ; copy selected to virtual clipboard. Copies all data; text, pictures, formatting. (ClipboardAll)
2::VirClip("v") ; paste all data from virtual clipboard. (Text, pictures, formatting)
3::VirClip("vt") ; paste just text from virtual clipboard.
; Example 3 - some sort of log
1::VirClip("as",4, "|") ; append selected text to virtual clipboard number 4. "|" serves as delimiter in this case (optional)
2::VirClip("v",4) ; paste from virtual clipboard number 4
; Example 4 - some other features
1::
VirClip("s",3,"set text") ; set "set text" to virtual clipboard number 3
VirClip("a",3," appended text") ; append " appended text" to virtual clipboard number 3
VirClip("tc",3) ; convert text of virtual clipboard number 3 to title case
VirClip("v",3) ; paste from virtual clipboard number 3
Return

Simple script:


#Include VirClip.ahk

; copy selected text, clear formating, preserve clipboard
+F1::VirClip("c",1)
+F2::VirClip("c",2)
+F3::VirClip("c",3)

; paste
F1::VirClip("v",1)
F2::VirClip("v",2)
F3::VirClip("v",3)


SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
Interesting, I made something similar for a script I'm working on :D

Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
VirClip() updated --> Added CopyAll. Copy - CopyAll difference:
- "Copy" or "c" copies just text. (Clipboard) Example: JustText := VirClip("c")
- "CopyAll" or "ca" copies all data; text, pictures, formatting. (ClipboardAll) Example: AnyData := VirClip("ca")

Yeah, I know that this function looks ugly, but is reliable, and that is priority. When I shortened it and removed some lines that look unnecessary, it wasn't so reliable.

vahju
  • Members
  • 337 posts
  • Last active: Sep 21 2014 03:52 AM
  • Joined: 17 Feb 2008
Any thoughts to adding Prepend/PrependSelected and PasteTextOnly.

PasteTextOnly would keep the virtual clip intact (encase it contains pics and still need to use it later) but allows you to remove all formatting when pasting for that particular time.

Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
WARNING: This is old thread. It is continued here.

Ok. I'm preparing VirClip() update with more tasks. I'll probably post it tomorrow.

By the way, here is gst()
; Gets selected text but preserves clipboard. Can also get full path of selected file(s) in windows explorer.
gst() { ; GetSelectedText by Learning one
IsClipEmpty := (Clipboard = "") ? 1 : 0
if !IsClipEmpty {
ClipboardBackup := ClipboardAll
While !(Clipboard = "") {
Clipboard =
Sleep, 10
}
}
Send, ^c
ClipWait, 0.1
ToReturn := Clipboard, Clipboard := ClipboardBackup
if !IsClipEmpty
ClipWait, 0.5, 1
Return ToReturn
}

/*
; Example
1::MsgBox,,, % "Selected text:" A_Tab gst() "`nClipboard:" A_Tab Clipboard
*/


Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006

- no globals

nice.

Consider it as public domain.

nice!

Learning one

nice :D

And all in a function form. nice.

I keep an eye and will include it into "you know where". :D I like most of your work. Do not overload it with features please. Otherwise testing time and bug possibilities increases and the simplecity for end user is gone.

For example, I do not see any sense to include an "upper case" functionality. It is (along with some other features) a bit non related to me. May be its only me, but a good function should not include all functions.

No signature.


vahju
  • Members
  • 337 posts
  • Last active: Sep 21 2014 03:52 AM
  • Joined: 17 Feb 2008
Not sure if this will be useful for your function but PastText will allow you to paste clip as text only and keeps the original clip intact. I added the 3 lines in black lines.

   else if (task = "pt" or task = "PasteText")    ;Pastes clipboard
   {
      if (Clip%ClipNum% = "")
         Return
      ClipboardBackup := ClipboardAll
      While !(Clipboard = "")   
      {
         Clipboard = 
         Sleep, 20
      }
      Clipboard := Clip%ClipNum%
      ClipWait, 0.5, 1
      Sleep, 30
      [color=black]Clipboard := Clipboard
      ClipWait, 0.5, 1
      Sleep, 30[/color]
      Send, ^v
      Sleep, 20
      While !(Clipboard = "")   
      {
         Clipboard = 
         Sleep, 20
      }
      Clipboard := ClipboardBackup
      ClipWait, 0.5, 1
      Return Clip%ClipNum%
   }


Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
VirClip() updated. See first post.
Unfortunately, due to ClipboardAll --> Virtual clipboard or ClipboardBackup --> Clipboard data transfer,
in some cases images may become corrupted/unusable. I think I can't fix this.

@Tuncay: Thanks for kind words. I agree that some features maybe don't fit in. If you want, delete them. I needed them for one of mine scripts.
@vahju: I implemented your suggestions. Thanks for posting example code for paste text only feature.

Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
I forgot to code case when clipboard is empty from beginning. In this case:
- current system waits 0.5 sec in vain
- in upcoming update there will not be useless wait

Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
quick fix for case mentioned above done for both VirClip() and gst()

vahju
  • Members
  • 337 posts
  • Last active: Sep 21 2014 03:52 AM
  • Joined: 17 Feb 2008
Every once and while I run into a problem with the latest VirClip() code. If I already have a clip populated and go to replace that same clip sometimes the previous clip gets pasted. If I go to paste the same clip again the correct clip gets pasted. I am using your code that was updated as of Apr 20. I did not make any changes out side of limiting clips from 1-10.

Not sure if it has to do with erasing the previous clip before replacing it when doing copy, copyall, cut, or cutall. I am using this on a WinXP sp2 box at work. I have some other ahk code I use and verified its not using the same variable.

I ran another test just now and here is what happened.
Ran myahk script which contain virclip function.
Copied text to clip1
Copied text to clip2
Copied text to clip3
pasted clip1 end up with nothing pasted
pasted clip2 worked
pasted clip3 worked
pasted clip1 worked

Just wanted to say thanks for this great function.

Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
Hmm... I can't figure out what in VirClip() code could cause that... Can anybody find mistake?

vahju
  • Members
  • 337 posts
  • Last active: Sep 21 2014 03:52 AM
  • Joined: 17 Feb 2008
I made some tweaks to your code and it seemed to fix my problem. If I encounter anything else I will let you know.

      if (task = "c" or task = "ca" or task = "Copy" or task = "CopyAll") {
         Clip%ClipNum% =
         Sleep, 20
         Send, ^c
      }
      Else {
         Clip%ClipNum% =
         Sleep, 20
         Send, ^x
      }


berban!
  • Guests
  • Last active:
  • Joined: --
Hey, neat function! 3 comments:

[*:12gareik]I recommend you remove this code:
While !(Clipboard = "") { 
         Clipboard = 
         Sleep, 10 
      }
In my experience, a regular ol' Clipboard := "" works fine. Maybe that's just my computer, but I'd recommend you try it out and see if it makes any problems.

[*:12gareik]After you paste, you sleep 20. In my experience, PASTING is where clipboard stuff can mess up. I always sleep at least 400 after pasting before modifying the contents of the clipboard again. Otherwise, sometimes the script has a tendency to paste the BACKUP contents, instead of what you wanted to paste. 400 might be overkill but it's just that this is where i have seen issues in the past so I just want to be safe.

[*:12gareik]What does this line do?
Clipboard := Clipboard
:?:

Another nice feature might be the option to save the clips to files? That way they could be preserved between reloads. Like Clips\Clip1.dat for instance.


Finally, not to plug inappropriately :oops: but for the simpler tasks of getting selected text and pasting some text I made this function http://www.autohotke...ic.php?p=468243 that can do both very efficiently thanks to combining the functionalities and using a timer.


Bye!

berban!
  • Guests
  • Last active:
  • Joined: --
ps. looking at vahju's comment, what i said in my #2 could be the culprit...?

It took me forever to figure out that bug with the clipboard :p very tricky!

If its not that then I'd be interested to know what is causing that behavior, cause I doubt it's your script.