Weird space trimming in variable assignation

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Nixcalo
Posts: 116
Joined: 06 Feb 2018, 04:24

Weird space trimming in variable assignation

Post by Nixcalo » 11 Nov 2021, 06:18

Hi everyone!

This must be probably stupid, but I am puzzled by this and I cannot find the way to solve it elegantly.

Simply said, I want to enclose a string in parentheses when I use a hotkey.

If I double click in a word, it usually adds spaces to the right of it, doesn't it? And sometimes, if I am selecting the string manually with the mouse, I may accidentally select spaces from the left as well.

So if I want to enclose the number 28 in the following sentence: "I want 28 cakes" I need to have "I want (28) cakes", regardless of whether I select spaces to the left or right of it.

I tried with regex substitution but couldn't make it work for all cases, so here's my final code.

Code: Select all

 ^>:: ; Copies the source and encloses it in parentheses
ClipBoard :=""
SendInput ^x
ClipWait, .1
Word := Clipboard  
Word := SubStr(Word, 1,1) = A_Space ? A_Space . "(" . LTrim(Word) : "(" . Word
Word:=SubStr(Word, 0,1) = A_Space ? RTrim(Word) . ")" . A_Space  :  Word . ")"
ClipBoard:= Word
SendInput, ^v

Return
The thing is, this code works perfectly with parentheses on the RIGHT of the string. It correctly takes the space OUT of the rightmost parenthesis.Remember, I don't want to get rid of the parentheses, I just want them placed OUTSIDE of them (in case they exist).
So if I have "I want 28 cakes" I need it to be "I want (28) cakes", not "I want ( 28) cakes" or "I want(28) cakes" or any combination, so the spaces in the selection MUST be preserved, just placed in their proper place so I can have a nice sentence with right spacing.

So the thing is, Autohotkey trims any number of spaces to the LEFT of the string. Not just one, but all of them. If I do this:

Code: Select all

Word := SubStr(Word, 1,1) = A_Space ?  "            (" . LTrim(Word) : "(" . Word ; A lot of spaces to the left of the open parenthesis
it still won't work.
So with this selection.
image.png
image.png (5.89 KiB) Viewed 508 times
it works and this is the result
image.png
image.png (6.18 KiB) Viewed 508 times
but with this one
image.png
image.png (6.13 KiB) Viewed 508 times
it does not, I obtain this
Interruptor para establecer/decelerar(28) funciona correctamente.

Any ideas why and how to solve it?



But if the parentheses are on the left, it does not work. Autohotkey trims it, no matter what!

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Weird space trimming in variable assignation

Post by mikeyww » 11 Nov 2021, 06:49

Code: Select all

^>:: ; Copies the source and encloses it in parentheses
Clipboard =
Send ^x
ClipWait, 0
If !ErrorLevel {
 RegExMatch(Clipboard, "^ +", prefix), RegExMatch(Clipboard, " +$", suffix)
 SendInput % prefix "(" Trim(Clipboard) ")" suffix
} Else MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Return

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Weird space trimming in variable assignation

Post by wetware05 » 11 Nov 2021, 08:06

Thanks Mikeyww.

I have changed it to close between two percentage symbols %Clipboard%, very useful for writing Autohotkey scripts.

Nixcalo
Posts: 116
Joined: 06 Feb 2018, 04:24

Re: Weird space trimming in variable assignation

Post by Nixcalo » 11 Nov 2021, 08:27

As expected, it works beautifully. Now I have to study why, of course, so I can learn.

Do you have any idea why my method does not work? Why it does that strange trimming on the left of the string?

And of course, a huge THANK YOU! :clap:

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Weird space trimming in variable assignation

Post by mikeyww » 11 Nov 2021, 08:49

I did not reproduce your problem, but I noticed that you pasted your final text without a ClipWait. This can lead to problems, because the clipboard is slow. An example of how to use ClipWait can be found... in your script!

My second suggestion is that if you are going to use ClipWait, then check the ErrorLevel so that your script can then respond to it.

Post Reply

Return to “Ask for Help (v1)”