Q:

Trim text / delete trailing spaces from clipboard

I'm struggling with the following thing. I have a simple script, that points to a word with mouse, clicks on it, does copy (ctrl+c), then does paste (ctrl+v).
Now, when a copy is done, I need to trim the text in the buffer/clipboard, meaning delete either beginning space (if any) or ending space (if any) - or both. Here's the code I have so far (I list only relevant part):

...
MouseMove, 439, 219
Click 2
Send ^c
ClipWait
clipboard := RegexReplace(clipboard, "^\s+|\s+$")
clipboard := Trim(clipboard," `t`r`n")
clipboard := LTrim(clipboard, OmitChars = " `t`r`n")
clipboard := RTrim(clipboard, OmitChars = " `t`r`n")
StringReplace, clipboard, clipboard, `t`r`n, , All
StringReplace, clipboard, clipboard, %A_Space%, , All
Send ^v
...

As you can see I used all the possible options I could find, and STILL the space does not get removed. But also, the strange thing is sometimes the space is removed, but in around 40% of cases it's not. But in any case, I simply need to a solution that would trim/remove spaces in all cases (every single time).
Any ideas, suggestions? 

A:

Your code seems to work for me.  This leads be to believe it has something to do with the timing of your clipboard contents. The likely culprit of your problem is your usage of ClipWait.  The most reliable way to use it is to clear out your clipboard first, then copy, then use ClipWait with a timeout value. Here is some example usage.
clipboard := ""  ; Clear clipboard
Send ^c
ClipWait, 10  ; Waits 10 seconds for clipboard to contain something
Msgbox % clipboard

