Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

AutoHotkey: add line number based on selected text

Hello experts,

The following AutoHotkey script allows me to add specific character at the beginning of each line:

;====================================
;Add specific character at the beginning of each line
;====================================

^+-::
Send, ^c
Sleep, 50
ClipWait,1
If (ErrorLevel=1)
{
  MsgBox,4144,Error, No text appearing after one second
  Return
}
ClipboardVar:=Clipboard
UpdatedLines:=""
Loop,Parse,ClipboardVar,`n,`r
  UpdatedLines:=UpdatedLines . "-" . A_LoopField . "`n"
Clipboard:=UpdatedLines
Sleep, 50
Send, ^v
Return

Open in new window



I was wondering if I can take as a reference to add line number for each line.

Example:

I have the following text block:

text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text

-I select the text
-I launch the AutoHotkey script and I expect to have the following:

1.text text text text text text text text text text text text text
2.text text text text text text text text text text text text text text text text
3.text text text text text text text text text text text text text text text text text

If you have questions, please contact me.

Thank you for your help.
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Hi Luis,
Here you go:


;====================================
;Add line numbers at the beginning of each line
;====================================

^+l:: ; Ctrl+Shift+L - as always, make it whatever you want
Send, ^c
Sleep, 50
ClipWait,1
If (ErrorLevel=1)
{
  MsgBox,4144,Error, No text appearing after one second
  Return
}
LineNum:=0
ClipboardVar:=Clipboard
UpdatedLines:=""
Loop,Parse,ClipboardVar,`n,`r
{
  LineNum:=LineNum+1
  UpdatedLines:=UpdatedLines . LineNum . "." . A_LoopField . "`n"
}
Clipboard:=UpdatedLines
Sleep, 50
Send, ^v
Return

Open in new window

If you want a space after the dot, change line 20 to this:

  UpdatedLines:=UpdatedLines . LineNum . ". " . A_LoopField . "`n"

Open in new window

Regards, Joe
Avatar of Luis Diaz

ASKER

Hi Joe,

This script is amazing! I expect to have some issues with word 2016 but it works nicely.
In notepad++ it also works!

I am going to continue testing in a moment in other text editor that I used and I will keep you informed if I see a review to perform such as how to manage text selected with blank lines.

Thank you again!
Regards,
Luis
1.Tested here in:
2.Notepad
3.Notepad++
4.Word 365
5.And in this comment box, as you can see. :)
6.All worked perfectly! Regards, Joe
Yes it works very nice!

I am just having a dilemma concerning blank lines.

I don't know if the best approach for the following case :

text text text

text text text
text text text

Is to manage blank lines as normal lines:

1.text text text
2.
3.text text text
4.text text text

Or avoid blank lines when it comes to add the numbering:

1.text text text

2.text text text
3.text text text
> Yes it works very nice!

Glad to hear it.

> I am just having a dilemma concerning blank lines.

Of course, I can't help you with that...it's whatever you want. You could even have the capability to do both...pick the one you want each time you use it, depending on the circumstances.

Thank you very much for your comment Joe!

Possible the following 2 approachs:

1.Avoid blank lines when it comes to add the numbering:

Example:

Text 

Text


Text


Result:

1.Text 

2.Text


3.Text


2.Have a GUI as you proposed at: https://www.experts-exchange.com/questions/29170091/Autohotkey-copy-file-folder-with-msgbox-gui.html in order to have the option to have numbering with blank lines or without blank lines.


Thank you very much for your help!


Regards,

Luis.

Yes, those are both possible approaches. A third approach is a simple MsgBox, since there are only two choices for numbering (with a third choice for exiting). It's easy to do a 3-button MsgBox, something like this:

User generated image
Regards, Joe
Agreed. Thank you.
Regards,
Luis.
> Agreed.

Can you do that yourself or do you want me to code it for you?
SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Joe,
Sorry for the delay. I tested your proposal in Word 365 + Notepad +  and it works!
I have just a question, If I want to remove the extra lines added at the end when the revised text is pasted, Can I remove . "`n" from line 33?
> Sorry for the delay.

That's fine...I'm back now.

> I tested your proposal in Word 365 + Notepad + and it works!

Great news!

> Can I remove . "`n" from line 33?

No! If you remove that, all lines will be on a single line!
Sorry my bad, If I want to avoid the return at the end of text selected, how can I proceed?

User generated image
Another concern that I have: Combination Ctrl+Shift+L is the right one for this. However this clash with filter activation on Excel which I use on a daily basis. As a result I was thinking to restrict the AutoHotkey to applications in which I am going to use like this:

;====================================
;Add line numbers at the beginning of each line
;====================================

#If (WinActive("ahk_exe notepad++.exe") or WinActive("ahk_exe winword.exe") or WinActive("ahk_exe outlook.exe") or WinActive("ahk_exe ahk_exe powerpnt.exe") or WinActive("ahk_exe chrome.exe"))
^+l:: ; Ctrl+Shift+L - as always, make it whatever you want
SkipBlankLines:=True
MsgBox,4131,Do you want to number blank lines?,Press YES to number the blank lines`n`nPress NO not to number the blank lines`n`nPress CANCEL to exit/quit (not do any numbering)
IfMsgBox,Cancel
  Return
IfMsgBox,Yes
  SkipBlankLines:=False
Sleep, 50
Send, ^c
Sleep, 50
ClipWait,1
If (ErrorLevel=1)
{
  MsgBox,4144,Error, No text appearing after one second
  Return
}
LineNum:=0
ClipboardVar:=Clipboard
UpdatedLines:=""
Loop,Parse,ClipboardVar,`n,`r
{
  If ((A_LoopField="") and (SkipBlankLines))
    LineNumOut:=""
  Else
  {
    LineNum:=LineNum+1
    LineNumOut:=LineNum . "."
  }
  UpdatedLines:=UpdatedLines . LineNumOut . A_LoopField . "`n"
}
Clipboard:=UpdatedLines
Sleep, 50
Send, ^v
Return
#If

Open in new window


Do you see another approach for this, knowing that the objective is to use for this script Ctrl+Shift+L and make available the default Ctrl+Shift+L for Excel.

Thank you for your help.

Regards,
Luis.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Joe,
Thank you very much for your last comment and proposal. I tested and it works!
I just want to rise a little issue that I am having.
Just for the record but I believe that the entire need has been fully covered.
The AutoHotkey has an strange behavior when I compose a mail on gmail.
I attached a video.
Gmail propose the same add line number mechanism through the following combination (Ctrl + Shift + 7).
I don't know if the best approach is to add another exception to have an standard combination. When WinActive contains gmail as a title it will not performed the operation but just send Ctrl + Shift + 7 to attain the need.
Other option will be to also exclude gmail as you proposed for excel.
Or if winactive related to gmail, display a message and inform that the right combination for gmail is Ctrl + Shift + 7.

Regards,
Luis.
20200129012154_Add-lines-LD-video_20.zip
Joe,

Thank you very much for your last comment and proposal. I tested and it works!
I just want to rise a little issue that I am having.
Just for the record but I believe that the entire need has been fully covered.

The AutoHotkey has an strange behavior when I compose a mail on gmail.
I attached a video.

Gmail propose the same add line number mechanism through the following combination (Ctrl + Shift + 7).
I don't know if the best approach is to add another exception to have an standard combination. When WinActive contains gmail as a title it will not performed the operation but just send Ctrl + Shift + 7 to attain the need.
Other option will be to also exclude gmail as you proposed for excel.
Or if winactive related to gmail, display a message and inform that the right combination for gmail is Ctrl + Shift + 7.

Regards,
Luis.
> I tested and it works!

Glad to hear it!

> when I compose a mail on gmail

I never use the Gmail web portal (I do all my Gmail with an email client), so I can't help you from personal experience with this issue. But if I'm understanding you right, your ideas are fine, i.e., put in an exception for the hotkey when in Gmail, perhaps also displaying a MsgBox reminding the user of Ctrl+Shift+7.

Regards, Joe
Noted. Thank you very much Joe for your help!
You're very welcome!