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
  • There is a very simple way to get rid of trailing spaces: just assign a variable to itself as a value, in the process, trailing spaces will be removed.

     

  • Hi Raphaël and Nora,

    Script 2 does exactly that by setting AutoTrim OFF before setting "word" equal to the ClipBoard, so that "word" DOES includes the trailing space in the selection and then setting AutoTrim ON before setting the variable "wordWithoutSpaces" equal to "word" so that any trailing spaces are trimmed.

    The question is why script 2 is not working.

    Nora, can you put msgbox commands in script 2 to display the contents of the different variables, like "word", "wordWithoutSpaces", "len1" and "len2"?

    Then double-click a word followed by a punctuation mark to find out why len1 and len2 are not equal.

    They should be equal, but the only way script 2 would execute the "Sendinput +{left}" is if they are NOT equal. So it would be interesting to see what is actually happening ...

    Best regards,
    Bruce Campbell
    ASAP Language Services
  • Hi Bruce,

    Good idea! Here are two screenshots. According to this, it should work, right?

             

     

    Here's a video showing the script in action. Not sure about the false start there at the beginning with the first word, but you can see how it works for all the words except the last one right before the period.

  • Hi Nora,

    Okay. Problem solved :-)

    To make the script work in Word and Notepad, you need a "Sleep, 200" after emptying the clipboard. Here is what the script looks like:

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

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

    ; Empty the Clipboard
    clipboard =

    Sleep, 200 ; without this the script breaks in Word and Notepad !!

    ; 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

    n := len1 - len2 ; introduce variable n so that multiple spaces can be eliminated !!
    If (n > 0)
    {
    Sendinput +{left %n%} ; SHIFT LEFT
    }

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

    ; Free memory of temp variable
    oldClipboard =

    }
    Return

Reply
  • Hi Nora,

    Okay. Problem solved :-)

    To make the script work in Word and Notepad, you need a "Sleep, 200" after emptying the clipboard. Here is what the script looks like:

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

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

    ; Empty the Clipboard
    clipboard =

    Sleep, 200 ; without this the script breaks in Word and Notepad !!

    ; 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

    n := len1 - len2 ; introduce variable n so that multiple spaces can be eliminated !!
    If (n > 0)
    {
    Sendinput +{left %n%} ; SHIFT LEFT
    }

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

    ; Free memory of temp variable
    oldClipboard =

    }
    Return

Children