Replace multiple spaces with single space

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Sam P
Posts: 7
Joined: 19 Jan 2022, 18:00

Replace multiple spaces with single space

Post by Sam P » 19 Jan 2022, 18:40

Newbie here. I have written a script to remove double and treble space in some text:

Code: Select all

clipboard := "" ; Empty the clipboard
Send ^a
Send ^c
Loop
{ 
    StringReplace, Clipboard, Clipboard, %A_Space%%A_Space%, %A_Space%, UseErrorLevel 
    if ErrorLevel = 0  ; No more replacements. 
    break 
}
Send ^v
return
I now want the script to operate only on a substring betweeen the strings "ABSTRACT:" and "INTRODUCTION" in the example below:
ABSTRACT:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

INTRODUCTION:

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
I have used the below to create a variable "Alltext2" and then run the script above, using the variable instead of the clipboard, but this does not work. I would be grateful for help.

Code: Select all

	WinActivate, Document - WordPad
	ControlGetText, Alltext, Document - WordPad
	GuiControl, Focus, Document - WordPad

	; Get all text between ABSTRACT and INTRODUCTION
	alltextlen:= strlen(Alltext)
	
	StartPos := InStr(Alltext, "ABSTRACT:" , CaseSensitive := true, StartingPos := 1, Occurrence := 1)
	EndPos := InStr(Alltext, "INTRODUCTION:" , CaseSensitive := true, StartingPos := Startpos, Occurrence := 1)
	Alltext2 := SubStr(Alltext, Startpos+Strlen("ABSTRACT:"), endpos-(startpos+Strlen("INTRODUCTION:")+2)) ; Crop Text

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Replace multiple spaces with single space

Post by boiler » 19 Jan 2022, 19:29

This is the whole script:

Code: Select all

ControlGetText, Alltext, RICHEDIT50W1, Document - WordPad
Count := 1
While Count
	Alltext := RegExReplace(Alltext, "s)ABSTRACT:.*?\K {2,3}(?=.*INTRODUCTION)", " ", Count)
ControlSetText, RICHEDIT50W1, % Alltext, Document - WordPad

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Replace multiple spaces with single space

Post by boiler » 19 Jan 2022, 19:42

Make sure you use the latest version above that was updated to replace multiple instances.

Sam P
Posts: 7
Joined: 19 Jan 2022, 18:00

Re: Replace multiple spaces with single space

Post by Sam P » 19 Jan 2022, 19:50

Thank you very much, boiler. Your script works well, but it removes the bold formatting of 'ABSTRACT' and 'INTRODUCTION', which is why I tried the approach with the substrings. Sorry for not being clear.

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Replace multiple spaces with single space

Post by boiler » 19 Jan 2022, 19:56

You're not really going to be able to keep the formatting if you want to use the ControlGetText approach. You would have to do something like highlight the text you want to update and press a hotkey, and it will replace that.

Run the following script, then highlight the text in between those headers, then press Ctrl+Tab, and it will replace that text with the extra spaces removed. It uses the clipboard, but it restores its prior contents.

Code: Select all

^Tab::
	ClipSave := ClipboardAll
	Clipboard := ""
	Send, {Ctrl down}c{Ctrl up}
	ClipWait, 1
	Clipboard := RegExReplace(Clipboard, " {2,3}", " ")
	Send, {Ctrl down}v{Ctrl up}
	Sleep, 500
	Clipboard := ClipSave
return

Sam P
Posts: 7
Joined: 19 Jan 2022, 18:00

Re: Replace multiple spaces with single space

Post by Sam P » 19 Jan 2022, 20:05

Thanks, I will try that approach.

Post Reply

Return to “Ask for Help (v1)”