AutoHotkey Community

It is currently May 27th, 2012, 12:48 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 27 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: April 14th, 2010, 12:31 pm 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
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:
Code:
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, 20
      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:
Quote:
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:
Code:
; 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.

Code:
; 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.

Code:
; 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

Code:
; 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:
Code:
#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)


Last edited by Learning one on April 20th, 2010, 3:20 pm, edited 8 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 14th, 2010, 12:51 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Interesting, I made something similar for a script I'm working on :D

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 16th, 2010, 8:49 pm 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 16th, 2010, 11:05 pm 
Offline

Joined: February 17th, 2008, 8:52 pm
Posts: 314
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 17th, 2010, 9:21 pm 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
Ok. I'm preparing VirClip() update with more tasks. I'll probably post it tomorrow.

By the way, here is gst()
Code:
; 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
*/


Last edited by Learning one on April 20th, 2010, 3:30 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 17th, 2010, 9:55 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
Quote:
- no globals

nice.
Quote:
Consider it as public domain.

nice!
Quote:
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.

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 18th, 2010, 5:12 pm 
Offline

Joined: February 17th, 2008, 8:52 pm
Posts: 314
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.

Code:
   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
      Clipboard := Clipboard
      ClipWait, 0.5, 1
      Sleep, 30

      Send, ^v
      Sleep, 20
      While !(Clipboard = "")   
      {
         Clipboard =
         Sleep, 20
      }
      Clipboard := ClipboardBackup
      ClipWait, 0.5, 1
      Return Clip%ClipNum%
   }


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 19th, 2010, 10:40 am 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 20th, 2010, 10:16 am 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 20th, 2010, 3:27 pm 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
quick fix for case mentioned above done for both VirClip() and gst()


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 25th, 2010, 2:57 pm 
Offline

Joined: February 17th, 2008, 8:52 pm
Posts: 314
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 26th, 2010, 7:24 pm 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
Hmm... I can't figure out what in VirClip() code could cause that... Can anybody find mistake?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 26th, 2010, 8:45 pm 
Offline

Joined: February 17th, 2008, 8:52 pm
Posts: 314
I made some tweaks to your code and it seemed to fix my problem. If I encounter anything else I will let you know.

Code:
      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
      }


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 21st, 2011, 3:46 pm 
Hey, neat function! 3 comments:

  1. I recommend you remove this code:
    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.
  2. 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.
  3. What does this line do?
    Code:
    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.autohotkey.com/forum/viewtopic.php?p=468243 that can do both very efficiently thanks to combining the functionalities and using a timer.


Bye!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 21st, 2011, 3:50 pm 
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.


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 27 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Bon, bowen666, Yahoo [Bot] and 18 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group