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

asked on

Autohotkey: select text from current position of the cursor to a specific line v2

Hello experts,
I use the following procedure reported at: https://www.experts-exchange.com/questions/29151200 to go to a line and select text from current position:
; Joe Winograd 8-Jul-2019
Version:="1.2"
#Warn,UseUnsetLocal ; warning on uninitialized variables
#NoEnv ; avoid checking empty variables to see if they are environment variables
#SingleInstance Force ; replace old instance immediately
SetBatchLines,-1 ; run at maximum speed
SendMode Input ; faster and more reliable
TrayIconFile:="c:\Windows\System32\shell32.dll" ; use an icon from shell32.dll for the system tray
TrayIconNum:="-16775" ; clipboard icon
Menu,Tray,Icon,%TrayIconFile%,%TrayIconNum% ; display chosen icon in system tray
TrayTip:=A_ScriptName . " Version " . Version . "`nHotkey is Ctrl+F12" ; tray tip showing what the hotkey is
Menu,Tray,Tip,%TrayTip% ; display system tray tip
SetTitleMatchMode,3 ; exact match when looking for NPP GoTo dialog

^F12:: ; hotkey to perform action - this is Ctrl+F12 - use whatever hotkey you want
OldX:=A_CaretX ; get X pos of current text insertion point
OldY:=A_CaretY ; get Y pos of current text insertion point
Send ^g ; run GoTo in NPP
WinWaitActive,Go To... ; wait until GoTo dialog appears
WinWaitNotActive,Go To... ; wait until GoTo dialog disappears
Sleep 50 ; give it time to get to the new line
NewY:=A_CaretY ; get Y pos of new text insertion point
If (OldY=NewY)
{
    MsgBox,4144,Error,New line is same as old line
    Return
}
If (OldY<NewY)
  MoveDirection:="Down"
Else
  MoveDirection:="Up"
Loop ; go back to old Y line
{
  If (MoveDirection="Down") ; directions are reversed because we're going from new line back to old line
    Send {Up}
  Else
    Send {Down}
  Sleep 10 ; give it time to get there
  CurrentY:=A_CaretY ; get current Y pos
  If (CurrentY=OldY)
    Break ; found old line
}
; found old Y line, now go to old X loc
CurrentX:=A_CaretX ; get current X pos (should be column 1 after GoTo)
Loop ; find old X insertion point
{
  If (CurrentX=OldX)
    Break ; found old insertion point
  Send {Right}
  Sleep 10 ; give it time to get there
}
Loop ; go back to new line, keeping shift pressed as we go
{
  Send {Shift Down} ; keep the shift key pressed
  If (MoveDirection="Down")
  {
    Send {Down}
    If (CurrentY>=NewY)
      Break ; found new line
  }
  Else
  {
    Send {Up}
    If (CurrentY<=NewY)
      Break ; found new line
  }
  Sleep 10 ; give it time to get there
  CurrentY:=A_CaretY ; get current Y pos
  If (CurrentY=NewY)
    Break ; found new line
}
If (OldY<NewY) ; see if Home or End is needed - depends on relative locations of OldY and NewY
  Send {End} ; go to end of new line - shift is still pressed, so it will keep selecting
Else
  Send {Home} ; go to beginning of new line - shift is still pressed, so it will keep selecting
Sleep 10 ; give it time to get there
Send {Shift Up} ; release shift key
Return ; done

Open in new window


I would like to add the following requirement:
1-Be able to launch the procedure in any application that support ctrl + g shortcut to go to a line.
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

launch the procedure in any application that support ctrl + g shortcut to go to a line
Hi Luis,

Even though an app supports Ctrl+g as the shortcut for going to a line, that doesn't mean that the same set of keystrokes that work in one app will work in another. You will very likely need code that knows what each app requires.

Here's a good example. Read this EE article:

How to copy the short link of an ID at Experts Exchange to the clipboard with a single keystroke

The AutoHotkey code in that article is trying to perform the same function in all browsers, but it needs to know which browser is running so that it can send the correct keystrokes...hence, the IF tests on the Windows Title, looking for Chrome, Firefox, IE, Opera.

The same is true for what you want to do. For example, Ctrl+g in Notepad++ brings up a dialog box with "GoTo..." in the Title bar. Look at my script and you'll see that it tests for this:

WinWaitActive,Go To... ; wait until GoTo dialog appears

But even though Notepad supports Ctrl+g, the dialog box has "Go To Line" in the Title bar. Likewise, I use an editor called KEDIT. It also supports Ctrl+g, but it has "Go To" in the Title bar. Beyond just that issue, the same keystrokes can do different things in different products. For example, the Enter key creates a new line in NPP and Notepad, but I have it configured to go to the next line in KEDIT without creating a new line.

The point is, you need to write code that supports each product. Of course, you may put it all in one script with proper conditional statements that execute the varying logic, as I did in my article mentioned above and in the recent script for you to support both 32-bit and 64-bit Excel on different systems. Regards, Joe
Avatar of Luis Diaz

ASKER

Noted Joe. Thank you for this detailed feedback. The aim is to use shortcut in 4 apps:
Sql, Npp++, VBA, VBS. Ctrl + G shortcut perform the same action in there various apps:
Go to specific line without inserting a new line. Saying that is there a way to apply this useful shortcuts in the various  apps. I supposed that you need Windows Id related to each apps to built winwait loop, am I correct?
ASKER CERTIFIED 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
Thank you Joe,
I will take the time to test it tomorrow. Unable to test it right now.
Thank you very much for your help.
You're welcome, Luis. Looking forward to your feedback tomorrow. Regards, Joe
Joe, I tested shortcut on the 4 apps and shortcut work as expected. In order to make it available for 4 apps what is your opinion on this:  
1-Adding a loop to send the shortcut if activewindows is related to one of the 4 apps. In that case I supposed that I need to identify the id of the windows?
2-Remove the activewindows loop and make sure that I launch the shortcut exclusively for the 4 apps?
Thank you in advance for your help.
> tested shortcut on the 4 apps and shortcut work as expected

Great news!

> I supposed that I need to identify the id of the windows?

Yes, check the window Title to see if it is one of the four "go-to" dialogs. The AutoHotkey script that I posted at your other question will reveal the window Title. For example, here's what the window Title is in the go-to dialog of three text editors on my system:

KEDIT: Go To
Notepad: Go To Line
Notepad++: Go To...

There are many ways to do what you want in AutoHotkey. One idea is to check for the name of the process. I mentioned WinTitle earlier...study that doc. Then test the script below on the four programs...of course, you'll need to change all the "Whatever" strings in the WinActive function calls and the GotoTitle assignment statements to have the correct values for the other three programs (I know that NPP is correct):

^F10::
If (WinActive("ahk_exe notepad++.exe"))
  GotoTitle:="Go To..."
Else
If (WinActive("ahk_exe WhateverSQLis.exe"))
  GotoTitle:="Go To - Whatever SQL "
Else
If (WinActive("ahk_exe WhateverVBAis.exe"))
  GotoTitle:="Go To - Whatever SQL "
Else
If (WinActive("ahk_exe WhateverVBSis.exe"))
  GotoTitle:="Go To - Whatever SQL "
Else
{
  MsgBox,4144,Error,Active program is not one of the four
  Return
}
MsgBox,4160,Go To Title,%GotoTitle%
Return

Open in new window

I made the hotkey Ctrl+F10 in the above script...as always, make it whatever you want.

You would then incorporate the code above into the main script...and the new WinWaitActive,%GotoTitle% and WinWaitNotActive,%GotoTitle% statements would then work for all four programs. Regards, Joe
Noted, I will try to get this before Friday. Thank you very much for your help.
You're very welcome.
Ok, Joe, I retested and I have some little issues with vbsedit. Selection is not done properly.
I think that I will keep the ahk for exclusively notepad++  which is the editor that I used the most and if in the future I see the need to use it for another app such as vbsedit I will create a new question.
I attached video of my test.
You were totally right concerning the fact that this ahk should be used  for notepad++.
Thank you again for your help and sorry for the delay.
screen_recorder_video_2019_20_7_23_4.zip
> Selection is not done properly.

That's what I was afraid of with other products. It's probably doable, but you'd have to figure out the keystrokes needed for any particular product. Regards, Joe
Noted, thank you very much.