A script for word selection that trims the trailing space?

Can I ask for some more help, now with a different script?

I want a script that will trim the trailing space of a word selected via double-clicking. I have the following simple script, which works fine when a selected word is followed by a space but has the undesired result of "trimming" the last character of a word when followed by a punctuation mark.

 

~LButton::
SystemDoubleClickTime := DllCall("GetDoubleClickTime")

If (A_PriorHotKey = A_ThisHotKey and A_TimeSincePriorHotkey < SystemDoubleClickTime)
{
Send +{Left}
}
Return

  

I also have this, found somewhere some time ago, but even though it looks complicated, it has the same result:

  

~LButton::
SystemDoubleClickTime := DllCall("GetDoubleClickTime")

If (A_PriorHotKey = A_ThisHotKey and A_TimeSincePriorHotkey < SystemDoubleClickTime)
{
; Copy everything in the Clipboard
oldClipboard := ClipboardAll

; Empty the Clipboard
clipboard =
; CTRL+ C = copy text selected
Send ^c
; Wait until the Clipboard grabs the content
ClipWait

AutoTrim, Off
word = %Clipboard%
StringLen, len1, word

AutoTrim, On
wordWithoutSpaces = %word%
StringLen, len2, wordWithoutSpaces

IfNotEqual, len1, len2
{
Sendinput +{left} ; SHIFT LEFT
}

; Give contents back to the Clipboard
Clipboard = %oldClipboard%

; Free memory of temp variable
oldClipboard =
}
Return

Parents
  • Hi Nora,

    The first script assumes there is always a trailing space. It just trims off the last character, whatever it is. If you double-click a word that is followed by a space, then the space is included in the selection and is removed from the selection by the "Send +{Left}".

    But if the word is followed by a punctuation mark, then double-clicking creates a selection that ends at the last character of the word. The "Send +{Left}" then removes this last character from the selection.

    The second script should work, as it does not assume there is always a trailing space. If there is a trailing space, then AutoTrim trims it off, which means that len1 and len2 will differ and the trailing space will be removed from the selection by the "Sendinput +{left}".

    So I can't see why the second script wouldn't work. How strange. Are you sure it does the same as the first script?

    One problem with both scripts is that there could be multiple trailing spaces. The "Send +{Left}" in the first script and the "Sendinput +{left}" in the second script would only remove the last space from the selection.

    You might be able to remove all of the trailing spaces from the selection by using "SendInput +{Left %n%}" in the second script, where "n" is equal to the difference in lengths, i.e. "n := len1-len2". I am unfortunately not using AutoHotKey any more, so I can't test whether using "%n%" like this works in a SendInput command.

    Best regards,
    Bruce Campbell
    ASAP Language Services

     

     

  • Thank you for taking a look Bruce. You're right about the multiple spaces, only the last one gets trimmed. I tried SendInput +{Left %n%} and it makes no difference, though.

    I'm hoping I can get that second script to work, but at the moment it doesn't do what it's supposed to do.
Reply Children
No Data