Creating hotkeys to edit lists — issue with first/last lines Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
sn1perwild
Posts: 62
Joined: 04 Aug 2021, 15:11

Creating hotkeys to edit lists — issue with first/last lines

14 Sep 2021, 12:50

Hello, everyone!

I'm building a script with two hotkeys: one adds text to the beginning of each selected line, and another that adds text to the end of each selected line. Such text will come from an input box.

I've gotten it to work well for the most part — for some reason, it does not add text to the first line (beginning) and last line (end). I'm sure I'm missing something in RegExReplace(), I'm just not certain of what :(

Here is the script:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; Add text to the beginning of each line

^+!Left::
InputBox, text, Add text to the beginning of each line, Enter text:, , 256, 125
SaveVar=%ClipboardAll%
Clipboard=
Send ^c
ClipWait, 0.5
Clipboard := RegExReplace(Clipboard, "`n", text)
Send ^v
Sleep 100
Clipboard=%SaveVar%
SaveVar=
return

; Add text to the end of each line

^+!Right::
InputBox, text, Add text to the end of each line, Enter text:, , 256, 125
SaveVar=%ClipboardAll%
Clipboard=
Send ^c
ClipWait, 0.5
Clipboard := RegExReplace(Clipboard, "`r", text)
Send ^v
Sleep 100
Clipboard=%SaveVar%
SaveVar=
return
Can someone help me? Thanks in advance :)
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: Creating hotkeys to edit lists — issue with first/last lines

14 Sep 2021, 18:03

In the first case (hotkey), it looks like you are replacing the line feeds, right? Does every line start with a line feed?

I suggest that you avoid assigning the clipboard to be a function of the clipboard. You could work with a separate variable, or consider adding a sleep command after the assignment (before using the clipboard), because-- as your script reflects elsewhere-- the clipboard is a slow beast.
User avatar
sn1perwild
Posts: 62
Joined: 04 Aug 2021, 15:11

Re: Creating hotkeys to edit lists — issue with first/last lines

16 Sep 2021, 07:22

mikeyww wrote:
14 Sep 2021, 18:03
In the first case (hotkey), it looks like you are replacing the line feeds, right? Does every line start with a line feed?

I suggest that you avoid assigning the clipboard to be a function of the clipboard. You could work with a separate variable, or consider adding a sleep command after the assignment (before using the clipboard), because-- as your script reflects elsewhere-- the clipboard is a slow beast.
Hey, mikeyww!

I've used line feeds as the pattern as the only way I could to get the replacement to go at the beginning and end of each line. Is there a more effective way to do that?

As for the clipboard, I actually don't mind the script's speed. I believe the problem is simply the RegEx pattern used to match beginnings/ends of each line in selection.

Cheers!
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: Creating hotkeys to edit lists — issue with first/last lines  Topic is solved

16 Sep 2021, 07:32

You did not answer my question, "Does every line start with a line feed?", so I will assume that the answer is "No". Example:

Code: Select all

Clipboard := RegExReplace("`n" Clipboard, "`n", text)
User avatar
sn1perwild
Posts: 62
Joined: 04 Aug 2021, 15:11

Re: Creating hotkeys to edit lists — issue with first/last lines

16 Sep 2021, 08:54

mikeyww wrote:
16 Sep 2021, 07:32
You did not answer my question, "Does every line start with a line feed?", so I will assume that the answer is "No". Example:

Code: Select all

Clipboard := RegExReplace("`n" Clipboard, "`n", text)
Sorry, that was a no indeed.

That tweak worked wonderfully. I adapted the other one as such:

Code: Select all

Clipboard := RegExReplace(Clipboard "`r", "`r", text)
Thank you very much once again :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 211 guests