topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 8:16 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: CHS tip - retain last clip's formatting with an AutoHotKey workaround script  (Read 5009 times)

IainB

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,540
  • @Slartibartfarst
    • View Profile
    • Read more about this member.
    • Donate to Member
This tip is about something that I have found to be a very handy time-saver.
I use CHS a lot, and sometimes I want to:
(a) first, paste as plain (unformatted) text something that I have just copied, into a document.
(b) second, paste the same text - but with all its original formatting - into a different document.
I can't do this with CHS.
This post explains why, and how to get a reasonably good workaround to that, using an AHK script.

Using CHS, when you copy something (text or image) to the clipboard, it gets saved in CHS as either image or text.
An image on its own is saved as an image, in CHS.
Text is saved as text without the formatting, in CHS.
Images and text copied combined in a single clip together are saved as either image, or text, depending on your CHS priority setting.
But whatever was in the clipboard buffer (image, or text, or image and text) remains as is - including any text formatting - until it is overwritten by the next copy action or by CHS.

If you paste the last clip that was copied into the clipboard buffer, then it is pasted intact from the clipboard buffer, together with its formatting (includes any objects in the clip also).

But, if you use the CHS command to paste it as plain (unformatted) text, then CHS apparently removes the formatting - effectively overwriting the clipboard buffer with plain (unformatted) text - and that buffer's content is then pasted into your document.
Having done that, if you then want to paste the formatted text anywhere else - you cannot paste it. This is because the formatted text does not exist in the clipboard buffer, having been overwritten by the plain (unformatted) text. (This can be very frustrating at times!)

There is an AutoHotKey script which provides a handy and time-saving workaround for this.
To use it, first disable any keyboard hotkeys that may be set in CHS to force a plain (unformatted) text paste. So CHS does not change whatever text and formatting it finds in the clipboard buffer - i.e., it always pastes what is in the buffer.

The AHK script intercepts these key combo inputs, and takes action as shown:
  • Ctrl+Shift+V: this causes the formatted text to be pasted.
  • Ctrl+V: this causes the plain (unformatted) text to be pasted.

(You can change the script to use other key combos, if you want. These particular ones suit the ergonomics of my way of working to a "T".)

The AHK script works thus:
1. When Ctrl+Shift+V is pressed: the keyboard string "Ctrl+V" is passed to the system, causing a normal (unmodified) paste of whatever is in the clipboard buffer.
2. When Ctrl+V is pressed:
(a) the current as-copied formatted text contents (alas, minus any accompanying image objects) of the clipboard buffer are saved intact;
(b) the contents of the clipboard buffer are overwritten with the plain (unformatted) text (i.e., formatting is removed);
(c) the keyboard string "Ctrl+V" is passed to the system, causing a normal (unmodified) paste of whatever is in the clipboard buffer.
(d) the saved as-copied formatted text is restored to the clipboard buffer (minus any accompanying image objects that may have been in the original clip).

This is a copy of the AHK script:
Spoiler
;-------------------------------------------------------------------
$^v::                                         ; Intercept Ctrl+v to send an UNformatted Paste
   Gosub, UnformattedPaste         ; Text–only unformatted paste from ClipBoard
   return
;-------------------------------------------------------------------
$^+v::                      ; Intercept Ctrl+Shift+v to send a Formatted Paste
   Send ^v             ; Normal paste (sends full contents and formatting of Clipboard)
   return
;-------------------------------------------------------------------

;-------------------------------------------------------------------
UnformattedPaste:               ; Text–only unformatted paste from ClipBoard
   ClipSaved := ClipboardAll       ; save original clipboard contents
   Clipboard = %Clipboard%    ; Convert to text + remove formatting
   Send ^v                     ; send the Ctrl+v command
   Clipboard := ClipSaved       ; restore the original clipboard contents
   ClipSaved =              ; clear the variable
Return
;-------------------------------------------------------------------

« Last Edit: July 28, 2012, 07:55 AM by IainB, Reason: Script tidied up to enable easier comparison with later comment. »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Interesting.

It makes me happy that you like CHS enough to use it despite it not doing everything you need.

Eventually CHS will support saving fully formatted text and you won't have to go through such hoops.

But in the meantime i think i might be able to come up with an easier workaround for you:

You have diagnosed the problem, which is that when you ask CHS to paste the last clip as plaintext, it overwrites the current clipboard. I can try to fix that.

IainB

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,540
  • @Slartibartfarst
    • View Profile
    • Read more about this member.
    • Donate to Member
It makes me happy that you like CHS enough to use it despite it not doing everything you need.
 ...
You have diagnosed the problem, which is that when you ask CHS to paste the last clip as plaintext, it overwrites the current clipboard. I can try to fix that.
Oh, thankyou!
I use CHS because it is the best clipboard manager that I have found.
I mentioned elsewhere that I don't regard this as a problem or bug with CHS. It's just something that I personally find a nuisance because of my way of working.
It's not a show-stopper as far as I am concerned.

Ideally, I'd like CHS to save the formatting/RTF or mixed text and image objects too...    :)  (endearing smile)

IainB

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,540
  • @Slartibartfarst
    • View Profile
    • Read more about this member.
    • Donate to Member
Thought I'd better post this as an update/correction.
I had been experiencing some episodic but erratic behaviour (time-delays and failure to complete) in the proper pasting of formatted and unformatted text - where sometimes quite large Clipboard contents were involved - using the above AHK script. It worked OK before.
Since I had not changed the script, I figured that changes from either/both of CHS updates or Win7-64 updates had altered the behind-the-scenes Clipboard process activity/timings.

This was the script from above:
;-------------------------------------------------------------------
UnformattedPaste:                    ; Text–only unformatted paste from ClipBoard
   ClipSaved := ClipboardAll       ; save original clipboard contents
   Clipboard = %Clipboard%      ; Convert to text + remove formatting
   Send ^v                               ; send the Ctrl+v command
   Clipboard := ClipSaved          ; restore the original clipboard contents
   ClipSaved =                          ; clear the variable
Return
;-------------------------------------------------------------------

I managed to fix it all up after reading through:
- and by changing the above script to this:
;-------------------------------------------------------------------
UnformattedPaste:              ; MARK2 Text–only unformatted paste from ClipBoard
   ClipSaved = %ClipBoardAll%   ; save original clipboard contents
   ClipBoard = %ClipBoard%   ; Convert to text + remove formatting
   Send ^v                         ; For best compatibility: SendPlay
   Sleep 50                         ; Don't change clipboard while it is pasted! (Sleep > 0)
   ClipBoard = %ClipSaved%       ; Restore original ClipBoard
   VarSetCapacity(ClipSaved, 0)   ; Free memory
Return
;-------------------------------------------------------------------

The erratic behaviour seems to have completely gone away.

The MARK2 script may be faster (more efficient) because it stores values in variables as expressions:
Storing values in variables: To store a string or number in a variable, there are two methods: traditional and expression. The traditional method uses the equal sign operator (=) to assign unquoted literal strings or variables enclosed in percent signs.
- and tidier, because it uses arguably good/better practice to clear up and zeroise its ad hoc memory footprint.
However, I would suppose (have no proof) that the "Sleep 50" bit may be what fixed it though, allowing differences in CPU speed and memory speed to run their sequential course without interruption, where larger amounts of data are stored in the Clipboard.
It would be interesting to know exactly what had been going on in the CPU/RAM though.

The new MARK2 script is cribbed from the script posted by the most excellent Lazlo (Posted: July 29th, 2006, 3:20 pm) in the above AHK forum link.