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

  • Hi Nora,

    You might be able to reduce the "Sleep, 200".

    I can get away with "Sleep, 100", but if I try "Sleep, 75" the script breaks in Word and Notepad.

    Actually, the script isn't "breaking", it turns out it is just waiting for the "ClipWait" command to return, but it never does.

    To prevent "ClipWait" from waiting forever, you can change the "ClipWait" command to "ClipWait, 0.5"

    That way it will only wait a half a second. If you use too small a sleep period, the "ClipWait, 0.5" command will time out and the script will continue executing with the variable "word" empty, so it will appear that the command is not working.

    Best regards,
    Bruce Campbell
    ASAP Language Services
  • Bruce,

    Thank you so much, this does exactly what I need! I'll have a closer look tomorrow to understand the changes you made and hopefully learn from that.

    I'll be using this practically only in Studio. In fact, I'll be using it in Studio with Dragon while editing, so it will save me having to add and delete spaces after selecting a word.

    Thank you again!
Reply Children
  • Glad I could help Nora,

    Normally the Sleep would not be needed. I think what is happening is that the script starts running before the selection has stabilized.

    You double click and then there is a lag before the word is actually selected and ready to be copied with a Ctrl-C. The lag is longer in Word and Notepad than it is in Studio.

    So if the script starts immediately after a double click and zooms to the Ctrl-C before the word has actually been selected, then the Clipboard remains empty, and ClipWait waits forever.

    If you are using this mainly with Studio, then it can be sped up a bit using "IfWinNotActive". See the script below :-)

    Again, you might be able to reduce the "Sleep, 200", and you might be able to eliminate the "Sleep, 50" (and the else branch) altogether.

    Best regards,
    Bruce Campbell
    ASAP Language Services

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

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

    ; Empty the Clipboard
    clipboard =

    IfWinNotActive SDL Trados Studio
    {
    Sleep, 200 ; This sleep command is for all windows other than Studio. You might be able to use a number less than 200.
    }
    else
    {
    Sleep, 50 ; This sleep command is for Studio. I don't need to sleep in Studio, so you might be able to delete this line.
    }

    ; CTRL+C = copy text selected
    SendInput ^c

    ; Wait until the Clipboard grabs the content
    ClipWait, 0.5 ; Just in case you don't sleep long enough, the 0.5 keeps the script from waiting forever.

    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